【问题标题】:Visual Studio Code breakpoints in typescript打字稿中的 Visual Studio Code 断点
【发布时间】:2017-01-10 00:00:43
【问题描述】:

使用 Visual Studio Code,当我在 .ts 文件中设置断点时,它没有被命中。 (并且在我调试时不是红色的)。

但是,如果我在编译后的 JS 中设置断点,那么它会在我逐步执行代码时将我切换到 ts 文件。然后我的 .ts 文件中的所有断点开始工作。

有没有办法让 .ts 文件断点正常工作(无需先在我编译的 .js 文件中设置一个?

注意:我查看了这个问题:Setting breakpoints in Typescript Jasmine tests with Visual Studio Code,这正是我的问题。然而,这个问题的答案是升级到 VS Code 0.10.9 和 Typescript 1.8.2。我正在使用 VS Code 1.8.1 和 Typescript 2.1.4。

【问题讨论】:

  • 您是否在 tsconfig.json 上启用了sourceMap
  • @BrunoLM - 是的。他们似乎在某种程度上发挥了作用。否则我放置 JavaScript 文件的断点将无法将我换回 Typescript 文件。
  • 我想我遇到了同样的问题。我建议创建一个简单的项目并尝试复制您的问题。仅供参考,您可以将' trace: "all" ' option 添加到您的启动配置中(它在文档中,但没有出现在智能感知中),这对我有一点帮助。我的问题似乎是 vsc 中有关子目录中 TS 文件的错误。我为此提出了一个问题here
  • 经过进一步调查,对我来说问题出在 gulp 生成的源映射中。很多人有类似的问题as mentioned here

标签: debugging typescript visual-studio-code breakpoints source-maps


【解决方案1】:

如果不了解设置的特殊性,就很难确切知道需要更改什么。也就是说,这里有一些想法给你and a demo project. 还有一个很好的对话in this GitHub issue. 以下是在我的机器上工作的最小 TypeScript 项目设置。

.vscode/launch.json 设置programprelaunchTaskoutFilessourceMaps 属性。

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceRoot}/index.ts",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "tsc",
            "outFiles": [
                "${workspaceRoot}/**/*.js"
            ],
            "sourceMaps": true
        }
    ]
}

.vscode/settings.json 告诉 Visual Studio Code 使用我们在 node_modules 中安装的 TypeScript 版本,而不是使用全局安装的 TypeScript。

{
    "typescript.tsdk": "./node_modules/typescript/lib"
}

.vscode/tasks.json 定义tsc -p . shell 命令,也就是我们在launch.json中定义的prelaunchTask

{
    "command": "tsc",
    "isShellCommand": true,
    "args": ["-p", "."]
}

index.ts

console.log('foo');
console.log('bar'); // a breakpoint here gets hit.

package.json 在本地安装 TypeScript,让我们更好地控制我们使用的版本。

{
  "devDependencies": {
    "typescript": "^2.1.4"
  }
}

tsconfig.json告诉编译器生成源映射。

{
    "compilerOptions": {
        "target": "es5",
        "sourceMap": true
    }
}

【讨论】:

  • isShellCommand 已弃用。没有帮助我。同一函数中的断点会被命中,但更深的断点不会被命中。
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 2015-08-04
  • 1970-01-01
  • 2018-03-10
  • 2017-05-21
  • 2018-09-08
  • 2021-07-12
  • 2019-04-27
相关资源
最近更新 更多