【问题标题】:Could not find the preLaunch task 'build'找不到启动前任务“构建”
【发布时间】:2016-03-13 20:47:11
【问题描述】:

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

Debugging C# on OSX with Visual Studio Code

当我尝试调试示例 C# 脚本时,Visual Studio Code 报告了这个错误:

找不到启动前任务“构建”

因此,我无法检查脚本中定义的变量。

这是 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


【解决方案1】:

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

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

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

【讨论】:

  • 如果您在项目根目录的子目录中创建应用程序并且 args 明确定义 app.csproj 文件的直接路径,您也会看到此错误,在这种情况下您将不得不更改与该文件所在位置匹配的路径。只需遵循默认设置,您应该不会遇到此问题,但请注意,如果您必须在子目录中设置 .net 核心应用程序,以防您处理不同语言的多个项目
  • 我还必须更新 launch.json 并设置正确的路径而不是 "program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll"
  • 如果您作为初学者发现很难使用所有关键字和占位符内容自定义预启动任务,您可以从.json 文件中删除该元素并保存它。在调试可执行文件之前运行任何预启动任务。也就是说,确实需要一些时间来了解 VSCode 在线文档中的所有工作原理。
  • 谢谢。对于那些使用 VS 项目并尝试使用 VSC 加载它的人,您可能仍然会因为 VS 版本错误而遇到问题。我使用 Microsoft 链接从 VSC 构建了一个 C# 项目:docs.microsoft.com/en-us/dotnet/core/tutorials/…
  • 关于 OzzyTheGiant 的评论:如果您的项目在子目录中,有一个来自 Germán here 的简单示例说明如何修复它。
【解决方案2】:

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

对我来说,@Jeferson Tenorio 的工作原理是什么,但它需要更多步骤,所以让我们添加它们:

  1. 单击配置任务:
  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"

    我希望这会有所帮助。

【讨论】:

    【解决方案3】:

    如上述答案中所建议的,您需要launch.json 文件中定义启动项,而不是在blah.code-workspace 文件中。

    后者不读取tasks.json 中定义的任务,而只读取同一个 .code-workspace 文件中定义的任务。

    错误报告:

    【讨论】:

      【解决方案4】:

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

      launch.json 文件的preLaunchTask 属性定义了在脚本启动之前应该执行的任务。从问题来看,Visual Studio Code 已配置为在启动脚本之前运行任务build

      preLaunchTask: 'build'
      

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

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

      【讨论】:

      • 这没有帮助 - 我的默认 tasks.json 文件中没有 exe 任务
      • 执行dotnet new webapi 后我什至没有tasks.json 文件。
      • @rbraun 我在默认的 tasks.json 中也没有 exe 任务,所以我从 launch.json 中删除了它,现在它可以工作了。
      • 请注意,taskName 已被弃用,tasks.json 应使用 label 来命名任务。然后,launch.json 可以通过该标签引用任务。
      【解决方案5】:

      在 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”而不是“dotnet”,参数为“build”。如果不进行更改,您将收到带有退出代码 127 的“dotnet build: command not found”

      【讨论】:

        【解决方案6】:

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

        (我原以为它应该自己解决这个问题,但显然目前还没有 - 但确实(对我而言)将调试配置提交到项目基础而不是全局配置的优势)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-10
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-27
          相关资源
          最近更新 更多