Skip to content

Commit 2745260

Browse files
committed
(chocolatey#1956) Ignore versioned directories in Local Repository
This commit updates the ChocolateyLocalPackageRepository to allow a property being set which allows our code to control whether any directories that are versioned (side by side installed) to be ignored. This is done to prevent NuGet Core throwing an error when it finds multiple package versions of the same package being installed.
1 parent 18d7948 commit 2745260

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/chocolatey/infrastructure.app/nuget/ChocolateyLocalPackageRepository.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,42 @@ public ChocolateyLocalPackageRepository(IPackagePathResolver pathResolver, IFile
4848
{
4949
}
5050

51+
public bool IgnoreVersionedDirectories { get; set; }
52+
53+
public override IQueryable<IPackage> GetPackages()
54+
{
55+
var packages = base.GetPackages();
56+
57+
if (IgnoreVersionedDirectories)
58+
{
59+
packages = packages.Where(ExcludeVersionedDirectories).ToList().AsQueryable();
60+
}
61+
62+
return packages;
63+
}
64+
65+
public override IPackage FindPackage(string packageId, SemanticVersion version)
66+
{
67+
if (IgnoreVersionedDirectories)
68+
{
69+
return FindPackagesById(packageId).FirstOrDefault(package => package.Version >= version);
70+
}
71+
72+
return base.FindPackage(packageId, version);
73+
}
74+
75+
public override IEnumerable<IPackage> FindPackagesById(string packageId)
76+
{
77+
var packages = base.FindPackagesById(packageId);
78+
79+
if (IgnoreVersionedDirectories)
80+
{
81+
packages = packages.Where(ExcludeVersionedDirectories);
82+
}
83+
84+
return packages;
85+
}
86+
5187
public override void AddPackage(IPackage package)
5288
{
5389
string packageFilePath = GetPackageFilePath(package);
@@ -72,6 +108,19 @@ public override void AddPackage(IPackage package)
72108
}
73109
}
74110

111+
private bool ExcludeVersionedDirectories(IPackage package)
112+
{
113+
var directoryPath = PathResolver.GetInstallPath(package);
114+
if (string.IsNullOrWhiteSpace(directoryPath))
115+
{
116+
return true;
117+
}
118+
119+
var directoryName = Path.GetFileName(directoryPath);
120+
121+
return string.Compare(directoryName, package.Id + "." + package.Version, StringComparison.OrdinalIgnoreCase) != 0;
122+
}
123+
75124
private string GetManifestFilePath(string packageId, SemanticVersion version)
76125
{
77126
string packageDirectory = PathResolver.GetPackageDirectory(packageId, version);

src/chocolatey/infrastructure.app/services/NugetService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright © 2017 - 2021 Chocolatey Software, Inc
1+
// Copyright © 2017 - 2021 Chocolatey Software, Inc
22
// Copyright © 2011 - 2017 RealDimensions Software, LLC
33
//
44
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -618,6 +618,13 @@ public virtual ConcurrentDictionary<string, PackageResult> upgrade_run(Chocolate
618618
uninstallSuccessAction: null,
619619
addUninstallHandler: false);
620620

621+
var localRepository = packageManager.LocalRepository as ChocolateyLocalPackageRepository;
622+
623+
if (localRepository != null)
624+
{
625+
localRepository.IgnoreVersionedDirectories = !config.AllowMultipleVersions;
626+
}
627+
621628
var configIgnoreDependencies = config.IgnoreDependencies;
622629
set_package_names_if_all_is_specified(config, () => { config.IgnoreDependencies = true; });
623630
config.IgnoreDependencies = configIgnoreDependencies;

0 commit comments

Comments
 (0)