Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code cannot build the default dotnet core class library

I cannot get VS Code to build an empty class library while dotnet core can quite happily.

In PowerShell I create a folder called CoreTesting, navigate into it and launch VS Code with code.

I hit CTRL + ` to enter the terminal and navigate into the solution's folder.

I then enter dotnet new classlib --name Common, see the new folder Common and enter dotnet build .\Common\ to build the class library. All is well.

I add the Common folder to VS Code and hit CTRL + SHIFT + B and see No build task to run found. Configure Build Task..., so I hit return and see Create tasks.json file from template, so I hit return again and see:

  • MSBuild
  • maven
  • .NET Core
  • Others

So I select .NET Core and see that a .vscode folder is created containing tasks.json. This file contains:

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

I hit CTRL + SHIFT + B again and see the option build Common, so I hit return and see this:

> Executing task in folder Common: dotnet build <

Microsoft (R) Build Engine version 16.0.225-preview+g5ebeba52a1 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

The structure I can see is this:

  • Common\
    • .vscode\
      • tasks.json
    • bin\
    • obj\
    • Class1.cs
    • Common.csproj

What have I done wrong?

like image 897
Matt W Avatar asked Jun 17 '26 13:06

Matt W


1 Answers

I was able to reproduce your problem on v1.41.1 for Windows. In doing so it created this tasks.json which is similar to yours...

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

When you invoke a task, it defaults to using the workspace folder (CoreTesting) path as the working directory. However, the project file is a directory beneath in Common, hence the error The current working directory does not contain a project or solution file.

A quick fix for this is to simply open the directory with the project file as the workspace folder (i.e. FileOpen Folder... → Select the Common directory).

Alternatively, if that solution is undesirable then with CoreTesting opened as the workspace folder you can configure the task to execute with a different working directory. To do this, set the task's options.cwd property...

{
    /* snip */
    "tasks": [
        {
            /* snip */
            "problemMatcher": "$msCompile",
            "options": {
                "cwd": "${workspaceFolder}/Common"
            }
        }
    ]
}

I found this property in the Schema for tasks.json, and it's also mentioned in the Custom tasks section of the Tasks documentation. After making either change above the library builds successfully for me.

like image 128
Lance U. Matthews Avatar answered Jun 19 '26 06:06

Lance U. Matthews



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!