【问题标题】:How to fix NU1212 for dotnet tool install如何修复 NU1212 以进行 dotnet 工具安装
【发布时间】:2018-09-26 22:46:42
【问题描述】:

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

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

testconsole.csproj

  1. dotnet restore testconsole.csproj
  2. dotnet 构建 testconsole.csproj
  3. dotnet pack testconsole.csproj
  4. dotnet tool install -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

【问题讨论】:

    标签: c# .net-core nuget-package


    【解决方案1】:

    找到根本原因后,这​​个错误很可笑,但也表明系统问题。

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

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

    这个 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/>s nuget 提要,以便在安装testconsole 时仅将./nupkg 目录用作提要。

    【讨论】:

    • 我确实想知道为什么会出现该警告,但无法弄清楚为什么使用了错误的框架。
    • 谢谢。我将 testconsole 应用程序的名称更改为独特的名称,它现在可以安装了。你认为我应该在 github repo 中提出一个错误吗?
    • @AusDev 是的,我认为这也是一个错误。它应该说明它从哪里获取包,或者是否找到了多个版本。这就是我所说的“系统性问题”。
    【解决方案2】:

    基于 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
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 1970-01-01
      • 2019-01-29
      • 2019-02-27
      • 1970-01-01
      • 2020-08-28
      • 2022-09-24
      • 1970-01-01
      • 2021-05-16
      相关资源
      最近更新 更多