【问题标题】:Visual Studio Code cannot build the default dotnet core class libraryVisual Studio Code 无法构建默认的 dotnet core 类库
【发布时间】:2019-06-15 03:57:09
【问题描述】:

我无法让 VS Code 构建一个空的类库,而 dotnet core 可以很高兴。

在 PowerShell 中,我创建了一个名为 CoreTesting 的文件夹,导航到该文件夹​​并使用 code 启动 VS Code。

我点击CTRL + ` 进入终端并导航到解决方案的文件夹。

然后我输入dotnet new classlib --name Common,看到新建文件夹Common,输入dotnet build .\Common\构建类库。一切都很好。

我将 Common 文件夹添加到 VS Code 并点击 CTRL + SHIFT + B 并看到 No build task to run found. Configure Build Task...,所以我点击 return 并看到 Create tasks.json file from template,所以我再次点击 return 并看到:

  • MSBuild
  • maven
  • .NET Core
  • Others

所以我选择.NET Core 并看到创建了一个包含tasks.json.vscode 文件夹。该文件包含:

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

我再次点击CTRL + SHIFT + B 并看到build Common 选项,所以我点击return 并看到这个:

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

我能看到的结构是这样的:

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

我做错了什么?

【问题讨论】:

  • 否;为了更清楚,我已将文件夹结构添加到帖子中。

标签: .net-core visual-studio-code build class-library vscode-tasks


【解决方案1】:

我能够在 v1.41.1 for Windows 上重现您的问题。在这样做的过程中,它创建了这个tasks.json,它与你的相似......

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

当您调用任务时,它默认使用工作区文件夹 (CoreTesting) 路径作为工作目录。但是,项目文件是Common 下的目录,因此出现错误The current working directory does not contain a project or solution file.

对此的快速解决方法是简单地打开带有项目文件的目录作为工作区文件夹(即FileOpen Folder... → 选择Common 目录)。

或者,如果该解决方案不受欢迎,则将CoreTesting 作为工作区文件夹打开,您可以将任务配置为使用不同的工作目录执行。为此,请设置任务的 options.cwd 属性...

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

我在Schema for tasks.json 中找到了这个属性,Custom tasks section of the Tasks documentation 中也提到了它。在进行以上任一更改后,库为我成功构建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 2016-08-18
    • 2018-04-08
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    相关资源
    最近更新 更多