-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix NullReferenceException when NuGet source URLs contain leading whitespace #49315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
fe6b456
to
68eb541
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a NullReferenceException caused by untrimmed whitespace in NuGet source URLs and adds tests to validate the change.
- Adds
.Trim()
calls when constructingPackageSource
in both override and default source loaders - Introduces two new tests covering leading-space scenarios for source overrides and additional feeds
- Ensures package operations succeed even if
NuGet.config
entries start with whitespace
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs | Trimmed source string before creating PackageSource to prevent NRE |
test/Microsoft.DotNet.PackageInstall.Tests/NuGetPackageInstallerTests.cs | Added tests for leading-space in source overrides and additional feeds |
Comments suppressed due to low confidence (3)
test/Microsoft.DotNet.PackageInstall.Tests/NuGetPackageInstallerTests.cs:245
- [nitpick] The variable name 'getTestLocalFeedPath' is confusing as it shares the method name. Consider renaming it to 'testLocalFeedPath' or 'localFeedPath' for clarity.
string getTestLocalFeedPath = GetTestLocalFeedPath();
test/Microsoft.DotNet.PackageInstall.Tests/NuGetPackageInstallerTests.cs:68
- [nitpick] Test covers leading spaces but not other whitespace such as tabs or mixed whitespace. Consider adding cases for '\t' or mixed whitespace to ensure full coverage.
new PackageSourceLocation(sourceFeedOverrides: new[] { " " + GetTestLocalFeedPath() }); // Leading space
src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs:358
- [nitpick] The trimming logic is applied inline in multiple places. Consider extracting a helper method to normalize source strings to reduce duplication and improve readability.
PackageSource packageSource = new(source.Trim());
The new tests fail with this error because sdk/src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs Lines 34 to 58 in 8eb971c
|
… issues Co-authored-by: akoeplinger <1376924+akoeplinger@users.noreply.github.com>
Fixed in commit b14bd65. The issue was that trimming was happening too late in the process. I moved the Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Test failures are unrelated, this looks good :) |
After a second look, while this looks good I'm not sure if it would fix the actual issue from #49211 where the feed is defined in nuget.config. Wouldn't it need to be fixed in https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Configuration/PackageSource/PackageSourceProvider.cs ? |
You're absolutely correct. After investigating further, I can see that my fix addresses the wrong layer. The NuGet.config parsing happens in Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Problem
When a
NuGet.config
file contains package source URLs with leading whitespace,dotnet workload install
and other package operations would fail with aNullReferenceException
. This created a confusing error message for users who accidentally introduced whitespace in their configuration files.Example problematic
NuGet.config
:Root Cause
The issue occurred in
NuGetPackageDownloader.cs
where string URLs were used to createPackageSource
objects without trimming whitespace. While NuGet'sPackageSource
class handles most whitespace scenarios internally, there were specific code paths in the SDK where untrimmed URLs could cause issues during processing and comparisons.Solution
Added defensive
.Trim()
calls when creatingPackageSource
objects from string URLs in two locations:LoadOverrideSources
method - for source feed overridesLoadDefaultSources
method - for additional source feedsTesting
Added comprehensive test coverage for both affected code paths:
GivenSourceWithLeadingWhitespaceInstallSucceeds
- Tests sourceFeedOverrides with leading whitespaceGivenAdditionalSourceWithLeadingWhitespaceInstallSucceeds
- Tests additionalSourceFeeds with leading whitespaceBenefits
NullReferenceException
with malformed NuGet.config filesThis follows the principle of "be liberal in what you accept" and provides a better developer experience by gracefully handling common configuration formatting issues.
Fixes #49211.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
x3yvsblobprodcus370.vsblob.vsassets.io
/home/REDACTED/work/sdk/sdk/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/sdk/sdk/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/sdk/sdk/artifacts/toolset/10.0.0-beta.25304.106.txt
(dns block)dotnet build test/Microsoft.DotNet.PackageInstall.Tests/Microsoft.DotNet.PackageInstall.Tests.csproj --configuration Debug --verbosity minimal
(dns block)If you need me to access, download, or install something from one of these locations, you can either: