debugging 当入口项目引用类库时,应如何配置tasks.json以生成用于在VSCode中进行调试的. net核心解决方案

kuhbmx9i  于 2022-12-19  发布在  Vscode
关注(0)|答案(1)|浏览(176)

当入门项目引用了另一个内部类库时,我在VSCode中构建dotnet核心解决方案进行调试时遇到了一些问题。我安装了“C# for Visual Studio Code(由OmniSharp提供支持)”扩展。我在Ubuntu 22.04上运行dotnet 6.0.403 sdk。
虽然我可以毫无问题地从命令行编写、构建和运行引用类库的程序,但当从VS代码的“运行和调试”部分启动时,它们无法构建。
我尝试从头开始完成一个基本的演示解决方案:

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

我在DebugCode.Core项目中放置了一个简单的SockDrawer.cs类,代码如下:

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);
    }

}

然后我在DebugCode.Console项目的Program.cs中引用了它。

using DebugCode.Core;

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

我可以从根DebugCode目录构建并执行程序,没有任何问题

dotnet run --project DebugCode.Console

当我尝试启动构建进行调试时,问题开始出现。从“运行和调试”部分启动和构建时,构建失败。使用默认扩展名生成launch.jsontasks.json文件,其内容为:
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"
        }
    ]
}

引用依赖类库时出现找不到文件错误:

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.

基于此错误,我尝试更新生成任务以生成整个解决方案,而不仅仅是控制台项目。

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

在这种情况下,生成成功,但调试失败,因为缺少DebugCode.Console.deps.json文件:

[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.

有趣的是,如果我自己使用DebugCode目录中的dotnet build DebugCode.sln命令手动构建解决方案,则会生成上面引用的DebugCode.Console.deps.json文件。然后,当我注解掉launch.json的"preLaunchTask": "build"行时,调试器会从“Run and Debug”部分成功启动。我想我一定是在tasks.json中的“build”任务中出错了。但是我没有意识到手动使用的dotnet build命令与tasks.json需要的命令之间的区别。

piv4azn7

piv4azn71#

通过从解决方案文件的路径中删除${workspaceFolder},我获得了preLaunchTask以在调试器中成功构建和启动解决方案。

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

我不太明白这是为什么,但至少我在构建和调试。

相关问题