NuGet "source with name already exists" - Fix Duplicate Feeds
A CI step ran dotnet nuget add source for a feed that is already present in the effective NuGet.config, and NuGet refuses to add a duplicate name or URL.
What this error means
The dotnet nuget add source step fails saying the source name or URL already exists. It commonly appears on re-runs or when a global NuGet.config already defines the feed.
error: The source specified has already been added to the list of available
package sources. Provided source: https://nuget.pkg.github.com/contoso/index.json.Common causes
The feed is already in an inherited NuGet.config
NuGet merges machine-, user-, and repo-level configs. A feed defined in a parent config makes a later add source a duplicate.
A non-idempotent add in a re-run job
Adding the source in a step that runs twice (retry, matrix) errors the second time because the first add already registered it.
How to fix it
Update the source instead of adding it
Use update source (or add-then-fallback-to-update) so the step is idempotent.
dotnet nuget update source github \
--username USER --password "$GH_PACKAGES_TOKEN" \
--store-password-in-clear-text \
|| dotnet nuget add source https://nuget.pkg.github.com/contoso/index.json --name github ...Define feeds in a committed NuGet.config
Declare sources statically so no per-run add source is needed at all.
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="github" value="https://nuget.pkg.github.com/contoso/index.json" />
</packageSources>How to prevent it
- Prefer a committed
NuGet.configover runtimeadd sourcecalls. - When you must add at runtime, make it idempotent with update-or-add.
- Use
<clear />to control inherited sources predictably.