奇怪的构建错误 .Net Core 项目

7

我的电脑上安装了以下软件:

  • Visual Studio Pro 2015
  • Visual Studio 2015 Update 3
  • .NET Core 1.0.1 工具预览版2

注意:在 Windows 10 和 Windows 7 机器上都出现了构建错误,但是在另一台 Windows 7 机器上可以正常构建。

因此,我从另一位开发人员那里 git clone 了一个 .Net Core 项目,然后在 Visual Studio 2015 中尝试构建时,出现了以下错误:

"The system cannot find the file specified in Microsoft.DotNet.Common.Targets line 262"

前往 C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets 的第 262 行,我看到了以下内容:

    <Dnx
  RuntimeExe="$(SDKToolingExe)"
  Condition="'$(_DesignTimeHostBuild)' != 'true'"
  ProjectFolder="$(MSBuildProjectDirectory)"
  Arguments="$(_BuildArguments)"
  />

如果从Microsoft.DotNet.Common.Targets中删除此部分,则项目将构建。
我知道dnx是旧的.Net Core工具,我的猜测是类库的project.json中的某些内容对旧的dnx工具是遗留物,而我安装的新的1.0.1 Preview 2 .Net Core工具将该遗留的project.json条目推迟到Microsoft.DotNet.Common.Targets中的旧的dnx工具条目,但由于我没有安装dnx工具,因此构建失败,当然,这只是一个猜测。
我在网上广泛搜索了这个问题,我找到一篇文章,其中提到在project.json中为Microsoft.NETCore.App依赖项添加"type": "platform",但我没有使用该依赖项,我尝试将"type": "platform"添加到我的"NETStandard.Library"依赖项中,但没有帮助,这是我的project.json:
{
  "version": "1.0.1-*",

  "dependencies":
  {
    "Microsoft.AspNetCore.Mvc.Abstractions": "1.1.0",
    "Microsoft.AspNetCore.Mvc.Core": "1.1.0",
    "NETStandard.Library": "1.6.1",
    "TSO.ProductItemList.Model": "1.0.2"
  },

  "frameworks":
  {
    "netstandard1.6":
    {
      "imports": "dnxcore50"
    }
  },

  "scripts":
  {
    "postcompile":
    [
      "dotnet pack --no-build --configuration %compile:Configuration%",
      "\"C:\\Program Files (x86)\\NuGet\\nuget\" push \"%project:Directory%\\bin\\%compile:Configuration%\\%project:Name%.%project:Version%.nupkg\" -s http://foo/NugetServer/ -apikey testkey"
    ]
  }
}

这是构建输出:
1>------ Build started: Project: TSO.ProductItemList.Model, Configuration: Debug Any CPU ------
1>  C:\Program Files\dotnet\dotnet.exe build "C:\projects\tsl\ProductItemList\src\TSO.ProductItemList.Model" --configuration Debug --no-dependencies
1>  Project TSO.ProductItemList.Model (.NETStandard,Version=v1.6) will be compiled because project is not safe for incremental compilation. Use --build-profile flag for more information.
1>  Compiling TSO.ProductItemList.Model for .NETStandard,Version=v1.6
1>  Producing nuget package "TSO.ProductItemList.Model.1.0.2" for TSO.ProductItemList.Model
1>  TSO.ProductItemList.Model -> C:\projects\tsl\ProductItemList\src\TSO.ProductItemList.Model\bin\Debug\TSO.ProductItemList.Model.1.0.2.nupkg
1>  Producing nuget package "TSO.ProductItemList.Model.1.0.2.symbols" for TSO.ProductItemList.Model
1>  TSO.ProductItemList.Model -> C:\projects\tsl\ProductItemList\src\TSO.ProductItemList.Model\bin\Debug\TSO.ProductItemList.Model.1.0.2.symbols.nupkg
1>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets(262,5): error : The system cannot find the file specified
2>------ Build started: Project: TSO.ProductItemList.Client, Configuration: Debug Any CPU ------
2>  C:\Program Files\dotnet\dotnet.exe build "C:\projects\tsl\ProductItemList\src\TSO.ProductItemList.Client" --configuration Debug --no-dependencies
2>  Project TSO.ProductItemList.Client (.NETStandard,Version=v1.6) will be compiled because project is not safe for incremental compilation. Use --build-profile flag for more information.
2>  Compiling TSO.ProductItemList.Client for .NETStandard,Version=v1.6
2>  Producing nuget package "TSO.ProductItemList.Client.1.0.1" for TSO.ProductItemList.Client
2>  TSO.ProductItemList.Client -> C:\projects\tsl\ProductItemList\src\TSO.ProductItemList.Client\bin\Debug\TSO.ProductItemList.Client.1.0.1.nupkg
2>  Producing nuget package "TSO.ProductItemList.Client.1.0.1.symbols" for TSO.ProductItemList.Client
2>  TSO.ProductItemList.Client -> C:\projects\tsl\ProductItemList\src\TSO.ProductItemList.Client\bin\Debug\TSO.ProductItemList.Client.1.0.1.symbols.nupkg
2>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Common.Targets(262,5): error : The system cannot find the file specified
========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========

