Skip to content

Commit 536f117

Browse files
Correct check for whether a package exists in a package folder
1 parent d37c917 commit 536f117

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/Microsoft.NuGet.Build.Tasks.Tests/NuGetTestHelpers.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ public static ResolvePackagesResult ResolvePackagesWithJsonFileContents(
5555
filesInPackages.Add(fileInPackage);
5656
}
5757
}
58+
else
59+
{
60+
// We will assume there is a location in the lock file we're using
61+
var lockFile = JObject.Parse(projectLockJsonFileContents);
62+
var firstLocation = ((JObject)lockFile["packageFolders"]).Properties().First().Name;
63+
64+
foreach (var fileInPackage in GetFakeFileNamesFromPackages(projectLockJsonFileContents, firstLocation))
65+
{
66+
filesInPackages.Add(fileInPackage);
67+
}
68+
}
5869

5970
// Don't require the packages be restored on the machine
6071
ResolveNuGetPackageAssets task = null;
61-
DirectoryExists directoryExists = path => task.GetPackageFolders().Any(l => path.StartsWith(l)) || Directory.Exists(path);
6272
FileExists fileExists = path => filesInPackages.Contains(path) || File.Exists(path);
6373

64-
task = new ResolveNuGetPackageAssets(directoryExists, fileExists, tryGetRuntimeVersion);
74+
task = new ResolveNuGetPackageAssets(fileExists, tryGetRuntimeVersion);
6575
var sw = new StringWriter();
6676
task.BuildEngine = new MockBuildEngine(sw);
6777

@@ -105,6 +115,10 @@ private static IEnumerable<string> GetFakeFileNamesFromPackages(string projectJs
105115
yield return Path.Combine(packagesDirectory, library.Name, file).Replace('/', '\\');
106116
}
107117
}
118+
119+
// Some earlier versions of NuGet didn't include the hash file in the file list, so fake that
120+
// in here.
121+
yield return Path.Combine(packagesDirectory, library.Name.Replace('/', '\\'), library.Name.Replace('/', '.') + ".nupkg.sha512");
108122
}
109123
}
110124
}

src/Microsoft.NuGet.Build.Tasks/Delegates.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Microsoft.NuGet.Build.Tasks
55
{
6-
internal delegate bool DirectoryExists(string path);
76
internal delegate bool FileExists(string path);
87
internal delegate string TryGetRuntimeVersion(string path);
98
}

src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,13 @@ public sealed class ResolveNuGetPackageAssets : Task
4949
private readonly Dictionary<string, string> _projectReferencesToOutputBasePaths = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
5050

5151
#region UnitTestSupport
52-
private readonly DirectoryExists _directoryExists = new DirectoryExists(Directory.Exists);
5352
private readonly FileExists _fileExists = new FileExists(File.Exists);
5453
private readonly TryGetRuntimeVersion _tryGetRuntimeVersion = new TryGetRuntimeVersion(TryGetRuntimeVersion);
5554
private readonly bool _reportExceptionsToMSBuildLogger = true;
5655

57-
internal ResolveNuGetPackageAssets(DirectoryExists directoryExists, FileExists fileExists, TryGetRuntimeVersion tryGetRuntimeVersion)
56+
internal ResolveNuGetPackageAssets(FileExists fileExists, TryGetRuntimeVersion tryGetRuntimeVersion)
5857
: this()
5958
{
60-
if (directoryExists != null)
61-
{
62-
_directoryExists = directoryExists;
63-
}
64-
6559
if (fileExists != null)
6660
{
6761
_fileExists = fileExists;
@@ -894,7 +888,9 @@ private string GetNuGetPackagePath(string packageId, string packageVersion)
894888
{
895889
string packagePath = Path.Combine(packagesFolder, packageId, packageVersion);
896890

897-
if (_directoryExists(packagePath))
891+
// The proper way to check if a package is available is to look for the hash file, since that's the last
892+
// file written as a part of the restore process. If it's not there, it means something failed part way through.
893+
if (_fileExists(Path.Combine(packagePath, $"{packageId}.{packageVersion}.nupkg.sha512")))
898894
{
899895
return packagePath;
900896
}

0 commit comments

Comments
 (0)