From 13ac45716292059672735fd4d0e410fb3b8ebc61 Mon Sep 17 00:00:00 2001 From: Robert Coltheart Date: Wed, 25 Feb 2026 21:38:12 +1100 Subject: [PATCH] Pass explicit source to nuget downloader --- .../dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs | 9 +++++---- .../NugetPackageDownloader/NuGetPackageDownloader.cs | 5 +++++ .../NugetPackageDownloader/PackageSourceLocation.cs | 8 ++++++-- src/Cli/dotnet/ToolPackage/PackageLocation.cs | 7 +++++-- src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs | 2 +- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs b/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs index ca401c280ccd..6c5e83aebcab 100644 --- a/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; @@ -128,13 +128,14 @@ public override int Execute() } // We've already determined which source we will use and displayed that in a confirmation message to the user. - // So set the package location here to override the source feeds to just the source we already resolved to. + // So set the package location here to override the source feeds to the explicit source we already resolved to. // This does mean that we won't work with feeds that have a primary package but where the RID-specific packages are on // other feeds, but this is probably OK. var downloadPackageLocation = new PackageLocation( nugetConfig: _configFile != null ? new(_configFile) : null, - sourceFeedOverrides: [packageSource.Source], - additionalFeeds: _addSource); + sourceFeedOverrides: _sources, + additionalFeeds: _addSource, + explicitSource: packageSource); toolPackage = _toolPackageDownloader.InstallPackage( downloadPackageLocation, diff --git a/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs b/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs index a311e88c646d..8b12dba8efb7 100644 --- a/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs +++ b/src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs @@ -443,6 +443,11 @@ private List LoadDefaultSources(PackageId packageId, PackageSourc public IEnumerable LoadNuGetSources(PackageId packageId, PackageSourceLocation packageSourceLocation = null, PackageSourceMapping packageSourceMapping = null) { + if (packageSourceLocation?.ExplicitSource != null) + { + return [packageSourceLocation.ExplicitSource]; + } + var sources = (packageSourceLocation?.SourceFeedOverrides.Any() ?? false) ? LoadOverrideSources(packageSourceLocation) : LoadDefaultSources(packageId, packageSourceLocation, packageSourceMapping); diff --git a/src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs b/src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs index ed70adeaf924..3c826a33b5ba 100644 --- a/src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs +++ b/src/Cli/dotnet/NugetPackageDownloader/PackageSourceLocation.cs @@ -1,9 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #nullable disable using Microsoft.Extensions.EnvironmentAbstractions; +using NuGet.Configuration; namespace Microsoft.DotNet.Cli.NuGetPackageDownloader; @@ -14,7 +15,8 @@ public PackageSourceLocation( DirectoryPath? rootConfigDirectory = null, string[] sourceFeedOverrides = null, string[] additionalSourceFeeds = null, - string basePath = null) + string basePath = null, + PackageSource explicitSource = null) { basePath = basePath ?? Directory.GetCurrentDirectory(); @@ -24,12 +26,14 @@ public PackageSourceLocation( SourceFeedOverrides = ExpandLocalFeed(sourceFeedOverrides, basePath); // Feeds to be using in addition to config AdditionalSourceFeed = ExpandLocalFeed(additionalSourceFeeds, basePath); + ExplicitSource = explicitSource; } public FilePath? NugetConfig { get; } public DirectoryPath? RootConfigDirectory { get; } public string[] SourceFeedOverrides { get; private set; } public string[] AdditionalSourceFeed { get; private set; } + public PackageSource ExplicitSource { get; private set; } private static string[] ExpandLocalFeed(string[] sourceFeedOverrides, string basePath) { diff --git a/src/Cli/dotnet/ToolPackage/PackageLocation.cs b/src/Cli/dotnet/ToolPackage/PackageLocation.cs index b102097a1c2d..b5494bd802b5 100644 --- a/src/Cli/dotnet/ToolPackage/PackageLocation.cs +++ b/src/Cli/dotnet/ToolPackage/PackageLocation.cs @@ -1,9 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #nullable disable using Microsoft.Extensions.EnvironmentAbstractions; +using NuGet.Configuration; namespace Microsoft.DotNet.Cli.ToolPackage; @@ -11,10 +12,12 @@ internal class PackageLocation( FilePath? nugetConfig = null, DirectoryPath? rootConfigDirectory = null, string[] additionalFeeds = null, - string[] sourceFeedOverrides = null) + string[] sourceFeedOverrides = null, + PackageSource explicitSource = null) { public FilePath? NugetConfig { get; } = nugetConfig; public DirectoryPath? RootConfigDirectory { get; } = rootConfigDirectory; public string[] AdditionalFeeds { get; } = additionalFeeds ?? []; public string[] SourceFeedOverrides { get; } = sourceFeedOverrides ?? []; + public PackageSource ExplicitSource { get; } = explicitSource; } diff --git a/src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs b/src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs index 95bb5c1b900e..3e4ebf1426b6 100644 --- a/src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs +++ b/src/Cli/dotnet/ToolPackage/ToolPackageDownloaderBase.cs @@ -110,7 +110,7 @@ public IToolPackage InstallPackage(PackageLocation packageLocation, PackageId pa verbosity, restoreActionConfig); - var packageSourceLocation = new PackageSourceLocation(packageLocation.NugetConfig, packageLocation.RootConfigDirectory, packageLocation.SourceFeedOverrides, packageLocation.AdditionalFeeds, _currentWorkingDirectory); + var packageSourceLocation = new PackageSourceLocation(packageLocation.NugetConfig, packageLocation.RootConfigDirectory, packageLocation.SourceFeedOverrides, packageLocation.AdditionalFeeds, _currentWorkingDirectory, packageLocation.ExplicitSource); NuGetVersion packageVersion = nugetPackageDownloader.GetBestPackageVersionAsync(packageId, versionRange, packageSourceLocation).GetAwaiter().GetResult();