-
Notifications
You must be signed in to change notification settings - Fork 116
Fix opaque NullReferenceException when a NuGet V3 package version is missing (FD-440) #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ | |
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Calamari.Common.Plumbing.Extensions; | ||
| using NuGet.Commands; | ||
| using NuGet.Packaging.Core; | ||
| using Octopus.Versioning; | ||
|
|
||
|
|
@@ -28,17 +27,38 @@ public static void DownloadPackage(string packageId, IVersion version, Uri feedU | |
|
|
||
| using (var sourceCacheContext = new SourceCacheContext() { NoCache = true }) | ||
| { | ||
| var providers = new SourceRepositoryDependencyProvider(sourceRepository, logger, sourceCacheContext, sourceCacheContext.IgnoreFailedSources, false); | ||
| var targetPath = Directory.GetParent(targetFilePath).FullName; | ||
| if (!Directory.Exists(targetPath)) | ||
| { | ||
| Directory.CreateDirectory(targetPath); | ||
| } | ||
|
|
||
| // Resolve the downloader from FindPackageByIdResource directly rather than going through | ||
| // SourceRepositoryDependencyProvider. The provider unconditionally dereferences the downloader returned | ||
| // here, which is null when the requested version isn't on the feed, producing a bare NullReferenceException | ||
| // (FD-440). Calling the resource directly lets us null-check it and surface an actionable error. The | ||
| // provider's throttle / ignore-failed-sources behaviour isn't relevant here: Calamari passes no throttle | ||
| // and downloads from a single source. | ||
| var findPackageByIdResource = sourceRepository.GetResourceAsync<FindPackageByIdResource>(CancellationToken.None) | ||
| .GetAwaiter() | ||
| .GetResult(); | ||
|
|
||
| // GetResourceAsync returns null (rather than throwing) when no provider can supply the resource for this | ||
| // feed, so guard it explicitly — otherwise the dereference below would resurface the very | ||
| // NullReferenceException this change exists to eliminate (FD-440). | ||
| if (findPackageByIdResource == null) | ||
| throw new Exception($"The NuGet feed '{feedUri}' did not return a package lookup resource (FindPackageByIdResource). Make sure the feed URL is a valid NuGet V3 feed."); | ||
|
|
||
| var packageIdentity = new PackageIdentity(packageId, version.ToNuGetVersion()); | ||
|
|
||
| string targetTempNupkg = Path.Combine(targetPath, Path.GetRandomFileName()); | ||
| var packageDownloader = providers.GetPackageDownloaderAsync(new PackageIdentity(packageId, version.ToNuGetVersion()), sourceCacheContext, logger, CancellationToken.None) | ||
| .GetAwaiter() | ||
| .GetResult(); | ||
| var packageDownloader = findPackageByIdResource.GetPackageDownloaderAsync(packageIdentity, sourceCacheContext, logger, CancellationToken.None) | ||
| .GetAwaiter() | ||
| .GetResult(); | ||
|
Comment on lines
+42
to
+57
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change is essentially pulling the core of the method shown in this PR inline and adding the null check here - this assumes the linked PR doesn't get merged. |
||
|
|
||
| if (packageDownloader == null) | ||
| throw new Exception($"Package {packageId} version {version} was not found on the NuGet feed '{feedUri}'. Make sure the package version has been pushed to the feed."); | ||
|
|
||
| var fileCopied = packageDownloader.CopyNupkgFileToAsync(targetTempNupkg, CancellationToken.None).GetAwaiter().GetResult(); | ||
|
|
||
| if (!fileCopied) //I would expect any actual standard exception to be thrown above and not returned as a bool | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed
sourceRepository.GetResourceAsynccan return null, so we might need null handling forfindPackageByIdResourceas wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed —
GetResourceAsync<T>returns null when no provider can supply the resource for the feed (rather than throwing). Added a guard forfindPackageByIdResourcein 93a2a2c: it now throws an actionable error naming the feed, so that path no longer dereferences null and resurfaces the NRE this PR is removing.