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;