Skip to content

Commit fe6b456

Browse files
CopilotmarcpopMSFT
andcommitted
Fix NullReferenceException when NuGet sources have leading whitespace
Co-authored-by: marcpopMSFT <[email protected]>
1 parent 7ed5fe3 commit fe6b456

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ private IEnumerable<PackageSource> LoadOverrideSources(PackageSourceLocation pac
355355
continue;
356356
}
357357

358-
PackageSource packageSource = new(source);
358+
PackageSource packageSource = new(source.Trim());
359359
if (packageSource.TrySourceAsUri == null)
360360
{
361361
_verboseLogger.LogWarning(string.Format(
@@ -418,7 +418,7 @@ private List<PackageSource> LoadDefaultSources(PackageId packageId, PackageSourc
418418
continue;
419419
}
420420

421-
PackageSource packageSource = new(source);
421+
PackageSource packageSource = new(source.Trim());
422422
if (packageSource.TrySourceAsUri == null)
423423
{
424424
_verboseLogger.LogWarning(string.Format(

test/Microsoft.DotNet.PackageInstall.Tests/NuGetPackageInstallerTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ public async Task GivenASourceInstallSucceeds()
5959
packagePath.Should().Contain(_tempDirectory.Value, "Package should be downloaded to the input folder");
6060
}
6161

62+
[Fact]
63+
public async Task GivenSourceWithLeadingWhitespaceInstallSucceeds()
64+
{
65+
string packagePath = await _installer.DownloadPackageAsync(
66+
TestPackageId,
67+
new NuGetVersion(TestPackageVersion),
68+
new PackageSourceLocation(sourceFeedOverrides: new[] { " " + GetTestLocalFeedPath() })); // Leading space
69+
File.Exists(packagePath).Should().BeTrue();
70+
packagePath.Should().Contain(_tempDirectory.Value, "Package should be downloaded to the input folder");
71+
}
72+
6273
[Fact]
6374
public async Task GivenAFailedSourceItShouldError()
6475
{
@@ -228,6 +239,19 @@ public async Task WhenPassedIncludePreviewItInstallSucceeds()
228239
"Package should download higher package version");
229240
}
230241

242+
[Fact]
243+
public async Task GivenAdditionalSourceWithLeadingWhitespaceInstallSucceeds()
244+
{
245+
string getTestLocalFeedPath = GetTestLocalFeedPath();
246+
string relativePath = Path.GetRelativePath(Environment.CurrentDirectory, getTestLocalFeedPath);
247+
string packagePath = await _installer.DownloadPackageAsync(
248+
TestPackageId,
249+
new NuGetVersion(TestPackageVersion),
250+
new PackageSourceLocation(additionalSourceFeeds: new[] { " " + relativePath })); // Leading space
251+
File.Exists(packagePath).Should().BeTrue();
252+
packagePath.Should().Contain(_tempDirectory.Value, "Package should be downloaded to the input folder");
253+
}
254+
231255
[WindowsOnlyFact]
232256
public async Task GivenANonSignedSdkItShouldPrintMessageOnce()
233257
{

0 commit comments

Comments
 (0)