我有一个简单的
path/to/test.ts
脚本:
console.log(Deno.cwd())
它与
deno run --allow-read .\test.ts
运行良好,并按预期返回 path/to
。但是,使用调试器在退出之前不会返回任何内容。
这是为什么呢?以下是
launch.json
的内容:
{
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"name": "Deno",
"type": "node",
"program": "${file}",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": [
"run",
"--allow-read"
],
"attachSimplePort": 9229
}
]
}
我尝试为
${fileDirname}
键设置 ${workspaceFolder}
、${cwd}
和 cwd
值,但没有帮助。
调试器是一个独立的进程,拥有自己的 I/O 流(例如控制台)。
调试程序时,发送到程序的 I/O 流之一(例如,通过
stdout
的 console.log
)的任何数据都不会转发到调试器的控制台,直到调试器连接到程序并创建一个管道那条溪流。该连接过程需要时间,而且几乎肯定要等到您展示的示例程序中的单个 console.log
语句完成之后才会发生。
Deno 的
run
命令支持延迟执行的参数,直到调试器连接后(在 denoland/deno#17001 中添加)——这个参数是 --inspect-wait
。
来自手册:
其他运行时标志
更多影响执行环境的标志。
(snip) --inspect-wait=<HOST:PORT> activate inspector on host:port and wait for ... (snip)
将其包含在
configurations.runtimeArgs
中的 args 数组中 ./.vscode/launch.json
:
{
"configurations": [
{
"runtimeArgs": [
"run",
"--inspect-wait", // include this argument for debugging
"--allow-read"
]
}
]
}
添加该参数后,我可以使用调试器在 VS Code 工作区中运行示例复制文件,并且我在调试器控制台中看到了这一点:
/Users/deno/.deno/bin/deno run --inspect-wait --allow-read ./test.ts
/Users/deno/so-76891244 test.ts:1
以下是我用来重现该问题的软件版本:
so-76891244 % code --version
1.81.1
6c3e3dba23e8fadc360aed75ce363ba185c49794
arm64
so-76891244 % deno --version
deno 1.36.1 (release, aarch64-apple-darwin)
v8 11.6.189.12
typescript 5.1.6