visual-studio-codedebugging.net-corebuild

VSCode Build Script Configuration To Build Class Library


How should tasks.json be configured for building a dotnet core solution for debugging in VSCode when the entry project references a class library?

I'm having some trouble building dotnet core solutions in VSCode for debugging when the entry project has a reference to another internal class library. I have the "C# for Visual Studio Code (powered by OmniSharp)" extension installed. I am running on Ubuntu 22.04 with the dotnet 6.0.403 sdk.

While I can write, build, and run programs referencing class libraries from the command line without issue, they fail to build when launched from VS Code's "Run and Debug" section.

I've attempted to work through a basic demo solution starting from scratch:

mkdir DebugCode && cd DebugCode
dotnet new sln
dotnet new console -o DebugCode.Console
dotnet new classlib -o DebugCode.Core
dotnet sln add DebugCode.Console/Debug.Console.csproj
dotnet sln add DebugCode.Core/DebugCode.Core.csproj

I put a simple SockDrawer.cs class in the DebugCode.Core project with the following code:

namespace DebugCode.Core;

public class SockDrawer
{
    private IList<string> Socks { get; set; } = new List<string>();

    public SockDrawer()
    {
        FillDrawer();
    }

    public void FillDrawer()
    {
        Socks.Add("Godzilla socks");
        Socks.Add("Sasquatch socks");
        Socks.Add("Blue Lightning socks");
        Socks.Add("boring work socks");
    }

    public void ListSocks()
    {
        foreach(var socks in Socks)
        {
            Console.Write($"{socks}, ");
        }
        Console.Write(Environment.NewLine);
    }

}

And then I reference that in Program.cs of the DebugCode.Console project.

using DebugCode.Core;

var sockdrawer = new SockDrawer();
sockdrawer.ListSocks();

I can build and execute the program from the root DebugCode directory with no issue

dotnet run --project DebugCode.Console

The trouble starts when I try to launch the build for debugging. The build fails when launching and building from the "Run and Debug" section. Using the default extension generated launch.json and tasks.json files with the content:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.dll",
            "args": [],
            "cwd": "${workspaceFolder}/DebugCode.Console",
            "console": "integratedTerminal", // I did manually update this line
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/DebugCode.Console/DebugCode.Console.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "publish",
            "command": "dotnet",
            "type": "process",
            "args": [
                "publish",
                "${workspaceFolder}/DebugCode.Console/DebugCode.Console.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "watch",
            "command": "dotnet",
            "type": "process",
            "args": [
                "watch",
                "run",
                "--project",
                "${workspaceFolder}/DebugCode.Console/DebugCode.Console.csproj"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}

I get a file not found error referencing the dependent class library:

Executing task: dotnet build [redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary 

MSBuild version 17.3.2+561848881 for .NET
  Determining projects to restore...
  Restored [redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj (in 62 ms).
  1 of 2 projects are up-to-date for restore.
  DebugCode.Core -> [redacted]/DebugCode/DebugCode.Core/bin/Debug/net6.0/DebugCode.Core.dll
CSC : error CS0006: Metadata file '[redacted]/DebugCode/DebugCode.Core/obj/Debug/net6.0/ref/DebugCode.Core.dll' could not be found [[redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj]

 *  The terminal process "dotnet 'build', '[redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj', '/property:GenerateFullPaths=true', '/consoleloggerparameters:NoSummary'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

Based on this error, I've tried updating the build task to build the entire solution instead of just the console project.

{
    "label": "build",
    "command": "dotnet",
    "type": "process",
    "args": [
        "build",
        "${workspaceFolder}/DebugCode.sln",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
    ],
    "problemMatcher": "$msCompile"
}

In this case, the build succeeds but debugging fails due to a missing DebugCode.Console.deps.json file:

[redacted].vscode/extensions/ms-dotnettools.csharp-1.25.2-linux-x64/.debugger/vsdbg --interpreter=vscode --connection=/tmp/CoreFxPipe_vsdbg-ui-e0801ed66b6a4fb8aa6f0c54a5fc2cc3 
Cannot use file stream for [[redacted]/DebugCode/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.deps.json]: No such file or directory
A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '[redacted]/DebugCode.Console/bin/Debug/net6.0/'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because '[redacted]/DebugCode/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the '[redacted]/DebugCode/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.runtimeconfig.json' file and specify the appropriate framework.
 

Interestingly, if I build the solution myself manually using the dotnet build DebugCode.sln command from my DebugCode directory, the above referenced DebugCode.Console.deps.json file is generated. Then, when I comment out the "preLaunchTask": "build" line of launch.json, the debugger successfully launches from the "Run and Debug" section. I figure I must have something wrong in the 'build' task in tasks.json, but I'm failing to recognize the difference between the dotnet build command I'm using manually and what tasks.json needs.


Solution

  • I got the preLaunchTask to build and launch the solution successfully in the debugger by removing ${workspaceFolder} from the path to the solution file.

    {
        "label": "build",
        "command": "dotnet",
        "type": "process",
        "args": [
            "build",
            "DebugCode.sln", // removed ${workspaceFolder}/
            "/property:GenerateFullPaths=true",
            "/consoleloggerparameters:NoSummary"
        ],
        "problemMatcher": "$msCompile"
    }
    

    I don't exactly understand why that works, but at least I'm building and debugging.

    Below is the entire tasks.json file...(to provide overall context/syntax)

    
    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build",
                "command": "dotnet",
                "type": "process",
                "args": [
                    "build",
                    "DebugCode.sln", // removed ${workspaceFolder}/
                    "/property:GenerateFullPaths=true",
                    "/consoleloggerparameters:NoSummary"
                ],
                "problemMatcher": "$msCompile"
            }
        ]
    }