要配置 Visual Studio Code 以在 OSX 上调试 C# 脚本,我执行了下面文章中列出的所有步骤:
使用 Visual Studio Code 在 OSX 上调试 C#
当我尝试调试示例 C# 脚本时,Visual Studio Code 报告了此错误:
找不到预启动任务“build”
因此,我无法检查脚本中定义的变量。
这是 launch.json 文件的副本:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch console application",
"type": "mono",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/Program.exe",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false
}
]
}
这是 tasks.json 文件的副本:
{
"version": "0.1.0",
"command": "mcs",
"args": [
"-debug",
"Program.cs"
],
"showOutput": "silent",
"taskSelector": "/t:",
"tasks": [
{
"taskName": "exe",
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
我该如何解决这个问题?
您可以使用Visual Studio Code来解决。
VSCode 将创建一个类似的文件:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
完结了。 VSCode 将在运行之前构建项目。
发生错误的原因是 Visual Studio Code 在 tasks.json 中找不到任何
taskName
值设置为 'build'
的任务。
launch.json
文件的
preLaunchTask
属性定义了在启动脚本之前应执行的任务。根据问题,Visual Studio Code 已配置为在启动脚本之前运行任务 build
:
preLaunchTask: 'build'
但是
tasks.json
文件中没有名为
'build'
的任务。
要解决此问题,您应该将 preLaunchTask
属性的值更改为
'exe'
,这是已在tasks.json 文件中定义的构建任务。
对我来说@Jeferson Tenorio 有效,但它需要更多步骤,所以让我们添加它们:
launch.json
文件,在配置/程序下您将找到:
${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll
<insert-target-framework-here>
和
<insert-project-name-here>
替换为您的目标框架,在我的例子中,这将是
netcoreapp2.0
,然后是您的项目名称(如果您没有更改任何内容,您的项目名称应与您创建的文件夹相同)你的项目),它应该看起来像这样:
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/MyProject.dll"
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"args": [
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
至:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build"
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
原因是 Linux 会将 VSC 生成的任务视为运行命令“dotnet build”,而不是带有“build”参数的“dotnet”。 如果不进行更改,您将收到“dotnet build:未找到命令”,退出代码为 127
所以我的文件夹结构(预构建)是:
.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 设置问题。
(我本以为它应该自己解决这个问题,但显然在当前撰写本文时还没有解决 - 但确实强制(对我来说)基于项目而不是全局配置将调试配置提交到存储库的优势)
经过一番尝试和错误,我最终得出以下结论。我发布了 watch
和 构建的任务,因为 watch 任务与构建非常相似,如果您已经正确配置了该任务,那么您将拥有手动编写构建任务所需的大部分内容;
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/Mysite.sln"
],
"problemMatcher": "$msCompile"
},
{
"type": "dotnet",
"task": "watch Mysite.Web",
"file": "C:\\Users\\whoamI\\source\\mysite\\Mysite.Web\\Mysite.Web.csproj",
"problemMatcher": [],
"label": "dotnet: watch Mysite.Web"
},
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Mysite.sln"
],
"problemMatcher": "$msCompile"
}
Windows 文件系统,所以 \\
而不是
/
。