Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix NU1212 for dotnet tool install

I am building a dotnet core tool and I am having trouble installing it globally. I can replicate the problem I am having but don't know how to fix it. Below are my steps

  1. dotnet new console -o testconsole
  2. Modify testconsole.csproj to include <PackAsTool> and <PackageOutputPath>

testconsole.csproj

  1. dotnet restore testconsole.csproj
  2. dotnet build testconsole.csproj
  3. dotnet pack testconsole.csproj
  4. dotnet tool install -g -v d --add-source ./nupkg testconsole

When installing I receive the below error

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

install error

Here is a copy of testconsole.nuspec from the nupkg that includes <packageType name="DotnetTool" /> per the suggestion from https://natemcmaster.com/blog/2018/05/12/dotnet-global-tools/

testconsole.nupsec

like image 690
AusDev Avatar asked Sep 26 '18 22:09

AusDev


People also ask

How do I update dotnet tools?

Update the toolsUse dotnet tool update --global dotnet-ef to update the global tools to the latest available version. If you have the tools installed locally in your project use dotnet tool update dotnet-ef . Install a specific version by appending --version <VERSION> to your command.

What is dotnet tool restore?

The dotnet tool restore command finds the tool manifest file that is in scope for the current directory and installs the tools that are listed in it. For information about manifest files, see Install a local tool and Invoke a local tool.


2 Answers

After finding the root cause, this error is hilarious, but also an indication of systematic issue.

Do you see this part of the warning in your output?

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

What is this version 1.0.9? Where is .NET Framework 4.6.1 coming from? There's nothing like that in the .NET Core SDK (I looked through the sources) or under the testconsole directory on my disk.

Lets reduce the logging verbosity and re-run out install command:

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

Look at the last few lines carefully. dotnet tool install is trying to install https://www.nuget.org/packages/TestConsole/. Not your local testconsole nuget package!

You can work around it in a couple of ways:

  1. Give your tools a really unique name that doesn't clash with anything on nuget.org or in your organization's nuget feed.
  2. Add a nuget.config that <clear/>s the nuget feeds so only the ./nupkg directory is used as feed when looking to install testconsole.
like image 200
omajid Avatar answered Sep 22 '22 13:09

omajid


Building on Omair's answer, the practical steps to solving this problem:

1. Create disable_nuget.config to disable reading from the global feed

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

note: give it a special name so that it doesn't get picked up by nuget by accident when you don't want it to

2. Install the tool referencing the special nuget configuration

dotnet pack
dotnet tool install --global --configfile disable_nuget.config --add-source ./nupkg TestConsole
like image 29
daw Avatar answered Sep 26 '22 13:09

daw