Skip to content

Fix NullReferenceException in dotnet nuget update source for unknown source name - #7744

Merged
jeffkl merged 2 commits into
devfrom
copilot/fix-dotnet-nuget-update-source-error
Sep 29, 2026
Merged

jeffkl merged 2 commits into
devfrom
copilot/fix-dotnet-nuget-update-source-error

Conversation

Copilot AI commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

dotnet nuget update source <name> crashed with error: Object reference not set to an instance of an object. instead of a helpful error message when <name> did not match any configured package source.

Root cause

  • UpdateSourceRunner.Run assigned existingSource.AllowInsecureConnections = ... before checking whether existingSource (returned by GetPackageSourceByName) was null.
  • The intended error path (Unable to find any package source(s) matching name: <name>) already existed but was unreachable because the NullReferenceException was thrown first.

Fix

  • Reordered UpdateSourceRunner.Run to validate existingSource != null before dereferencing it, matching the pattern already used in RemoveSourceRunner.
var existingSource = sourceProvider.GetPackageSourceByName(args.Name);
if (existingSource == null)
{
    throw new CommandException(Strings.SourcesCommandNoMatchingSourcesFound, args.Name);
}

existingSource.AllowInsecureConnections = args.AllowInsecureConnections;

Tests

  • Added Sources_WhenUpdatingNonExistentSource_Errors in DotnetSourcesTests.cs to verify updating a nonexistent source name surfaces the correct error message rather than crashing.
  • Fixes #15094

Copilot AI changed the title [WIP] Fix 'Object reference not set' error in dotnet nuget update source command Fix NullReferenceException in dotnet nuget update source for unknown source name Sep 28, 2026
Copilot AI requested a review from jeffkl September 28, 2026 17:17
@jeffkl
jeffkl marked this pull request as ready for review September 28, 2026 17:22
@jeffkl
jeffkl requested a review from a team as a code owner September 28, 2026 17:22
@jeffkl
jeffkl requested a review from nkolev92 September 28, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotnet nuget update source nuget fails with "error: Object reference not set to an instance of an object."

4 participants