您好,我正在学习本教程 https://code.visualstudio.com/docs/typescript/typescript-tutorial 调试打字稿,但我遇到了错误,如屏幕截图所示。 如果我仍然选择调试,调试器可以工作,但我无法设置任何断点。我怀疑这与未能设置任务文件有关。 有什么建议吗?
我遇到了类似的问题。在我的例子中,tsconfig.json 文件不在主文件夹中,而是在子文件夹中。在我的例子中,工作配置看起来像这样
{
"type": "node",
"request": "launch",
"name": "Launch server",
"program": "${workspaceFolder}/server/index.ts",
"preLaunchTask": "tsc: build - server/tsconfig.json",
"outFiles": ["${workspaceFolder}/out/**/*.js"]
}
任务
tsc: build - tsconfig.json
默认来自VSCode,当它检测到tsconfig.json
的存在时。根据您的屏幕截图,我可以看出您已经拥有该文件。所以,检测不到才怪
请确保
tsconfig.json
文件内容有效。
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "out",
"sourceMap": true
}
}
还要检查任务是否存在,您可以选择菜单Terminal -> Run Build Task 或在 MacOS 上按Shift + Command + B。如果正确,您可以看到有两个任务可用,如下图所示。
不然肯定是步骤有问题。也许,在
preLaunchTask
中有一个额外的空间。作为参考,我也在这里复制粘贴我的launch.json
。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/helloworld.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}
对于其他语言的用户,
tsc: build
命令可能是另一个命令,例如中文中的tsc: 构建
。
我的 launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.ts",
"preLaunchTask": "tsc: 构建 - tsconfig.json",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}
当你Ctrl+Shift+B终端运行时
Run Build Task..
你会在终端看到
">执行任务:tsc -p c:....
终端将被任务重复使用,按任意键关闭它。”
然后双击一个 ts 文件并按 F5,您将必须选择您希望 ts 文件运行的环境,然后您将在调试控制台中看到输出。
因为它没有找到 tsconfig 文件的路径.... 查看您的文件夹结构该结构是否包含多个具有相同名称的相同文件夹...所以调试器混淆了查找路径...因此请确保您使用的开发文件夹具有正确的路径和唯一名称与其父名称不相同文件夹并包含 tsconfig 文件...
如果 Extension Host 崩溃,也会发生这种情况。这会阻止任务引擎找到请求的任务。通常,您应该会在烤面包机中看到一条消息,您可以在其中立即重启Extension Host。之后调试器和任务工作。
对于 Ubuntu Linux 20.04 LTS(但在其他操作系统上可能也一样)让 preLaunchTask 为我工作的是同时使用本地 tasks.json 和 launch.json
所以我的文件夹结构(预构建)是:
.vscode/launch.json
.vscode/tasks.json
dist
src/index.ts
package.json
tsconfig.json
我的 launch.json 包含:
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "TS preLaunchTask-build",
"program": "${file}",
"preLaunchTask": "tsc: build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"skipFiles": [
"<node_internals>/**", "node_modules",
]
},
]
}
我的 tasks.json 包含:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"command": "echo hello yes working!",
"problemMatcher": [],
"label": "myTask"
},
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": ["$tsc"],
"group": "build",
"label": "tsc: build"
},
]
}
我的 tsconfig.json 包含:
{
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"target": "es5",
"module": "commonjs"
},
"include": [
"src/**/*"
]
}
用法:在 index.ts 中按 F5 设置断点
我还包含了另一个任务“myTask”,您可以将 launch.json 中的 preLaunchTask 行更改为:
"preLaunchTask": "myTask",
(它将向控制台输出一些文本以验证 preLaunchTask 正在运行)
这样,如果您仍然有问题,您可以查看问题是在您的 tsconfig 设置中,还是 preTaskLaunch 设置问题。
(我原以为它应该自己解决这个问题,但显然不是在当前编写的时候 - 但确实(对我而言)强制将调试配置提交到基于项目的回购而不是全局配置)
在工作区的 .vscode 中创建 launch.json 并在那里设置,而不是使用 vscode 用户配置文件设置。 我不知道为什么:(
就我而言,我能够通过在 package.json 中添加构建脚本来修复它
“脚本”:{ “构建”:“tsc --build”
然后从 launch.json 中我将 tsc:build 替换为 npm: build.