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

We encountered a NU1101 error when trying to deploy a webApp to Stadium Server on one of our machines.

The deployment fails at the dotnet publish step where dotnet is falling back to using offline package sources and subsequently throwing an error because those packages do not exist in the offline packages.

What made things even weirder was that changing the Stadium Server service to make use of a user account instead of the default Local System account allowed publish to complete successfully. Clearing the nuget cache and running dotnet restore before publishing the failed webApp also resulted in the app being successfully published but this was just a workaround and didn’t explain why deployment was running successfully on other machines.

The issue was eventually found to be an empty NuGet.config file at

C:\Windows\System32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config

that was overwriting the NuGet sources, thus causing dotnet publish to default to offline sources.

Why the NuGet.config is empty is not entirely clear to us at this point but following this thread [Workflow fails to download dependencies from NuGet.org · Issue #3038 · actions/runner-images · GitHub], it seems that the empty config file gets generated when running Chocolatey or the Install-Module of Powershell on a system that does not have VIsual Studio installed; even if Visual Studio is then installed later, the issue persists.

Adding the correct NuGet sources to the NuGet.config file fixed the issue.

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