20

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'"

Gama11
  • 31,714
  • 9
  • 78
  • 100
OlavT
  • 2,496
  • 4
  • 31
  • 56

6 Answers6

21

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.

doomleika
  • 153
  • 2
  • 10
Set
  • 47,577
  • 22
  • 132
  • 150
  • Thanks restarting VS Code got me past the problem. But, now it complains about problems like: "Predefined type 'System.Object' is not defined or imported..." Shouldn't VS Code also prompt for a "dotnet restore" to be performed from VS Code? I think I have seen that before. – OlavT Apr 26 '17 at 15:12
  • In general, it should. Not sure how right now VS code decides when to show that prompt. Anyway, you always may manually do `dotnet restore` from the terminal inside VS Code. – Set Apr 26 '17 at 15:17
  • I also can't recall that I had to manually set the path to the program to debug in the launch.json file. Why can't VS Code do the tricks behind the scene? It should know where to find the executable without involving the user? – OlavT Apr 26 '17 at 15:32
  • @OlavT this is the [omnisharp-vscode](https://github.com/OmniSharp/omnisharp-vscode) responsibility and the team is going to provide such feature in future. You may look into this [issue](https://github.com/OmniSharp/omnisharp-vscode/issues/1167) if interesting – Set Apr 26 '17 at 16:48
8

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"
        }
    ]
}
Ali Dehqan
  • 461
  • 1
  • 8
  • 18
4

Yet another reason for this error could be, that the used launch configuration is defined inside my_project.code-workspace file (not in launch.json file).
In such case the tasks.json is not used, but tasks must be defined inside the my_project.code-workspace file.

This might be a "feature", but it behaves as a bug, because:

  • tasks doc says: Workspace or folder specific tasks are configured from the tasks.json file in the .vscode folder for a workspace.
  • The error "Could not find task 'xxx'." dialog window offers button: Configure Task which then opens or offer to create file ./.vscode/tasks.json
  • There is no mention or visible hint while editing tasks.json file, that it will not be used.
    This in comparison with e.g. editing setting.json, where in such case text is dimmed and on hover a message says: "This setting cannot be applied in this workspace. It will be applied when you open the containing workspace folder directly."

If a debugging session is started using debug config: my_debug_config (workspace), only tasks defined in my_project.code-workspace file are used.
But while still having the same workspace opened, selecting debug config: dir_debug_config (my_dir), which is defined in the launch.json file, will then use tasks from the tasks.json file.

papo
  • 1,606
  • 19
  • 18
  • exactly my issue.. is there a bug report ? – puio Nov 14 '21 at 05:39
  • I did not find one, nor created a new one. [git](https://github.com/microsoft/vscode/issues) has 5k open issues and being misused as a support forum. It seemed pointless to report there. – papo Nov 14 '21 at 11:43
  • 1
    posted another answer on a similar open question with links to some relevant bug reports https://stackoverflow.com/a/69967477/ – puio Nov 14 '21 at 21:57
1

Try to change the paths in tasks.json and launch.json with absolute paths.

For example in launch.json:

"program": "C:/Projects/MyProject/bin/Debug/netcoreapp1.0/XXXX.dll",
"cwd": "C:/Projects/MyProject/"

in tasks.json:

"tasks": [
  {
    "label": "build",
    "command": "dotnet",
    "type": "process",
    "args": [        
      "build",
      "C:/Projects/MyProject/XXXXXX.csproj"
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
pit
  • 11
  • 6
0

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.

Dirk Bäumer
  • 1,183
  • 9
  • 7
  • 4
    I have the latest version and still have the same problem. Do I have to manually create a 'tasks.json' file? One doesn't seem to be created automatically. – melston Jun 18 '17 at 03:00
0

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.

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84