From 3516bd4154b09592074e8195408a16cc5a601120 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Tue, 12 Mar 2019 12:00:59 +0000 Subject: [PATCH 1/2] (GH-1689) Delete packaging scripts before upgrade It is possible, due to the way that Package Reducer works, that there are extra files left in the package directory when doing an upgrade of a package. This can cause a package upgrade to fail. Prior to performing the upgrade, delete all packaging scripts (chocolateyInstall, chocolateyUninstall, and chocolateyBeforeModify) from the package folder, these will then be replaced during the upgrade, if they are still required.. --- .../services/NugetService.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 63159fd59d..b38ea3fc59 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -1146,6 +1146,8 @@ public virtual void backup_existing_version(ChocolateyConfiguration config, IPac try { _fileSystem.copy_directory(backupLocation, pkgInstallPath, overwriteExisting: true); + + remove_packaging_files_prior_to_upgrade(pkgInstallPath, config.CommandName); } catch (Exception ex) { @@ -1167,6 +1169,29 @@ process locking the folder or files. Please make sure nothing is } } + public virtual void remove_packaging_files_prior_to_upgrade(string directoryPath, string commandName) + { + if (commandName.to_lower() == "upgrade") + { + // Due to the way that Package Reducer works, there is a potential that a Chocolatey Packaging + // script could be incorrectly left in place during an upgrade operation. To guard against this, + // remove any Chocolatey Packaging scripts, which will then be restored by the new package, if + // they are still required + var filesToDelete = new List {"chocolateyinstall", "chocolateyuninstall", "chocolateybeforemodify"}; + var packagingScripts = _fileSystem.get_files(directoryPath, "*.ps1", SearchOption.AllDirectories) + .Where(p => filesToDelete.Contains(_fileSystem.get_file_name_without_extension(p).to_lower())); + + foreach (var packagingScript in packagingScripts) + { + if (_fileSystem.file_exists(packagingScript)) + { + this.Log().Debug("Deleting file {0}".format_with(packagingScript)); + _fileSystem.delete_file(packagingScript); + } + } + } + } + public virtual void backup_changed_files(string packageInstallPath, ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo) { if (packageInfo == null || packageInfo.Package == null) return; From 57f966ff5b51cf43a56e9aa56f844384326ae0a1 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Wed, 13 Mar 2019 09:30:06 +0000 Subject: [PATCH 2/2] (maint) Corrected whitespace --- src/chocolatey/infrastructure.app/services/NugetService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index b38ea3fc59..ae4d49a47e 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -899,7 +899,7 @@ public virtual ConcurrentDictionary get_outdated(Chocolat var pkgInfo = _packageInfoService.get_package_information(installedPackage); bool isPinned = pkgInfo.IsPinned; - // if the package is pinned and we are skipping pinned, + // if the package is pinned and we are skipping pinned, // move on quickly if (isPinned && config.OutdatedCommand.IgnorePinned) { @@ -918,7 +918,7 @@ public virtual ConcurrentDictionary get_outdated(Chocolat } var latestPackage = find_package(packageName, null, config, repository); - + if (latestPackage == null) { if (config.Features.IgnoreUnfoundPackagesOnUpgradeOutdated) continue; @@ -933,7 +933,7 @@ public virtual ConcurrentDictionary get_outdated(Chocolat } if (latestPackage.Version <= installedPackage.Version) continue; - + var packageResult = outdatedPackages.GetOrAdd(packageName, new PackageResult(latestPackage, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, latestPackage.Id))); string logMessage = "You have {0} v{1} installed. Version {2} is available based on your source(s).{3} Source(s): \"{4}\"".format_with(installedPackage.Id, installedPackage.Version, latestPackage.Version, Environment.NewLine, config.Sources);