Skip to content

Commit 27547d7

Browse files
Fix dnx not authenticating for private feeds (#53322)
1 parent 815727a commit 27547d7

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ public override int Execute()
114114
// other feeds, but this is probably OK.
115115
var downloadPackageLocation = new PackageLocation(
116116
nugetConfig: _configFile != null ? new(_configFile) : null,
117-
sourceFeedOverrides: [packageSource.Source],
118-
additionalFeeds: _addSource);
117+
sourceFeedOverrides: _sources,
118+
additionalFeeds: _addSource,
119+
packageSourceOverrides: [packageSource]);
119120

120121
toolPackage = _toolPackageDownloader.InstallPackage(
121122
downloadPackageLocation,

src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ private List<PackageSource> LoadDefaultSources(PackageId packageId, PackageSourc
468468

469469
public IEnumerable<PackageSource> LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null)
470470
{
471+
if (packageSourceLocation?.PackageSourceOverrides?.Any() ?? false)
472+
{
473+
return packageSourceLocation.PackageSourceOverrides;
474+
}
475+
471476
var sources = (packageSourceLocation?.SourceFeedOverrides.Any() ?? false) ?
472477
LoadOverrideSources(packageSourceLocation) :
473478
LoadDefaultSources(packageId, packageSourceLocation, packageSourceMapping);

src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using Microsoft.Extensions.EnvironmentAbstractions;
7+
using NuGet.Configuration;
78

89
namespace Microsoft.DotNet.Cli.NuGetPackageDownloader;
910

@@ -14,7 +15,8 @@ public PackageSourceLocation(
1415
DirectoryPath? rootConfigDirectory = null,
1516
string[] sourceFeedOverrides = null,
1617
string[] additionalSourceFeeds = null,
17-
string basePath = null)
18+
string basePath = null,
19+
PackageSource[] packageSourceOverrides = null)
1820
{
1921
basePath = basePath ?? Directory.GetCurrentDirectory();
2022

@@ -24,12 +26,15 @@ public PackageSourceLocation(
2426
SourceFeedOverrides = ExpandLocalFeed(sourceFeedOverrides, basePath);
2527
// Feeds to be using in addition to config
2628
AdditionalSourceFeed = ExpandLocalFeed(additionalSourceFeeds, basePath);
29+
// Feeds that have already been evaluated and selected to be used
30+
PackageSourceOverrides = packageSourceOverrides;
2731
}
2832

2933
public FilePath? NugetConfig { get; }
3034
public DirectoryPath? RootConfigDirectory { get; }
3135
public string[] SourceFeedOverrides { get; private set; }
3236
public string[] AdditionalSourceFeed { get; private set; }
37+
public PackageSource[] PackageSourceOverrides { get; private set; }
3338

3439
private static string[] ExpandLocalFeed(string[] sourceFeedOverrides, string basePath)
3540
{

src/Cli/dotnet/ToolPackage/PackageLocation.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
#nullable disable
55

66
using Microsoft.Extensions.EnvironmentAbstractions;
7+
using NuGet.Configuration;
78

89
namespace Microsoft.DotNet.Cli.ToolPackage;
910

1011
internal class PackageLocation(
1112
FilePath? nugetConfig = null,
1213
DirectoryPath? rootConfigDirectory = null,
1314
string[] additionalFeeds = null,
14-
string[] sourceFeedOverrides = null)
15+
string[] sourceFeedOverrides = null,
16+
PackageSource[] packageSourceOverrides = null)
1517
{
1618
public FilePath? NugetConfig { get; } = nugetConfig;
1719
public DirectoryPath? RootConfigDirectory { get; } = rootConfigDirectory;
1820
public string[] AdditionalFeeds { get; } = additionalFeeds ?? [];
1921
public string[] SourceFeedOverrides { get; } = sourceFeedOverrides ?? [];
22+
public PackageSource[] PackageSourceOverrides { get; } = packageSourceOverrides ?? [];
2023
}

src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId pa
110110
verbosity,
111111
restoreActionConfig);
112112

113-
var packageSourceLocation = new PackageSourceLocation(packageLocation.NugetConfig, packageLocation.RootConfigDirectory, packageLocation.SourceFeedOverrides, packageLocation.AdditionalFeeds, _currentWorkingDirectory);
113+
var packageSourceLocation = new PackageSourceLocation(packageLocation.NugetConfig, packageLocation.RootConfigDirectory, packageLocation.SourceFeedOverrides, packageLocation.AdditionalFeeds, _currentWorkingDirectory, packageLocation.PackageSourceOverrides);
114114

115115
NuGetVersion packageVersion = nugetPackageDownloader.GetBestPackageVersionAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult();
116116

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ public async Task WhenPassedIncludePreviewItInstallSucceeds()
244244
"Package should download higher package version");
245245
}
246246

247+
[Fact]
248+
public void GivenPackageOverrideSourceWithCredentialsNugetFeedReturnsSelectedSource()
249+
{
250+
PackageSource source = new PackageSource("NuGet")
251+
{
252+
Credentials = new PackageSourceCredential("NuGet", "user", "pass", true, "basic")
253+
};
254+
255+
IEnumerable<PackageSource> selectedSources = _toolInstaller.LoadNuGetSources(
256+
TestPackageId,
257+
packageSourceLocation: new PackageSourceLocation(packageSourceOverrides: new[] { source }));
258+
259+
selectedSources.Should().HaveCount(1).And.Contain(x => x.Credentials != null);
260+
}
261+
247262
[WindowsOnlyFact]
248263
public async Task GivenANonSignedSdkItShouldPrintMessageOnce()
249264
{

0 commit comments

Comments
 (0)