Could not find the preLaunch task 'build'

79,961

Solution 1

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

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

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

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

Share:
79,961
gnerkus
Author by

gnerkus

dev

Updated on July 05, 2022

Comments

  • gnerkus
    gnerkus almost 2 years

    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?