如何修复 NU1212 以安装 dotnet 工具

Aus*_*Dev 4 c# nuget-package .net-core

我正在构建一个 dotnet 核心工具,但在全局安装时遇到问题。我可以复制我遇到的问题,但不知道如何解决它。下面是我的步骤

  1. dotnet 新控制台 -o testconsole
  2. 修改 testconsole.csproj 以包含<PackAsTool><PackageOutputPath>

testconsole.csproj

  1. dotnet 恢复 testconsole.csproj
  2. dotnet 构建 testconsole.csproj
  3. dotnet 包 testconsole.csproj
  4. dotnet 工具安装 -g -vd --add-source ./nupkg testconsole

安装时我收到以下错误

error NU1212: Invalid project-package combination for TestConsole 1.0.9. DotnetToolReference project style can only contain references of the DotnetTool type

安装错误

这是 nupkg 中 testconsole.nuspec 的副本,其中包含 <packageType name="DotnetTool" />来自https://natemcmaster.com/blog/2018/05/12/dotnet-global-tools/的建议

testconsole.nupsec

Oma*_*jid 7

找到根本原因后,这​​个错误很搞笑,但也是系统问题的征兆。

您在输出中看到警告的这一部分了吗?

包“TestConsole 1.0.9”是使用“.NETFramework,Version=v4.6.1”而不是项目目标框架“.NETCoreApp,Version=v2.1”恢复的。此包可能与您的项目不完全兼容

这个1.0.9版本是什么?.NET Framework 4.6.1 来自哪里?在 .NET Core SDK(我查看了源代码)或testconsole我磁盘上的目录下没有类似的东西。

让我们减少日志记录的详细程度并重新运行安装命令:

$ dotnet tool install -g -v n --add-source ./nupkg testconsole
Build started 2018-09-26 7:16:47 p.m..
     1>Project "/tmp/q2whkgqf.tbt/restore.csproj" on node 1 (Restore target(s)).
     1>Restore:
         Restoring packages for /tmp/q2whkgqf.tbt/restore.csproj...
           CACHE https://api.nuget.org/v3-flatcontainer/testconsole/index.json
           CACHE https://api.nuget.org/v3-flatcontainer/testconsole/1.0.9/testconsole.1.0.9.nupkg
         Installing TestConsole 1.0.9.0.
Run Code Online (Sandbox Code Playgroud)

仔细看最后几行。dotnet tool install正在尝试安装https://www.nuget.org/packages/TestConsole/。不是您本地的testconsolenuget 包!

您可以通过以下几种方式解决它:

  1. 为您的工具提供一个真正独特的名称,该名称不会与 nuget.org 或您组织的 nuget 提要中的任何内容发生冲突。
  2. 添加nuget.config的是<clear/>S中的NuGet饲料所以只有./nupkg寻找到安装目录时,被用作饲料testconsole


daw*_*daw 7

基于 Omair 的回答,解决这个问题的实际步骤:

1. 创建disable_nuget.config禁用从全局提要读取

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <disabledPackageSources>
        <add key="nuget.org" value="true" />
    </disabledPackageSources>
</configuration>
Run Code Online (Sandbox Code Playgroud)

注意:给它一个特殊的名字,这样它就不会在你不想要的时候被 nuget 无意中捡到

2.安装参考特殊nuget配置的工具

dotnet pack
dotnet tool install --global --configfile disable_nuget.config --add-source ./nupkg TestConsole
Run Code Online (Sandbox Code Playgroud)