问题 Visual Studio代码:找不到preLaunchTask'build'?


我使用以下命令创建了一个新的.NET Core应用程序:

dotnet new console -o test

当我尝试在Visual Studio代码调试器中运行它时,我得到:

Could not find the preLaunchTask 'build'?

Visual Studio Code为我生成了这些文件:

tasks.json:
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build",
            "args": [ ],
            "isBuildCommand": true,
            "showOutput": "silent",
            "problemMatcher": "$msCompile"
        }
    ]
}

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
            "args": [],
            "cwd": "${workspaceRoot}",
            "stopAtEntry": false,
            "console": "internalConsole"
        },
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
            "args": [],
            "cwd": "${workspaceRoot}",
            "stopAtEntry": false,
            "launchBrowser": {
                "enabled": true,
                "args": "${auto-detect-url}",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start ${auto-detect-url}"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
            "env": {
                "ASPNETCORE_ENVIRONMENT": "Development"
            },
            "sourceFileMap": {
                "/Views": "${workspaceRoot}/Views"
            }
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"
        }
    ]
}

我的问题看起来很像 这个,但在我的情况下,在launchLaunchTask的launch.json和tasks.json中的名称之间没有不匹配,所以在这种情况下答案不适用。我正在运行Visual Studio Code版本1.11.2和.NET Core 1.1(创建此帖子时的最新版本)。

我在Windows机器和Mac上都尝试过同样的问题。如果我执行命令“dotnet restore”和“dotnet run”,代码运行没有问题,但我仍然得到相同的错误:“找不到preLaunchTask'构建'”


10541
2018-04-26 07:33


起源

任何解决方案@OlavT - Jagadeesh Govindaraj
github.com/Microsoft/vscode/issues/22250 - Jagadeesh Govindaraj
我认为Prelaunch任务只适用于tasks.json中设置为2.0.0的版本。看看jagadeesh提供的样本。 - Jon


答案:


对我来说,它可以在以后重新启动VS Code tasks.json 和/或 launch.json 文件创建。

另请注意,您需要更新 "program" 设置 launch.json 与dlls的路径。


12
2018-04-26 13:21



谢谢重新启动VS Code让我解决了这个问题。但是,现在它抱怨类似的问题:“未定义或导入预定义类型'System.Object'......”VS代码是否也不应该提示从VS代码执行“dotnet恢复”?我想我以前见过这个。 - OlavT
一般来说,它应该。不确定现在VS代码如何决定何时显示该提示。无论如何,你总是可以手动做 dotnet restore 从VS Code内的终端。 - Set
我也记不起我必须在launch.json文件中手动设置要调试的程序的路径。为什么VS Code不能在场景背后耍花招?它应该知道在哪里找到可执行文件而不涉及用户? - OlavT
@OlavT这是 omn​​isharp-vscode 责任和团队将来会提供这样的功能。你可以看看这个 问题 如果有趣 - Set


如下所示更改tasks.json。

tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "command": "",
    "args": [],
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "shell",
            "args": [
                "build"
            ],
            "options": {
                "cwd": "${workspaceRoot}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

2
2018-02-16 04:38





这已在master中得到解决,并将在下一个版本中提供。在文件夹上重新打开VS代码应该有助于解决问题。


0
2018-04-26 13:21



我有最新版本,仍然有同样的问题。我是否必须手动创建'tasks.json'文件?似乎没有自动创建一个。 - melston


请注意 -

如果 workspaceFolder 与...不同的水平app文件夹 tasks.json 将不会创建,您将获得上述所有错误。我在打开项目后创建了一个子文件夹并得到了上述所有错误 - 从正确的文件夹运行调试后都修复了 - 这可以解释重新启动VS代码的效果。


0
2017-08-23 11:53