Visual Studio无法构建默认的dotnet核心类库

时间:2019-01-21 17:53:39

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

虽然dotnet核心可以很高兴地使我无法获得VS Code来建立一个空的类库。

在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,并看到创建了一个包含.vscode的{​​{1}}文件夹。该文件包含:

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我能够在Windows v1.41.1上重现您的问题。为此,它创建了这个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中也提到了此属性。在进行上述任何更改之后,库对我来说都是成功构建的。