From ff464f7dc1745a8eff1e1fab020f461bf341a129 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Sun, 5 Dec 2021 00:18:34 -0600 Subject: [PATCH] (#1443) Reset config via action after every package upgrade This adds a extra parameter to the upgrade_run method, which is used to pass an action which resets the configuration in the function that calls upgrade_run. This fixes a bug when useRememberedArguments is enabled that causes arguments to be reused in the chocolateyInstall.ps1 when multiple packages are upgraded. It happens because OptionSet.parse() sets the configuration via the actions specified in the UpgradeCommand optionSet setup (and likewise in configuration builder as well). It only sets options that are saved, so if a later package does not have an option, it is not set, and the previous option is reused. This fixes the bug because it resets the configuration at the ChocolateyPackageService level, which is enough to reset the configuration for action that runs the PowerShell as well. --- .../services/ChocolateyPackageService.cs | 4 +++- .../infrastructure.app/services/CygwinService.cs | 2 +- .../infrastructure.app/services/ISourceRunner.cs | 3 ++- .../infrastructure.app/services/NugetService.cs | 11 +++++++---- .../infrastructure.app/services/PythonService.cs | 2 +- .../infrastructure.app/services/RubyGemsService.cs | 2 +- .../infrastructure.app/services/WebPiService.cs | 2 +- .../services/WindowsFeatureService.cs | 2 +- 8 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index f46986f461..448640785b 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -830,6 +830,8 @@ public virtual ConcurrentDictionary upgrade_run(Chocolate try { + var resetConfigAction = new Action(newConfig => config = newConfig.deep_copy()); + Action action = null; if (config.SourceType == SourceType.normal) { @@ -839,7 +841,7 @@ public virtual ConcurrentDictionary upgrade_run(Chocolate get_environment_before(config, allowLogging: true); var beforeUpgradeAction = new Action(packageResult => before_package_modify(packageResult, config)); - var results = perform_source_runner_function(config, r => r.upgrade_run(config, action, beforeUpgradeAction)); + var results = perform_source_runner_function(config, r => r.upgrade_run(config, action, resetConfigAction, beforeUpgradeAction)); foreach (var result in results) { diff --git a/src/chocolatey/infrastructure.app/services/CygwinService.cs b/src/chocolatey/infrastructure.app/services/CygwinService.cs index d3d6fb3ef7..eaf9ca6fed 100644 --- a/src/chocolatey/infrastructure.app/services/CygwinService.cs +++ b/src/chocolatey/infrastructure.app/services/CygwinService.cs @@ -267,7 +267,7 @@ public ConcurrentDictionary upgrade_noop(ChocolateyConfig return new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); } - public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) + public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null) { throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME)); } diff --git a/src/chocolatey/infrastructure.app/services/ISourceRunner.cs b/src/chocolatey/infrastructure.app/services/ISourceRunner.cs index 1950bda94a..ed10879269 100644 --- a/src/chocolatey/infrastructure.app/services/ISourceRunner.cs +++ b/src/chocolatey/infrastructure.app/services/ISourceRunner.cs @@ -87,9 +87,10 @@ public interface ISourceRunner /// /// The configuration. /// The action to continue with when upgrade is successful. + /// The action that can be used to reset the configuration in the caller /// The action (if any) to run on any currently installed package before triggering the upgrade. /// results of installs - ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null); + ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null); /// /// Run uninstall in noop mode diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 2864765bea..26208cf271 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -586,15 +586,16 @@ public virtual void remove_rollback_directory_if_exists(string packageName) public ConcurrentDictionary upgrade_noop(ChocolateyConfiguration config, Action continueAction) { config.Force = false; - return upgrade_run(config, continueAction, performAction: false); + var resetConfigAction = new Action(newConfig => { }); + return upgrade_run(config, continueAction, false, resetConfigAction); } - public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) + public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null) { - return upgrade_run(config, continueAction, performAction: true, beforeUpgradeAction: beforeUpgradeAction); + return upgrade_run(config, continueAction, true, resetConfigAction, beforeUpgradeAction); } - public virtual ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, bool performAction, Action beforeUpgradeAction = null) + public virtual ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, bool performAction, Action resetConfigAction, Action beforeUpgradeAction = null) { _fileSystem.create_directory_if_not_exists(ApplicationParameters.PackagesLocation); var packageInstalls = new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); @@ -877,6 +878,7 @@ public virtual ConcurrentDictionary upgrade_run(Chocolate } } } + resetConfigAction.Invoke(originalConfig); } return packageInstalls; @@ -951,6 +953,7 @@ public virtual ConcurrentDictionary get_outdated(Chocolat packageResult.Messages.Add(new ResultMessage(ResultType.Note, logMessage)); this.Log().Info("{0}|{1}|{2}|{3}".format_with(installedPackage.Id, installedPackage.Version, latestPackage.Version, isPinned.to_string().to_lower())); + } return outdatedPackages; diff --git a/src/chocolatey/infrastructure.app/services/PythonService.cs b/src/chocolatey/infrastructure.app/services/PythonService.cs index 972354c10c..768e8faa54 100644 --- a/src/chocolatey/infrastructure.app/services/PythonService.cs +++ b/src/chocolatey/infrastructure.app/services/PythonService.cs @@ -411,7 +411,7 @@ public ConcurrentDictionary upgrade_noop(ChocolateyConfig return new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); } - public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) + public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null) { if (config.PackageNames.is_equal_to(ApplicationParameters.AllPackages)) { diff --git a/src/chocolatey/infrastructure.app/services/RubyGemsService.cs b/src/chocolatey/infrastructure.app/services/RubyGemsService.cs index 67a62df642..d5ea41a08c 100644 --- a/src/chocolatey/infrastructure.app/services/RubyGemsService.cs +++ b/src/chocolatey/infrastructure.app/services/RubyGemsService.cs @@ -265,7 +265,7 @@ public ConcurrentDictionary upgrade_noop(ChocolateyConfig return new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); } - public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) + public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null) { throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME)); } diff --git a/src/chocolatey/infrastructure.app/services/WebPiService.cs b/src/chocolatey/infrastructure.app/services/WebPiService.cs index 6c36381614..cd322201a5 100644 --- a/src/chocolatey/infrastructure.app/services/WebPiService.cs +++ b/src/chocolatey/infrastructure.app/services/WebPiService.cs @@ -258,7 +258,7 @@ public ConcurrentDictionary upgrade_noop(ChocolateyConfig return new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); } - public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) + public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null) { throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME)); } diff --git a/src/chocolatey/infrastructure.app/services/WindowsFeatureService.cs b/src/chocolatey/infrastructure.app/services/WindowsFeatureService.cs index 1011ff8824..e664245cad 100644 --- a/src/chocolatey/infrastructure.app/services/WindowsFeatureService.cs +++ b/src/chocolatey/infrastructure.app/services/WindowsFeatureService.cs @@ -328,7 +328,7 @@ public ConcurrentDictionary upgrade_noop(ChocolateyConfig return new ConcurrentDictionary(StringComparer.InvariantCultureIgnoreCase); } - public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action beforeUpgradeAction = null) + public ConcurrentDictionary upgrade_run(ChocolateyConfiguration config, Action continueAction, Action resetConfigAction, Action beforeUpgradeAction = null) { set_executable_path_if_not_set(); throw new NotImplementedException("{0} does not implement upgrade".format_with(APP_NAME));