找不到预启动任务'build'。

72
为了在OSX上调试C#脚本,我按照下面文章中列出的所有步骤配置了Visual Studio Code:

Debugging C# on OSX with Visual Studio Code

当我尝试调试样例C#脚本时,Visual Studio Code报告了以下错误:

Could not find the preLaunch task '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"
        }
    ]
}

我该如何解决这个问题?


更多关于这个错误信息的原因,请参见:[https://dev59.com/51cP5IYBdhLWcg3w6-Kp#68102888]。 - papo
6个回答

82

你可以使用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将构建该项目。


1
如果您在项目的根目录的子目录中创建应用程序,并且您明确定义了直接路径到app.csproj文件,则还会看到此错误,此时您必须更改路径以匹配该文件所在的位置。只需按照默认设置进行操作,您就不应该遇到此问题,但是请注意,如果您必须将.NET Core应用程序设置为子目录,则需要这样做,以防您正在处理不同语言的多个项目。 - OzzyTheGiant
1
我还需要更新launch.json并设置正确的路径,而不是使用"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll" - isapir
如果你作为一个初学者觉得自定义预启动任务并使用所有的关键字和占位符很困难,你可以直接从 .json 文件中删除该元素并保存。在你调试可执行文件之前,运行任何预启动任务。话虽如此,请花些时间去了解在 VSCode 的在线文档中是如何工作的。 - user11991978
谢谢。对于那些使用VS项目并尝试在VSC中加载它的人来说,由于错误的VS版本,您可能仍然会遇到问题。我使用了微软链接从VSC构建了一个C#项目:https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code?pivots=dotnet-6-0#create-the-app - E235
关于OzzyTheGiant的评论:如果您的项目子目录中,这里有一个简单的示例来自Germán [here](https://dev59.com/XqDia4cB1Zd3GeqPKuHk),说明如何解决它。 - Joey

22

错误发生的原因是 Visual Studio Code 在 tasks.json 文件中找不到 taskName 值设置为 'build' 的任何任务。

launch.json 文件中的 preLaunchTask 属性定义了脚本启动前应执行的任务。从问题描述中可以得知,在启动脚本之前,Visual Studio Code 已配置运行任务 build

preLaunchTask: 'build'

但在 tasks.json 文件中没有名为 'build' 的任务。

要解决这个问题,您应该将 preLaunchTask 属性的值更改为 'exe',它是在 tasks.json 文件中已定义的构建任务。


5
没什么帮助 - 我的默认tasks.json文件中没有“exe”任务。 - asdfgh
执行 dotnet new webapi 后,我甚至没有一个 tasks.json 文件。 - Bart Read
@rbraun 我也在默认的 tasks.json 中没有 exe 任务,所以我从 launch.json 中将其删除了,现在它可以正常工作了。 - Aneeq Azam Khan
2
请注意,taskName已被弃用,tasks.json应使用label来命名任务。然后,launch.json可以通过该标签引用任务。 - Simon Opelt

10

每个场景都可能不同。

对于我来说,@Jeferson Tenorio的方法可行,但还需要进行几个步骤,因此让我们将它们添加进来:

  1. 单击“配置任务”: enter image description here
  2. 从模板创建tasks.json文件
  3. .NET Core执行.NET Core构建命令
  4. 转到您的launch.json文件,在configurations / program下找到以下内容:

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

这对我来说是解决方案 - 我的启动设置在settings.json中配置,但它没有起作用。将它们分离到launch.json中解决了问题,谢谢! - samneric

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"命令。如果不进行更改,则会收到退出码为127的"dotnet build: command not found"错误提示。

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设置问题。

(我原本认为它应该自己解决这个问题,但显然在撰写本文时不是当前时间 - 但对我来说,它强制优势是按项目而不是全局配置提交调试配置到repo)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接