找不到预启动任务“build”

问题描述 投票:0回答:7

要配置 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"
        }
    ]
}

我该如何解决这个问题?

c# visual-studio visual-studio-code
7个回答
87
投票

您可以使用Visual Studio Code来解决。

当您看到错误消息时,请单击以下步骤 error sample

  1. 配置任务
  2. 从模板创建tasks.json文件
  3. NET Core 执行 .NET Core 构建命令

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 将在运行之前构建项目。


23
投票

发生错误的原因是 Visual Studio Code 在 tasks.json 中找不到任何

taskName
值设置为
'build'
的任务。

launch.json

 文件的 
preLaunchTask 属性定义了在启动脚本之前应执行的任务。根据问题,Visual Studio Code 已配置为在启动脚本之前运行任务 build

preLaunchTask: 'build'

但是

tasks.json

 文件中没有名为 
'build' 的任务。

要解决此问题,您应该将

preLaunchTask

 属性的值更改为 
'exe'
,这是已在 
tasks.json 文件中定义的构建任务。


10
投票
似乎每个场景都会有所不同。

对我来说@Jeferson Tenorio 有效,但它需要更多步骤,所以让我们添加它们:

    单击配置任务:
  1. enter image description here
  2. 从模板创建tasks.json文件
  3. .NET Core 执行 .NET Core 构建命令
  4. 转到您的

    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"

    
    
    

    我希望这有帮助。


5
投票
  • Visual Studio 代码:找不到 preLaunchTask 'build'?
正如上面答案中所建议的,您

需要launch.json

文件中定义启动项,而不是在
blah.code-workspace
文件中。

后者不读取

tasks.json

 中定义的任务,而仅读取同一 .code-workspace 文件中定义的任务。

错误报告:

  • https://github.com/microsoft/vscode/issues/136187
  • https://github.com/microsoft/vscode/issues/93149

1
投票
在 Linux 上,为了让构建命令正常工作,我需要更改tasks.json 文件:

{ // 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


0
投票
对于 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 设置问题。

(我本以为它应该自己解决这个问题,但显然在当前撰写本文时还没有解决 - 但确实强制(对我来说)基于项目而不是全局配置将调试配置提交到存储库的优势)


0
投票
我无法让 VSCode 正确生成tasks.json 中的任务,我继续收到“无法找到构建任务”错误。

经过一番尝试和错误,我最终得出以下结论。我发布了 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 文件系统,所以 

\\

 而不是 
/

© www.soinside.com 2019 - 2024. All rights reserved.