Could not find the preLaunch task 'build'

C#Visual StudioVisual Studio-Code

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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

Solution 5 - C#

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:

Solution 6 - C#

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongnerkusView Question on Stackoverflow
Solution 1 - C#Jeferson TenorioView Answer on Stackoverflow
Solution 2 - C#gnerkusView Answer on Stackoverflow
Solution 3 - C#marcos.borundaView Answer on Stackoverflow
Solution 4 - C#JixsterView Answer on Stackoverflow
Solution 5 - C#puioView Answer on Stackoverflow
Solution 6 - C#Leigh MathiesonView Answer on Stackoverflow