VSCode构建脚本配置构建类库

问题描述 投票:0回答:1

当入门项目引用类库时,如何配置tasks.json来构建dotnet core解决方案以在VSCode中调试?

当入门项目引用另一个内部类库时,我在 VSCode 中构建用于调试的 dotnet core 解决方案时遇到了一些麻烦。 我安装了“C# for Visual Studio Code(由 OmniSharp 提供支持)”扩展。 我正在使用 dotnet 6.0.403 sdk 在 Ubuntu 22.04 上运行。

虽然我可以毫无问题地从命令行编写、构建和运行引用类库的程序,但从 VS Code 的“运行和调试”部分启动时,它们无法构建。

我尝试从头开始完成基本的演示解决方案:

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

我在

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

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

}

然后我在

Program.cs
项目的
DebugCode.Console
中引用了这一点。

using DebugCode.Core;

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

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

dotnet run --project DebugCode.Console

当我尝试启动构建进行调试时,问题就开始了。 从“运行和调试”部分启动和构建时,构建失败。 使用默认扩展生成

launch.json
tasks.json
文件,内容如下:

启动.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"
        }
    ]
}

任务.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"
行时,调试器成功从“运行和调试”部分启动。 我认为我在
tasks.json
中的“构建”任务中一定有问题,但我无法识别我手动使用的
dotnet build
命令与
tasks.json
需要的命令之间的区别。

visual-studio-code debugging .net-core build
1个回答
7
投票

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

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

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

下面是整个tasks.json 文件...(提供整体上下文/语法)


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

© www.soinside.com 2019 - 2024. All rights reserved.