@LexLi,Visual Studio 2015 被认为是旧的工具?为什么微软的 .NET Core 安装指南的第一步是“下载 Visual Studio 2015”?https://www.microsoft.com/net/core#windowsvs2015 - Brian Ogden
只是因为现在还不是3月7日。他们可能不想在这么短的时间内更新页面两次。 - Lex Li
@LexLi 我对 VS 2015 存在问题持怀疑态度,因为 VS 2015 应该只是使用已安装的 .NET Core 工具,就像我这种情况下所用的 .NET Core 1.0.1 工具预览版 2。 - Brian Ogden
1
那显然是预览2的问题。由于整个工具链正在转移到VS2017,您很快就会每天需要它。 - Lex Li
抱歉 @LexLi,我错了,我刚发现是脚本中的 postcompile 命令,在发布 nuget 包时出了一些问题。 - Brian Ogden
显示剩余3条评论
3个回答

10

错误信息完全误导了我,实际上是我的 postcompile 命令发布库到自己的NuGet服务器时出错了:

  "scripts":
  {
    "postcompile":
    [
      "dotnet pack --no-build --configuration %compile:Configuration%",
      "\"C:\\Program Files (x86)\\NuGet\\nuget\" push \"%project:Directory%\\bin\\%compile:Configuration%\\%project:Name%.%project:Version%.nupkg\" -s http://foo/NugetServer/ -apikey testkey"
    ]
  }

该 cmd 尝试调用 C:\Program Files (x86)\NuGet\nuget.exe

正如我所说,我克隆了这个项目并没有创建它,创建该项目的开发人员已经安装了 C:\Program Files (x86)\NuGet\nuget.exe

我只有 C:\Program Files (x86)\NuGet,没有 nuget.exe,该文件夹存在是因为我安装了 Visual Studio 2015 的 nuget 扩展,并且 .vsix 文件存放在此处,但没有 nuget.exe。

我只是从这里下载了最新版(v3.5.0)的 nuget.exe 并将其放置在我的后编译命令所期望的位置:C:\Program Files (x86)\NuGet


1
很高兴看到你找到了原因,但是建议升级到最新的SDK,因为预览版本将过期。 - Lex Li
很好的提示,在我的情况下也是原因。希望随着csproj在VS2017中的回归,能有更好的SDK。 - ZoolWay

0

我已经创建了场景。

你可以完全恢复包。

按照以下步骤操作:

右键单击项目-> 点击还原包

然后检查输出。

希望这能解决你的问题。


此外,我建议您使用Visual Studio 2017社区版或专业版进行高版本的.NET Core 2.x开发。 - Sakeri Dilip Kumar

0

我已经安装了 .net core 1.1,我已经删除了 .json.lock 并等待还原,但随后的构建仍然失败。 - Brian Ogden
请尝试添加运行时部分(在框架部分下方): - travis.js
"runtimes": { "win10-x64": {} }, - travis.js
同样发生在Windows 7上。 - Brian Ogden
尝试添加运行时条目,但没有成功,我已经将构建输出添加到我的问题中。 - Brian Ogden

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接