Skip to content

Commit 7ffe7d4

Browse files
authored
Clean up error experience when downloading non-tools (#43045)
2 parents dfaa9c5 + aed512a commit 7ffe7d4

20 files changed

+130
-14
lines changed

src/Cli/dotnet/NugetPackageDownloader/INuGetPackageDownloader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ Task<string> DownloadPackageAsync(PackageId packageId,
1616
bool includePreview = false,
1717
bool? includeUnlisted = null,
1818
DirectoryPath? downloadFolder = null,
19-
PackageSourceMapping packageSourceMapping = null);
19+
PackageSourceMapping packageSourceMapping = null,
20+
bool isTool = false);
2021

2122
Task<string> GetPackageUrl(PackageId packageId,
2223
NuGetVersion packageVersion = null,

src/Cli/dotnet/NugetPackageDownloader/LocalizableStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@
126126
<data name="IsNotFoundInNuGetFeeds" xml:space="preserve">
127127
<value>{0} is not found in NuGet feeds {1}.</value>
128128
</data>
129+
<data name="NotATool" xml:space="preserve">
130+
<value>Package {0} is not a .NET tool.</value>
131+
</data>
129132
<data name="DownloadVersionFailed" xml:space="preserve">
130133
<value>Downloading {0} version {1} failed.</value>
131134
</data>

src/Cli/dotnet/NugetPackageDownloader/NuGetPackageDownloader.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,45 @@ public async Task<string> DownloadPackageAsync(PackageId packageId,
8383
bool includePreview = false,
8484
bool? includeUnlisted = null,
8585
DirectoryPath? downloadFolder = null,
86-
PackageSourceMapping packageSourceMapping = null)
86+
PackageSourceMapping packageSourceMapping = null,
87+
bool isTool = false)
8788
{
8889
CancellationToken cancellationToken = CancellationToken.None;
8990

9091
(var source, var resolvedPackageVersion) = await GetPackageSourceAndVersion(packageId, packageVersion,
9192
packageSourceLocation, includePreview, includeUnlisted ?? packageVersion is not null, packageSourceMapping).ConfigureAwait(false);
9293

93-
FindPackageByIdResource resource = null;
9494
SourceRepository repository = GetSourceRepository(source);
9595

96-
resource = await repository.GetResourceAsync<FindPackageByIdResource>(cancellationToken)
97-
.ConfigureAwait(false);
96+
// TODO: Fix this to use the PackageSearchResourceV3 once https://github.com/NuGet/NuGet.Client/pull/5991 is completed.
97+
if (isTool && await repository.GetResourceAsync<ServiceIndexResourceV3>().ConfigureAwait(false) is ServiceIndexResourceV3 serviceIndex)
98+
{
99+
// See https://learn.microsoft.com/en-us/nuget/api/package-base-address-resource#download-package-manifest-nuspec
100+
var uri = serviceIndex.GetServiceEntries("PackageBaseAddress/3.0.0")[0].Uri;
101+
var queryUri = uri + $"{packageId}/{packageVersion}/{packageId}.nuspec";
102+
103+
using HttpClient client = new(new HttpClientHandler() { CheckCertificateRevocationList = true });
104+
using HttpResponseMessage response = await client.GetAsync(queryUri).ConfigureAwait(false);
105+
106+
if (response.IsSuccessStatusCode)
107+
{
108+
string nuspec = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
109+
110+
XDocument doc = XDocument.Parse(nuspec);
111+
112+
if (!ToolPackageInstance.IsToolPackage(doc))
113+
{
114+
throw new GracefulException(string.Format(LocalizableStrings.NotATool, packageId));
115+
}
116+
}
117+
else
118+
{
119+
throw new GracefulException(string.Format(LocalizableStrings.NotATool, packageId));
120+
}
121+
}
98122

123+
FindPackageByIdResource resource = await repository.GetResourceAsync<FindPackageByIdResource>(cancellationToken)
124+
.ConfigureAwait(false);
99125
if (resource == null)
100126
{
101127
throw new NuGetPackageNotFoundException(

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.cs.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.de.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.es.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.fr.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.it.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.ja.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/NugetPackageDownloader/xlf/LocalizableStrings.ko.xlf

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)