70

To configure Visual Studio Code to debug C# scripts on OSX, I followed through all the steps listed in the article below:

Debugging C# on OSX with Visual Studio Code

When I tried to debug the sample C# script, Visual Studio Code reported this error:

Could not find the preLaunch task 'build'

As a consequence, I could not inspect the variables defined in the script.

This is a copy of the launch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch console application",
            "type": "mono",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/Program.exe",
            "args": [],
            "cwd": "${workspaceRoot}",
            "stopAtEntry": false
        }
    ]
}

This is a copy of the tasks.json file:

{
    "version": "0.1.0",
    "command": "mcs",
    "args": [
        "-debug",
        "Program.cs"
    ],  
    "showOutput": "silent",
    "taskSelector": "/t:",
    "tasks": [
        {
            "taskName": "exe",
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"
        }
    ]
}

How do I resolve this?

gnerkus
  • 11,357
  • 6
  • 47
  • 71
  • [more reasons for this error message](https://stackoverflow.com/questions/43627751/visual-studio-code-could-not-find-the-prelaunchtask-build/68102888#68102888) – papo Jun 23 '21 at 15:33

6 Answers6

80

You can use the Visual Studio Code to solve it.

When you see the error message, click on the steps below error sample

  1. Configure Task
  2. Create tasks.json file from template
  3. NET Core Executes .NET Core build commands

The VSCode will create a file like it:

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

It's finished. The VSCode will build the project before run.

Jeferson Tenorio
  • 2,030
  • 25
  • 31
  • 1
    You will also see this error if you create the application in a subdirectory of your project's root directory and you args explicitly define the direct path to your app.csproj file, in which case you will have to change the path to match to where that file is. Just follow the default set up and you shouldn't get this issue, but be aware to do this if you have to set your .net core app in a subdirectory in case you are dealing with multiple projects in different languages – OzzyTheGiant May 29 '19 at 15:46
  • 1
    I had to also update launch.json and set the correct path instead of `"program": "${workspaceFolder}/bin/Debug//.dll"` – isapir Oct 06 '19 at 23:12
  • In case you find it tough as a beginner to customise the prelaunch task with all the keywords and the placeholders stuff, you can just delete that element from the ```.json``` file and save it. Run whatever prelaunch task right before you debug the executable. That said, do take some time to understand how it all works in the VSCode online documentation. –  Oct 30 '20 at 10:03
  • Thank you. For those who used VS project and tried to load it with VSC, you might still have issues because of wrong VS version. I used Microsoft link to build a C# project from VSC: https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code?pivots=dotnet-6-0#create-the-app – E235 Jan 05 '22 at 14:50
  • Regarding OzzyTheGiant's comment: If your project *is* in a subdirectory, there's a simple example from Germán [here](https://stackoverflow.com/questions/43393472/vs-code-debug-project-net-core-project-that-resides-in-a-subfolder) on how to fix it. – Joey Apr 06 '22 at 21:58
22

The error occurs because Visual Studio Code cannot find any task in the tasks.json with the taskName value set to 'build'.

The preLaunchTask property of the launch.json file defines the task that should be executed before the script is launched. From the question, Visual Studio Code has been configured to run the task build before launching the script:

preLaunchTask: 'build'

But there's no task named 'build' in the tasks.json file.

To fix this, you should change the value of the preLaunchTask property to 'exe', which is the build task that has been defined in the tasks.json file.

gnerkus
  • 11,357
  • 6
  • 47
  • 71
10

It seems like this will be different for every scenario.

For me what @Jeferson Tenorio worked, but it needed a few more steps so let's add them:

  1. Click on Configure Task: enter image description here
  2. Create tasks.json file from template
  3. .NET Core Executes .NET Core build commands
  4. Go to your launch.json file, and under configurations/program you will find this:

    ${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll

    Simply replace <insert-target-framework-here> and <insert-project-name-here> with your target framework, in my case that would be netcoreapp2.0 and then your project name (if you haven't changed anything your project name should be the same as the folder where you created your project), it should look something like this:

    "program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/MyProject.dll"

    I hope this helps.

marcos.borunda
  • 1,486
  • 1
  • 17
  • 34
5

As suggested in the above answer, you need to have the launch items defined in launch.json file and not in blah.code-workspace file.

The latter doesn't read tasks defined in tasks.json but only those defined in the same .code-workspace file.

Bug reports:

puio
  • 1,208
  • 6
  • 19
  • This was the solution for me - I had my launch settings configured in settings.json and it didn't work. Separating them out into the launch.json fixed it, thanks! – samneric Dec 17 '22 at 23:34
1

On Linux, to get the build command to work, I needed to change the tasks.json file from:

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

to:

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

the reason for this is the fact that Linux will treat the task generated by VSC as running command "dotnet build" instead of "dotnet" with the parameter of "build". Without the change you will receive "dotnet build: command not found" with exit code 127

Jixster
  • 7
  • 2
0

For Ubuntu Linux 20.04 LTS (but may well be the same on other OS's) what got preLaunchTask working for me, was using both a local tasks.json and launch.json

So my folder structure (pre-build) is:

.vscode/launch.json
.vscode/tasks.json
dist
src/index.ts
package.json
tsconfig.json

My launch.json contains:

{
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "TS preLaunchTask-build",
            "program": "${file}",
            "preLaunchTask": "tsc: build",
            "outFiles": ["${workspaceFolder}/dist/**/*.js"],
            "skipFiles": [
                "<node_internals>/**", "node_modules",
              ]
        },
    ]
}

My tasks.json contains:

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

And my tsconfig.json contains:

{
  "compilerOptions": {
      "outDir": "./dist",
      "sourceMap": true,
      "target": "es5",
      "module": "commonjs"
  },
  "include": [
      "src/**/*"
  ]
}

Usage: whilst within the index.ts just hit F5 with a breakpoint set

I have also included another task, "myTask" you can change the preLaunchTask line in your launch.json to: "preLaunchTask": "myTask", (where it will output some text to console to verify preLaunchTask is now working)

That way, if you still have issues, you can see if the issue is in your tsconfig setup, or if it's a preTaskLaunch setup issue.

(I would have thought it should have resolved this itself, but apparently not at the current time of writing anyway - but does force the advantage (for me) of committing debug config to the repo on project basis rather than global config)

Leigh Mathieson
  • 1,658
  • 2
  • 17
  • 25