如何解决“无法找到包”的核心错误

正在为一个项目(A.csproj)创建一个 nuget pkg,它依赖于另一个项目(B.csprojec)作为项目引用添加。
这是. 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>

我用过

nuget pack A.nuspec -Properties configuration=debug

然而,当我尝试在 c.csprojc 中使用这个包时,会得到以下错误

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

我错过了什么?

163344 次浏览

I resolved this by adding dependent file

<files>
<file src="bin\$configuration$\netstandard2.0\a.dll" target="lib\netstandard2.0" />
<file src="bin\$configuration$\net45\a.dll" target="lib\net45" />
<file src="bin\$configuration$\netstandard2.0\b.dll" target="lib\netstandard2.0" />
<file src="bin\$configuration$\net45\b.dll" target="lib\net45" />
</files>

Also, don't forget to clean local cache nuget locals all –clean or change version.

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

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

I was able to resolve this issue by adding few more package source links in nuget package source(Tools -> Nuget Package Manager -> Package Manager settings -> Package source).

Before trying above solution, I tried, clearing nuget cache, updating targetFramework, re-installing the package, updating the package manager, none of them worked.

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.

Started getting these errors sporadically for our Azure DevOps build pipeline but never locally.

Example error:

error NU1101: Unable to find package Microsoft.EntityFrameworkCore.Sqlite. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages

If we restarted the build it could work but then it happened again.

I then noticed it always happened in our test project and not our main web project.

We use .NET/NuGet pipeline caching to speed up our builds. We use the default version that looks like this:

variables:
NUGET_PACKAGES: $(Pipeline.Workspace)/.nuget/packages


steps:
- task: Cache@2
inputs:
key: 'nuget | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**'
restoreKeys: |
nuget | "$(Agent.OS)"
path: $(NUGET_PACKAGES)
displayName: Cache NuGet packages

https://learn.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops#netnuget

The only change is that Microsoft uses $(UserProfile) but we use $(Pipeline.Workspace) for the variable. Got an error using $(UserProfile) that you can read more about here:

https://github.com/MicrosoftDocs/azure-devops-docs/issues/10110

It reads packages.lock.json that needs to be enabled by editing your project file (.csproj).

<PropertyGroup>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

https://devblogs.microsoft.com/nuget/enable-repeatable-package-restores-using-a-lock-file/

Turned out we only generated a packages.lock.json for our web project but not our test project. After adding RestorePackagesWithLockFile to our test project as well it started working again.

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

I had a similar issue whereby I had multiple package sources defined, and api.nuget.org was not being hit due to a failure in another source. I mitigated this by using the --ignore-failed-sources option, e.g.:

dotnet tool install -g dotnet-ef --ignore-failed-sources

Output:

C:\Program Files\dotnet\sdk\5.0.302\NuGet.targets(131,5): warning NU1801: Unable to load the service index for source [[REDACTED]]. [c:\temp\3iqdpc5w.qpj\restore.csproj]
You can invoke the tool using the following command: dotnet-ef
Tool 'dotnet-ef' (version '5.0.8') was successfully installed.

Verified as installed via:

dotnet tool list -g

Output:

Package Id      Version      Commands
--------------------------------------
dotnet-ef       5.0.8        dotnet-ef

I had a similar issue. It couldn't find the packages as it was looking for them stored offline in my computer instead of online. The Visual Studio NuGet settings screen didn't show this but the error message did. I have no interest in offline packages so to solve it (windows):

First go to in Visual Studio 2019, Tools>Nuget Package Manager>Package Manager Settings>Package Sources and add: https://api.nuget.org/v3/index.json (if this isn't already present), give it the name nuget.org. My error persisted after doing this, hence the next the steps.

Press start and enter %appdata% to take you to C:/Users/[username]/AppData/Roaming. Navigate to the NuGet directory and open NuGet.Config.

All Package sources appear here, even if they don't appear on the Visual Studio Package Manager settings.

Modify the PackageSources section to remove the sources you don't need. In my case I didn't need the offline source, so I was left with:

<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>

This solved the issue for me.

In addition to sotto's answer, this is the way to do it with dotnet cli:

dotnet nuget add source "https://api.nuget.org/v3/index.json" --name "NuGet official package source"

For JetBrains Rider Users; This is how you solve this problem in JetBrains Rider:

  1. Open NuGet section
  2. Add a Feed by clicking "+" sign
  3. Write Nuget in the name section, and for the URL section write this https://api.nuget.org/v3/index.json then click ok.
  4. Restart JetBrains Rider and the problem should be resolved.

enter image description here