1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 05:14:21 -05:00
denoland-deno/docs/tools/debugger.md

146 lines
4.6 KiB
Markdown
Raw Normal View History

2020-05-06 18:21:13 -04:00
## Debugger
2020-06-10 06:48:07 -04:00
Deno supports the [V8 Inspector Protocol](https://v8.dev/docs/inspector).
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
It's possible to debug Deno programs using Chrome Devtools or other clients that
support the protocol (eg. VSCode).
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
To activate debugging capabilities run Deno with the `--inspect` or
`--inspect-brk` flags.
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
The `--inspect` flag allows attaching the debugger at any point in time, while
`--inspect-brk` will wait for the debugger to attach and will pause execution on
the first line of code.
2020-05-13 11:55:44 -04:00
### Chrome Devtools
2020-06-10 06:48:07 -04:00
Let's try debugging a program using Chrome Devtools. For this, we'll use
[file_server.ts](https://deno.land/std@v0.50.0/http/file_server.ts) from `std`,
2020-05-26 10:08:23 -04:00
a static file server.
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
Use the `--inspect-brk` flag to break execution on the first line:
2020-05-13 11:55:44 -04:00
```shell
$ deno run --inspect-brk --allow-read --allow-net https://deno.land/std@v0.50.0/http/file_server.ts
2020-05-13 11:55:44 -04:00
Debugger listening on ws://127.0.0.1:9229/ws/1e82c406-85a9-44ab-86b6-7341583480b1
Download https://deno.land/std@v0.50.0/http/file_server.ts
Compile https://deno.land/std@v0.50.0/http/file_server.ts
...
```
Open `chrome://inspect` and click `Inspect` next to target:
![chrome://inspect](../images/debugger1.jpg)
It might take a few seconds after opening the devtools to load all modules.
![Devtools opened](../images/debugger2.jpg)
You might notice that Devtools paused execution on the first line of
2020-06-10 06:48:07 -04:00
`_constants.ts` instead of `file_server.ts`. This is expected behavior and is
2020-05-13 11:55:44 -04:00
caused by the way ES modules are evaluated by V8 (`_constants.ts` is left-most,
bottom-most dependency of `file_server.ts` so it is evaluated first).
At this point all source code is available in the Devtools, so let's open up
`file_server.ts` and add a breakpoint there; go to "Sources" pane and expand the
tree:
![Open file_server.ts](../images/debugger3.jpg)
2020-05-14 03:03:55 -04:00
_Looking closely you'll find duplicate entries for each file; one written
2020-06-10 06:48:07 -04:00
regularly and one in italics. The former is compiled source file (so in the case
of `.ts` files it will be emitted JavaScript source), while the latter is a
source map for the file._
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
Next, add a breakpoint in the `listenAndServe` method:
2020-05-13 11:55:44 -04:00
![Break in file_server.ts](../images/debugger4.jpg)
2020-06-10 06:48:07 -04:00
As soon as we've added the breakpoint Devtools automatically opened up the
source map file, which allows us step through the actual source code that
includes types.
2020-05-13 11:55:44 -04:00
2020-05-21 07:05:25 -04:00
Now that we have our breakpoints set, we can resume the execution of our script
so that we might inspect an incoming request. Hit the Resume script execution
button to do so. You might even need to hit it twice!
Once our script is running again, let's send a request and inspect it in
Devtools:
2020-05-13 11:55:44 -04:00
```
$ curl http://0.0.0.0:4500/
```
![Break in request handling](../images/debugger5.jpg)
2020-06-10 06:48:07 -04:00
At this point we can introspect the contents of the request and go step-by-step
to debug the code.
2020-05-13 11:55:44 -04:00
### VSCode
Deno can be debugged using VSCode.
2020-06-10 06:48:07 -04:00
Official support via the plugin is being worked on -
2020-05-13 11:55:44 -04:00
https://github.com/denoland/vscode_deno/issues/12
2020-06-10 06:48:07 -04:00
We can still attach the debugger by manually providing a
[`launch.json`](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations)
config:
2020-05-13 11:55:44 -04:00
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
2020-06-10 06:48:07 -04:00
"runtimeArgs": ["run", "--inspect-brk", "-A", "${file}"],
2020-05-13 11:55:44 -04:00
"port": 9229
}
]
}
```
2020-06-10 06:48:07 -04:00
**NOTE**: This uses the file you have open as the entry point; replace `${file}`
with a script name if you want a fixed entry point.
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
Let's try out debugging a local source file. Create `server.ts`:
2020-05-13 11:55:44 -04:00
```ts
import { serve } from "https://deno.land/std@v0.50.0/http/server.ts";
2020-05-26 10:09:47 -04:00
const server = serve({ port: 8000 });
2020-05-13 11:55:44 -04:00
console.log("http://localhost:8000/");
2020-05-26 10:09:47 -04:00
for await (const req of server) {
2020-05-13 11:55:44 -04:00
req.respond({ body: "Hello World\n" });
}
```
2020-06-10 06:48:07 -04:00
Then we can set a breakpoint, and run the created configuration:
2020-05-13 11:55:44 -04:00
![VSCode debugger](../images/debugger7.jpg)
### JetBrains IDEs
You can debug Deno using your JetBrains IDE by right-clicking the file you want
to debug and selecting the `Debug 'Deno: <file name>'` option. This will create
2020-06-10 06:48:07 -04:00
a run/debug configuration with no permission flags set. To configure these flags
edit the run/debug configuration and modify the `Arguments` field with the
required flags.
2020-05-13 11:55:44 -04:00
### Other
2020-06-10 06:48:07 -04:00
Any client that implements the Devtools protocol should be able to connect to a
Deno process.
2020-05-13 11:55:44 -04:00
### Limitations
2020-06-10 06:48:07 -04:00
Devtools support is still immature. There is some functionality that is known to
be missing or buggy:
2020-05-13 11:55:44 -04:00
2020-06-10 06:48:07 -04:00
- autocomplete in Devtools' console causes the Deno process to exit
2020-05-13 11:55:44 -04:00
- profiling and memory dumps might not work correctly