如何为 dotnet 工具安装修复 NU1212

How to fix NU1212 for dotnet tool install

我正在构建一个 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 -v d --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

install error

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

的建议

testconsole.nupsec

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

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

Package 'TestConsole 1.0.9' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.1'. This package may not be fully compatible with your project

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

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

您可以通过多种方式解决此问题:

  1. 为您的工具起一个真正独特的名称,该名称不会与 nuget.org 或您组织的 nuget 提要中的任何内容冲突。
  2. 添加一个 nuget.config<clear/> 是 nuget 提要,因此在寻找安装 testconsole.
  3. 时,只有 ./nupkg 目录用作提要

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

1。创建 disable_nuget.config 以禁止从全局提要中读取

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <disabledPackageSources>
        <add key="nuget.org" value="true" />
    </disabledPackageSources>
</configuration>

注意:给它一个特殊的名字,这样它就不会在你不想要它的时候不小心被 nuget 拾取

2。安装引用特殊 nuget 配置的工具

dotnet pack
dotnet tool install --global --configfile disable_nuget.config --add-source ./nupkg TestConsole