vscode-chrome-debug/README.md

249 строки
19 KiB
Markdown
Исходник Обычный вид История

2016-07-29 02:18:45 +03:00
<h1 align="center">
<br>
2017-10-20 01:29:03 +03:00
<img src="https://github.com/Microsoft/vscode-chrome-debug/blob/master/images/icon.png?raw=true" alt="logo" width="200">
2016-07-29 02:18:45 +03:00
<br>
VS Code - Debugger for Chrome
<br>
<br>
</h1>
2016-07-29 02:23:27 +03:00
<h4 align="center">Debug your JavaScript code running in Google Chrome from VS Code.</h4>
2016-07-29 02:18:45 +03:00
<p align="center">
2018-07-03 20:30:44 +03:00
<a href="https://vscode.visualstudio.com/1e32b5a6-a974-467b-9d5f-f47e49589c5e/_build/definition?definitionId=9"><img src="https://vscode.visualstudio.com/_apis/public/build/definitions/1e32b5a6-a974-467b-9d5f-f47e49589c5e/9/badge" alt="vsts"></a>
<a href="https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome"><img src="https://vsmarketplacebadge.apphb.com/version/msjsdiag.debugger-for-chrome.svg?label=Debugger%20for%20Chrome" alt="Marketplace bagde"></a>
2016-07-29 02:21:56 +03:00
<a href="https://gitter.im/Microsoft/vscode-chrome-debug?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://badges.gitter.im/Microsoft/vscode-chrome-debug.svg" alt="Release"></a>
2016-07-29 02:18:45 +03:00
</p>
2015-12-27 09:13:11 +03:00
2017-06-25 21:14:07 +03:00
A VS Code extension to debug your JavaScript code in the Google Chrome browser, or other targets that support the [Chrome DevTools Protocol](https://chromedevtools.github.io/debugger-protocol-viewer/).
2015-10-30 19:42:45 +03:00
2017-10-20 01:29:03 +03:00
![Demo](https://github.com/Microsoft/vscode-chrome-debug/blob/master/images/demo.gif?raw=true)
2015-11-12 03:38:50 +03:00
2016-07-29 02:18:45 +03:00
**Supported features**
* Setting breakpoints, including in source files when source maps are enabled
* Stepping, including with the buttons on the Chrome page
* The Locals pane
* Debugging eval scripts, script tags, and scripts that are added dynamically
* Watches
* Console
2016-04-29 04:44:46 +03:00
2016-07-29 02:18:45 +03:00
**Unsupported scenarios**
* Debugging web workers
* Any features that aren't script debugging.
2015-10-30 19:42:45 +03:00
2016-07-29 02:18:45 +03:00
## Getting Started
1. [Install the extension](https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome)
2019-04-03 20:36:24 +03:00
2. Open the folder containing the project you want to work on.
2015-10-30 19:42:45 +03:00
2016-07-29 02:18:45 +03:00
## Using the debugger
When your launch config is set up, you can debug your project. Pick a launch config from the dropdown on the Debug pane in Code. Press the play button or F5 to start.
2016-07-29 02:18:45 +03:00
### Configuration
2016-07-29 02:18:45 +03:00
2017-11-13 15:33:29 +03:00
The extension operates in two modes - it can launch an instance of Chrome navigated to your app, or it can attach to a running instance of Chrome. Both modes requires you to be serving your web application from local web server, which is started from either a VS Code task or from your command-line. Using the `url` parameter you simply tell VS Code which URL to either open or launch in Chrome.
Just like when using the Node debugger, you configure these modes with a `.vscode/launch.json` file in the root directory of your project. You can create this file manually, or Code will create one for you if you try to run your project, and it doesn't exist yet.
2016-07-29 02:18:45 +03:00
2018-05-19 23:24:08 +03:00
> **Tip**: See recipes for debugging different frameworks here: https://github.com/Microsoft/vscode-recipes
2015-10-30 19:42:45 +03:00
### Launch
2017-11-16 02:13:16 +03:00
Two example `launch.json` configs with `"request": "launch"`. You must specify either `file` or `url` to launch Chrome against a local file or a url. If you use a url, set `webRoot` to the directory that files are served from. This can be either an absolute path or a path using `${workspaceFolder}` (the folder open in Code). `webRoot` is used to resolve urls (like "http://localhost/app.js") to a file on disk (like `/Users/me/project/app.js`), so be careful that it's set correctly.
```json
2015-10-30 19:42:45 +03:00
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch localhost",
2015-11-12 02:18:17 +03:00
"type": "chrome",
"request": "launch",
2015-11-12 02:18:17 +03:00
"url": "http://localhost/mypage.html",
2017-11-16 02:13:16 +03:00
"webRoot": "${workspaceFolder}/wwwroot"
2015-11-23 06:37:31 +03:00
},
{
2018-05-31 01:36:54 +03:00
"name": "Launch index.html",
2015-11-12 02:18:17 +03:00
"type": "chrome",
"request": "launch",
2017-11-16 02:13:16 +03:00
"file": "${workspaceFolder}/index.html"
2016-03-19 01:58:11 +03:00
},
2015-10-30 19:42:45 +03:00
]
}
```
2017-11-13 15:33:29 +03:00
If you want to use a different installation of Chrome, you can also set the `runtimeExecutable` field with a path to the Chrome app.
2015-10-30 21:50:44 +03:00
2015-10-30 19:42:45 +03:00
### Attach
2017-01-31 01:21:04 +03:00
With `"request": "attach"`, you must launch Chrome with remote debugging enabled in order for the extension to attach to it. Here's how to do that:
2015-11-12 02:18:17 +03:00
__Windows__
* Right click the Chrome shortcut, and select properties
2015-10-30 19:42:45 +03:00
* In the "target" field, append `--remote-debugging-port=9222`
2015-11-12 02:18:17 +03:00
* Or in a command prompt, execute `<path to chrome>/chrome.exe --remote-debugging-port=9222`
2016-11-07 00:44:44 +03:00
__macOS__
2015-11-12 02:18:17 +03:00
* In a terminal, execute `/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222`
2015-10-30 19:42:45 +03:00
__Linux__
* In a terminal, launch `google-chrome --remote-debugging-port=9222`
2017-04-11 08:33:57 +03:00
If you have another instance of Chrome running and don't want to restart it, you can run the new instance under a separate user profile with the `--user-data-dir` option. Example: `--user-data-dir=/tmp/chrome-debug`. This is the same as using the `userDataDir` option in a launch-type config.
2017-03-02 21:02:54 +03:00
2015-10-30 19:42:45 +03:00
Launch Chrome and navigate to your page.
2017-01-31 01:21:04 +03:00
An example `launch.json` file for an "attach" config.
```json
2015-10-30 19:42:45 +03:00
{
"version": "0.1.0",
"configurations": [
2015-11-23 06:37:31 +03:00
{
"name": "Attach to url with files served from ./out",
"type": "chrome",
"request": "attach",
"port": 9222,
2016-11-03 21:50:40 +03:00
"url": "<url of the open browser tab to connect to>",
2018-05-31 01:36:54 +03:00
"webRoot": "${workspaceFolder}/out"
2015-10-30 19:42:45 +03:00
}
]
}
```
2018-05-05 00:35:42 +03:00
### Chrome user profile note (`Cannot connect to the target: connect ECONNREFUSED`)
Normally, if Chrome is already running when you start debugging with a launch config, then the new instance won't start in remote debugging mode. So by default, the extension launches Chrome with a separate user profile in a temp folder. Use the `userDataDir` launch config field to override or disable this. If you are using the `runtimeExecutable` field, this isn't enabled by default, but you can forcibly enable it with `"userDataDir": true`.
If you are using an attach config, make sure you close other running instances of Chrome before launching a new one with `--remote-debugging-port`. Or, use a new profile with the `--user-data-dir` flag yourself.
For other troubleshooting tips for this error, [see below](#cannot-connect-to-the-target:-connect-ECONNREFUSED-127.0.0.1:9222).
2018-12-05 06:09:23 +03:00
### Errors from chrome-error://chromewebdata
If you see errors with a location like `chrome-error://chromewebdata/` in the error stack, these errors are not from the extension or from your app - they are usually a sign that Chrome was not able to load your app.
2019-04-05 16:45:05 +03:00
When you see these errors, first check whether Chrome was able to load your app. Does Chrome say "This site can't be reached" or something similar? You must start your own server to run your app. Double-check that your server is running, and that the url and port are configured correctly.
2018-12-05 06:09:23 +03:00
### Other targets
You can also theoretically attach to other targets that support the same Chrome Debugging protocol, such as Electron or Cordova. These aren't officially supported, but should work with basically the same steps. You can use a launch config by setting `"runtimeExecutable"` to a program or script to launch, or an attach config to attach to a process that's already running. If Code can't find the target, you can always verify that it is actually available by navigating to `http://localhost:<port>/json` in a browser. If you get a response with a bunch of JSON, and can find your target page in that JSON, then the target should be available to this extension.
2016-07-29 02:18:45 +03:00
### Examples
See our wiki page for some configured example apps: [Examples](https://github.com/Microsoft/vscode-chrome-debug/wiki/Examples)
2015-11-30 04:04:16 +03:00
### Other optional launch config fields
2017-11-17 20:21:48 +03:00
* `trace`: When true, the adapter logs its own diagnostic info to a file. The file path will be printed in the Debug Console. This is often useful info to include when filing an issue on GitHub. If you set it to "verbose", it will also log to the console.
2017-11-13 15:33:29 +03:00
* `runtimeExecutable`: Workspace relative or absolute path to the runtime executable to be used. If not specified, Chrome will be used from the default install location.
* `runtimeArgs`: Optional arguments passed to the runtime executable.
* `env`: Optional dictionary of environment key/value pairs.
* `cwd`: Optional working directory for the runtime executable.
2017-04-11 08:33:57 +03:00
* `userDataDir`: Normally, if Chrome is already running when you start debugging with a launch config, then the new instance won't start in remote debugging mode. So by default, the extension launches Chrome with a separate user profile in a temp folder. Use this option to set a different path to use, or set to false to launch with your default user profile.
2017-03-19 02:30:12 +03:00
* `url`: On a 'launch' config, it will launch Chrome at this URL.
2017-11-13 15:33:29 +03:00
* `urlFilter`: On an 'attach' config, or a 'launch' config with no 'url' set, search for a page with this url and attach to it. It can also contain wildcards, for example, `"localhost:*/app"` will match either `"http://localhost:123/app"` or `"http://localhost:456/app"`, but not `"https://stackoverflow.com"`.
* `targetTypes`: On an 'attach' config, or a 'launch' config with no 'url' set, set a list of acceptable target types from the default `["page"]`. For example, if you are attaching to an Electron app, you might want to set this to `["page", "webview"]`. A value of `null` disables filtering by target type.
* `sourceMaps`: By default, the adapter will use sourcemaps and your original sources whenever possible. You can disable this by setting `sourceMaps` to false.
2017-11-16 02:13:16 +03:00
* `pathMapping`: This property takes a mapping of URL paths to local paths, to give you more flexibility in how URLs are resolved to local files. `"webRoot": "${workspaceFolder}"` is just shorthand for a pathMapping like `{ "/": "${workspaceFolder}" }`.
2017-03-07 04:06:02 +03:00
* `smartStep`: Automatically steps over code that doesn't map to source files. Especially useful for debugging with async/await.
* `disableNetworkCache`: If true, the network cache will be disabled.
2017-04-04 02:09:13 +03:00
* `showAsyncStacks`: If true, callstacks across async calls (like `setTimeout`, `fetch`, resolved Promises, etc) will be shown.
2017-01-05 18:09:25 +03:00
## Skip files / Blackboxing / Ignore files
You can use the `skipFiles` property to ignore/blackbox specific files while debugging. For example, if you set `"skipFiles": ["jquery.js"]`, then you will skip any file named 'jquery.js' when stepping through your code. You also won't break on exceptions thrown from 'jquery.js'. This works the same as "blackboxing scripts" in Chrome DevTools.
2017-01-03 11:40:41 +03:00
The supported formats are:
2016-11-05 00:00:40 +03:00
* The name of a file (like `jquery.js`)
* The name of a folder, under which to skip all scripts (like `node_modules`)
* A path glob, to skip all scripts that match (like `node_modules/react/*.min.js`)
2016-11-05 00:00:40 +03:00
2016-12-06 03:57:03 +03:00
## Page refreshing
2018-09-12 19:04:59 +03:00
This debugger also enables you to refresh your target by simply hitting the restart button in the debugger UI. Additionally you can map the refresh action to your favorite keyboard shortcut by adding the following key mapping to [Key Bindings](https://code.visualstudio.com/docs/getstarted/keybindings):
2016-12-06 03:57:03 +03:00
```json
2016-12-06 03:57:03 +03:00
{
2016-12-06 03:57:49 +03:00
"key": "ctrl+r",
2016-12-06 03:57:03 +03:00
"command": "workbench.action.debug.restart",
"when": "inDebugMode"
}
```
Read more here https://github.com/Microsoft/vscode-chrome-debug-core/issues/91#issuecomment-265027348
2017-01-03 11:44:50 +03:00
## Sourcemaps
2018-03-06 03:18:29 +03:00
The debugger uses sourcemaps to let you debug with your original sources, but sometimes the sourcemaps aren't generated properly and overrides are needed. In the config we support `sourceMapPathOverrides`, a mapping of source paths from the sourcemap, to the locations of these sources on disk. Useful when the sourcemap isn't accurate or can't be fixed in the build process.
2017-01-03 11:44:50 +03:00
The left hand side of the mapping is a pattern that can contain a wildcard, and will be tested against the `sourceRoot` + `sources` entry in the source map. If it matches, the source file will be resolved to the path on the right hand side, which should be an absolute path to the source file on disk.
A few mappings are applied by default, corresponding to some common default configs for Webpack and Meteor:
```javascript
// Note: These are the mappings that are included by default out of the box, with examples of how they could be resolved in different scenarios. These are not mappings that would make sense together in one project.
// webRoot = /Users/me/project
2017-01-03 11:44:50 +03:00
"sourceMapPathOverrides": {
"webpack:///./~/*": "${webRoot}/node_modules/*", // Example: "webpack:///./~/querystring/index.js" -> "/Users/me/project/node_modules/querystring/index.js"
"webpack:///./*": "${webRoot}/*", // Example: "webpack:///./src/app.js" -> "/Users/me/project/src/app.js",
"webpack:///*": "*", // Example: "webpack:///project/app.ts" -> "/project/app.ts"
"webpack:///src/*": "${webRoot}/*", // Example: "webpack:///src/app.js" -> "/Users/me/project/app.js"
"meteor://💻app/*": "${webRoot}/*" // Example: "meteor://💻app/main.ts" -> "/Users/me/project/main.ts"
2017-01-03 11:44:50 +03:00
}
```
2017-11-16 02:13:16 +03:00
If you set `sourceMapPathOverrides` in your launch config, that will override these defaults. `${workspaceFolder}` and `${webRoot}` can be used here. If you aren't sure what the left side should be, you can use the `.scripts` command (details below). You can also use the `trace` option to see the contents of the sourcemap, or look at the paths of the sources in Chrome DevTools, or open your `.js.map` file and check the values manually.
2017-01-03 11:44:50 +03:00
### Ionic/gulp-sourcemaps note
Ionic and gulp-sourcemaps output a sourceRoot of `"/source/"` by default. If you can't fix this via your build config, I suggest this setting:
```json
"sourceMapPathOverrides": {
2017-11-16 02:13:16 +03:00
"/source/*": "${workspaceFolder}/*"
}
```
2015-11-12 02:18:17 +03:00
2015-11-12 03:38:50 +03:00
## Troubleshooting
### My breakpoints aren't hit. What's wrong?
2017-11-13 15:33:29 +03:00
If your breakpoints aren't hit, it's most likely a sourcemapping issue or because you are having breakpoints in immediately executed code. If you for example have a breakpoint in a `render function` that runs on page load, sometimes our debugger might not be attached to Chrome before the code has been executed. This means that you will have to refresh the page in Chrome after we have attached from VS Code to hit your breakpoint. We are working in simplify this in with "break-on-load" breakpoints in https://github.com/Microsoft/vscode-chrome-debug/issues/445, which will make this timing issue transparent.
If you have a sourcemapping issue, please see https://github.com/Microsoft/vscode-chrome-debug#sourcemaps
### Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9222
This message means that the extension can't attach to Chrome, because Chrome wasn't launched in debug mode. Here are some things to try:
2018-05-05 00:35:42 +03:00
* If using an `attach` type config, ensure that you launched Chrome using `--remote-debugging-port=9222`. And if there was already a running instance, close it first or see note about `--user-data-dir` above.
* Ensure that the `port` property matches the port on which Chrome is listening for remote debugging connections. This is `9222` by default. Ensure nothing else is using this port, including your web server. If something else on your computer responds at `http://localhost:9222`, then set a different port.
2017-08-29 21:41:45 +03:00
* If using a `launch` type config with the `userDataDir` option explicitly disabled, close other running instances of Chrome - if Chrome is already running, the extension may not be able to attach, when using launch mode. Chrome can even stay running in the background when all its windows are closed, which will interfere - check the taskbar or kill the process if necessary.
2018-05-05 00:35:42 +03:00
* If all else fails, try to navigate to `http://localhost:<port>/json` in a browser when you see this message - if there is no response, then something is wrong upstream of the extension. If there is a page of JSON returned, then ensure that the `port` in the launch config matches the port in that url.
### General things to try if you're having issues:
2015-11-23 07:16:08 +03:00
* Ensure `webRoot` is set correctly if needed
2016-03-23 07:39:28 +03:00
* Look at your sourcemap config carefully. A sourcemap has a path to the source files, and this extension uses that path to find the original source files on disk. Check the `sourceRoot` and `sources` properties in your sourcemap and make sure that they can be combined with the `webRoot` property in your launch config to build the correct path to the original source files.
* This extension ignores sources that are inlined in the sourcemap - you may have a setup that works in Chrome Dev Tools, but not this extension, because the paths are incorrect, but Chrome Dev Tools are reading the inlined source content.
2017-11-13 15:33:29 +03:00
* Check the console for warnings that this extension prints in some cases when it can't attach.
2015-11-12 03:38:50 +03:00
* Ensure the code in Chrome matches the code in Code. Chrome may cache an old version.
* If your breakpoints bind, but aren't hit, try refreshing the page. If you set a breakpoint in code that runs immediately when the page loads, you won't hit that breakpoint until you refresh the page.
2017-11-17 20:21:48 +03:00
* File a bug in this extension's [GitHub repo](https://github.com/Microsoft/vscode-chrome-debug), including the debug adapter log file. Create the log file by setting the "trace" field in your launch config and reproducing the issue. It will print the path to the log file at the top of the Debug Console. You can drag this file into an issue comment to upload it to GitHub.
* If you're using Webpack, we recommend using the `"devtool": "source-map"` option (in your `webpack.config.js` file) as the others produce lower-fidelity sourcemaps and you may have issues setting breakpoints. See the [full list of devtool options for webpack](https://webpack.js.org/configuration/devtool/) for more information.
2016-06-21 23:33:34 +03:00
2016-11-03 21:50:40 +03:00
### The `.scripts` command
2017-11-17 20:21:48 +03:00
This feature is extremely useful for understanding how the extension maps files in your workspace to files running in Chrome. You can enter `.scripts` in the Debug Console to see a listing of all scripts loaded in the runtime, their sourcemap information, and how they are mapped to files on disk. The format is like this:
2016-11-03 21:50:40 +03:00
```
<The exact URL for a script, reported by Chrome> (<The local path that has been inferred for this script, using webRoot, if applicable>)
- <The exact source path from the sourcemap> (<The local path inferred for the source, using sourceMapPathOverrides, or webRoot, etc, if applicable>)
```
Example:
```
.scripts
eval://43
http://localhost:8080/index.html (/Users/me/project/wwwroot/index.html)
http://localhost:8080/out/test1.js (/Users/me/project/wwwroot/out/test1.js)
- /src/test1a.ts (/Users/me/project/wwwroot/src/test1a.ts)
- /src/test1b.ts (/Users/me/project/wwwroot/src/test1b.ts)
- /src/test1c.ts (/Users/me/project/wwwroot/src/test1c.ts)
http://localhost:8080/out/test2.js (/Users/me/project/wwwroot/out/test2.js)
- /src/test2.ts (/Users/me/project/wwwroot/src/test2.ts)
```
2018-05-12 21:09:28 +03:00
If the paths of your source files show as not being resolved correctly here, you may have to change `sourceMapPathOverrides` or `webRoot` to help the debugger resolve them to real paths on disk.
2016-11-03 21:50:40 +03:00
If you are wondering what a script is, for example, that 'eval' script, you can also use `.scripts` to get its contents: `.scripts eval://43`.
2018-05-12 21:06:54 +03:00
---
2016-06-21 23:33:34 +03:00
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.