Skip to content

Commit

Permalink
Do not automatically add nuget.org as a package source when it is abs…
Browse files Browse the repository at this point in the history
…ent from nuget.config Fixes #44312 (#45410)

Fixes #44312

If a user doesn't have nuget.org as a source in their nuget.config, we shouldn't assume they want it. This sends a message for dotnet tool search if nuget.org is absent, as it is needed for dotnet tool search to work properly, instead of running the command. For other commands like dotnet new details, it does not automatically add nuget.org to the sources.

image

image

(Neither command implicitly went to nuget.org.)
  • Loading branch information
Forgind authored Dec 19, 2024
1 parent 0c8a668 commit 20ac0a6
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/PathUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Utils;
using NuGet.Configuration;

namespace Microsoft.DotNet.Tools.Common
{
public static class PathUtility
{
public static bool CheckForNuGetInNuGetConfig()
{
var otherFiles = SettingsUtility.GetEnabledSources(Settings.LoadDefaultSettings(Directory.GetCurrentDirectory()));
return otherFiles.Any(source => source.SourceUri.Equals("https://api.nuget.org/v3/index.json"));
}

public static bool IsPlaceholderFile(string path)
{
return string.Equals(Path.GetFileName(path), "_._", StringComparison.Ordinal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ internal NugetApiManager()
PackageSource? sourceFeed = null,
CancellationToken cancellationToken = default)
{
if (sourceFeed == null)
if (sourceFeed == null && Microsoft.DotNet.Tools.Common.PathUtility.CheckForNuGetInNuGetConfig())
{
sourceFeed = _nugetOrgSource;
}
else if (sourceFeed is null)
{
return null;
}

SourceRepository repository = GetSourceRepository(sourceFeed);
PackageMetadataResource resource = await repository.GetResourceAsync<PackageMetadataResource>(cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Constraints;
using Microsoft.TemplateEngine.Abstractions.Installer;
Expand Down Expand Up @@ -436,7 +437,7 @@ internal async Task<NewCommandStatus> DisplayTemplatePackageMetadata(
}
else
{
IEnumerable<PackageSource> packageSources = LoadNuGetSources(additionalSources, true);
IEnumerable<PackageSource> packageSources = LoadNuGetSources(additionalSources, includeNuGetFeed: PathUtility.CheckForNuGetInNuGetConfig());

nuGetPackageMetadata = await GetPackageMetadataFromMultipleFeedsAsync(packageSources, nugetApiManager, packageIdentity, packageVersion, cancellationToken).ConfigureAwait(false);
if (nuGetPackageMetadata != null && nuGetPackageMetadata.Source.Source.Equals(NugetOrgFeed))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,10 @@
<value>Version</value>
<comment>Table lable</comment>
</data>
<data name="NeedNuGetInConfig" xml:space="preserve">
<value>The 'dotnet tool search' command unconditionally accesses nuget.org to find tools, but it is not present in your nuget.config. Add it to run this command.
This can be done with this command:
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org</value>
<comment>Do not translate 'dotnet tool search' or 'nuget.config'. Do not localize the last line at all.</comment>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.NugetSearch;
using Microsoft.DotNet.Tools.Common;
using NuGet.Configuration;

namespace Microsoft.DotNet.Tools.Tool.Search
{
Expand All @@ -26,6 +28,12 @@ public ToolSearchCommand(
public override int Execute()
{
var isDetailed = _parseResult.GetValue(ToolSearchCommandParser.DetailOption);
if (!PathUtility.CheckForNuGetInNuGetConfig())
{
Reporter.Output.WriteLine(LocalizableStrings.NeedNuGetInConfig);
return 0;
}

NugetSearchApiParameter nugetSearchApiParameter = new(_parseResult);
IReadOnlyCollection<SearchResultPackage> searchResultPackages =
NugetSearchApiResultDeserializer.Deserialize(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions test/dotnet-new.Tests/DotnetNewDetailsTest.Approval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@ public Task CanDisplayDetails_RemotePackage_NuGetFeedWithVersion()
[Fact]
public Task CanDisplayDetails_RemotePackage_NuGetFeedNoVersion()
{
CommandResult commandResult = new DotnetNewCommand(_log, "details", _nuGetPackageId)
.WithCustomHive(CreateTemporaryFolder(folderName: "Home"))
.WithWorkingDirectory(CreateTemporaryFolder())
var folder = CreateTemporaryFolder();

var createCommandResult = () => new DotnetNewCommand(_log, "details", _nuGetPackageId)
.WithCustomHive(CreateTemporaryFolder(folderName: "Home"))
.WithWorkingDirectory(folder)
.Execute();

commandResult
.Should()
.Pass();
createCommandResult().Should().Fail();

File.WriteAllText(Path.Combine(folder, "NuGet.Config"), @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
<packageSources>
<clear />
<add key=""NuGet.org"" value=""https://api.nuget.org/v3/index.json"" />
</packageSources>
</configuration>
");

var commandResult = createCommandResult();

commandResult.Should().Pass();

return Verify(commandResult.StdOut);
}
Expand Down

0 comments on commit 20ac0a6

Please sign in to comment.