10

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

AusDev
  • 103
  • 1
  • 4

2 Answers2

11

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.
omajid
  • 14,165
  • 4
  • 47
  • 64
  • I did wonder why that warning was appearing but couldn't figure out why the wrong framework was being used. – AusDev Sep 27 '18 at 00:56
  • Thanks. I changed the name of the testconsole app to something unique and it now installs. Do you think I should raise a bug in the github repo? – AusDev Sep 27 '18 at 00:57
  • @AusDev yes, I think it's a bug too. It should say something about where it's getting the package from, or if there are multiple versions found. That's what I meant by a "systematic issue". – omajid Sep 27 '18 at 14:36
10

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
daw
  • 1,959
  • 1
  • 20
  • 25