Skip to content

Commit

Permalink
(chocolatey#1956) Ignore versioned directories in Local Repository
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AdmiringWorm committed May 20, 2022
1 parent 18d7948 commit 2745260
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ public ChocolateyLocalPackageRepository(IPackagePathResolver pathResolver, IFile
{
}

public bool IgnoreVersionedDirectories { get; set; }

public override IQueryable<IPackage> GetPackages()
{
var packages = base.GetPackages();

if (IgnoreVersionedDirectories)
{
packages = packages.Where(ExcludeVersionedDirectories).ToList().AsQueryable();
}

return packages;
}

public override IPackage FindPackage(string packageId, SemanticVersion version)
{
if (IgnoreVersionedDirectories)
{
return FindPackagesById(packageId).FirstOrDefault(package => package.Version >= version);
}

return base.FindPackage(packageId, version);
}

public override IEnumerable<IPackage> FindPackagesById(string packageId)
{
var packages = base.FindPackagesById(packageId);

if (IgnoreVersionedDirectories)
{
packages = packages.Where(ExcludeVersionedDirectories);
}

return packages;
}

public override void AddPackage(IPackage package)
{
string packageFilePath = GetPackageFilePath(package);
Expand All @@ -72,6 +108,19 @@ public override void AddPackage(IPackage package)
}
}

private bool ExcludeVersionedDirectories(IPackage package)
{
var directoryPath = PathResolver.GetInstallPath(package);
if (string.IsNullOrWhiteSpace(directoryPath))
{
return true;
}

var directoryName = Path.GetFileName(directoryPath);

return string.Compare(directoryName, package.Id + "." + package.Version, StringComparison.OrdinalIgnoreCase) != 0;
}

private string GetManifestFilePath(string packageId, SemanticVersion version)
{
string packageDirectory = PathResolver.GetPackageDirectory(packageId, version);
Expand Down
9 changes: 8 additions & 1 deletion src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -618,6 +618,13 @@ public virtual ConcurrentDictionary<string, PackageResult> upgrade_run(Chocolate
uninstallSuccessAction: null,
addUninstallHandler: false);

var localRepository = packageManager.LocalRepository as ChocolateyLocalPackageRepository;

if (localRepository != null)
{
localRepository.IgnoreVersionedDirectories = !config.AllowMultipleVersions;
}

var configIgnoreDependencies = config.IgnoreDependencies;
set_package_names_if_all_is_specified(config, () => { config.IgnoreDependencies = true; });
config.IgnoreDependencies = configIgnoreDependencies;
Expand Down

0 comments on commit 2745260

Please sign in to comment.