Visual Studio Code: Could not find the preLaunchTask 'build'?

人走茶凉 提交于 2019-12-22 01:35:04

问题


I have created a new .NET Core application with the command:

dotnet new console -o test

When I try to run it in the Visual Studio Code debugger, I get:

Could not find the preLaunchTask 'build'?

Visual Studio Code generated these files for me:

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"
        }
    ]
}

and

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}"
        }
    ]
}

My problem looks similar to this one, but in my case there are no mismatches between the names in launch.json and tasks.json for the preLaunchTask so the answer does not apply in this case. I'm running Visual Studio Code version 1.11.2 and .NET Core 1.1 (latest versions as of when this post was created).

I have tried the same on both a Windows machine and a Mac with the same problem. If I do the command "dotnet restore" and "dotnet run", the code runs with no problems, but I still get the same error: "Could not find the preLaunchTask 'build'"


回答1:


For me, it works to restart VS Code after tasks.json and/or launch.json files creation.

Also note, that you need to update "program" settings in launch.json with the path to dlls.




回答2:


Change tasks.json as below.

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"
        }
    ]
}



回答3:


This got addressed in master already and will be available in the next release. Reopening VS Code on the folder should help workaround the problem.




回答4:


Just a word of caution-

If workspaceFolder is not at the same level as e.g. the app folder tasks.json will not be created and you'll get all of the above errors. I created a subfolder after opening the project and got all the above errors - all fixed after running debug from the correct folder - This could explain the effect of relaunching VS code.



来源:https://stackoverflow.com/questions/43627751/visual-studio-code-could-not-find-the-prelaunchtask-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!