Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to resolve "Unable to find package" nuget error

Tags:

nuget

nuspec

Working on creating a nuget pkg for a project (A.csproj) which depends on another project (B.csprojec) added as a project reference.
Here is the .nuspec ,

<?xml version="1.0"?>
<package >
  <metadata>
    <id>A.Client</id>
    <title>A.Client</title>
    <description>HttpClient and Models for calling the A Microservice.</description>
    <version>1.0.2</version>
    <authors></authors>
    <owners></owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <copyright>.</copyright>
    <tags></tags>
    <dependencies>
      <group targetFramework=".NETFramework4.5">
        <dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
        // <dependency id="B" version="1.0.0"/> tried this but same error
      </group>
      <group targetFramework=".NETStandard2.0">
        <dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
        // <dependency id="B" version="1.0.0"/> tried this but same error
      </group>
    </dependencies>
    <frameworkAssemblies>
      <frameworkAssembly assemblyName="System.Net.Http" targetFramework=".NETFramework4.5" />
    </frameworkAssemblies>
  </metadata>
  <files>
    <file src="bin\$configuration$\netstandard2.0\A.dll" target="lib\netstandard2.0" />
    <file src="bin\$configuration$\net45\A.dll" target="lib\net45" />
  </files>
</package>

I used

nuget pack A.nuspec -Properties configuration=debug

To generate the package. However when I tried to consume this package inside c.csprojc, I get the following error

Severity    Code    Description Project File    Line    Suppression State
Error   NU1101  Unable to find B. No packages exist with this id in source(s): Local Package source, Microsoft Visual Studio Offline Packages, nuget.org, Package source    

What did I miss ?

like image 796
yesIcan Avatar asked Sep 17 '18 22:09

yesIcan


People also ask

How do I fix a missing NuGet package?

Enable package restore by choosing Tools > Options > NuGet Package Manager. Under Package Restore options, select Allow NuGet to download missing packages. In Solution Explorer, right click the solution and select Restore NuGet Packages.

How do I force a NuGet package to reinstall?

Switch to the Browse tab, search for the package name, select it, then select Install). For all packages, delete the package folder, then run nuget install . For a single package, delete the package folder and use nuget install <id> to reinstall the same one.

How do I find NuGet packages?

To find native packages on nuget.org, search using tag:native . Such packages typically provide . targets and . props files, which NuGet imports automatically when the package is added to a project.


4 Answers

The package source was offline !!

I installed VS2017 professional on my system and opened an existing project and found that multiple packages were missing. I tried everything I could, without looking at Package Source !!

Manage Nuget Packages for Solution

I am writing this answer as I tried the below solutions but none of them worked :

  1. Clearing the cache
  2. Restoring or re-installing the packages
  3. Changing the targetFramework
  4. Updating the Nuget Package manager

Solution:

Step 01. Go to Package Manager Settings (Tools > Nuget Package Manager > Package Manager Settings) Nuget Package Manager > Package Manager Settings

Step 02. Check the Package Source(s). As you can see, the package source is here already downloaded SDK/nugets/packages. I don't know the reason but the online package source from nuget.org was missing from my system installation of Visual Studio.

enter image description here

Step 03. Install the nuget.org as package source and then 'Clear All Nuget Cache(s)' and then restore the packages. The error will go away.

Name: nuget.org ( or as you wish) Source: https://api.nuget.org/v3/index.json

enter image description here

like image 197
jainashish Avatar answered Oct 23 '22 13:10

jainashish


I just did this and it went fine. In your Visual Studio, go to:

  1. Tools
  2. Nuget Package Manager
  3. General
  4. Click button "Clear All Nuget Cache(s)
  5. Package Resources and click "Update"
  6. Woolah! Error gone...
like image 23
Israel Ocbina Avatar answered Oct 23 '22 15:10

Israel Ocbina


for me, %appdata%\NuGet\NuGet.Config contained only

<activePackageSource>
  <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
</activePackageSource>

after doing

nuget sources add -Name "NuGet official package source" -Source "https://api.nuget.org/v3/index.json"

this was added

<packageSources>
  <add key="https://www.nuget.org/api/v2/" value="https://www.nuget.org/api/v2/" />
  <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
</packageSources>

afterwards packages were found.

like image 9
sotto Avatar answered Oct 23 '22 13:10

sotto


GitHub Actions

I would like to add that in Github Actions, the nuget.org repository is not queried by default. Instead GitHub defaults to a cached package repository and it might happen that your referenced package is not in that cache but is on nuget.org (I had that for System.CommandLine).

You can fix this by adding a nuget.config file to your root directory containing the following

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
</configuration>

Example github_action.yml file

name: .NET

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0
    - name: Clean
      run: dotnet clean && dotnet nuget locals all --clear
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Test
      run: dotnet test --no-build --verbosity normal
like image 8
JBSnorro Avatar answered Oct 23 '22 14:10

JBSnorro