From f6d797ec98f8256b5da893f7071fccd2008e056a Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Thu, 22 Jun 2023 13:28:26 +0100 Subject: [PATCH] (#2630) Re-instate setting of config properties Previously, in this commit: https://github.com/chocolatey/choco/commit/da19356578e3c90850ac766f150ccebf809c6ce2#diff-cb6a0471e41268b22a928bd57a59d51b70b7024e9beb30e89a330e193a089eba The usage of the top level CacheLocation and CommandExecutionTimeoutSeconds values had been removed, since these top level properties within the chocolatey.config had been replaced with values contained within the config section of the chocolatey.config file. However, the changes in that commit were too aggressive, and removed the call to the set_config_item (which has subsequently been renamed to SetConfigItem). The method call does the work of taking any value that is defined in the chocolatey.config for a given property name, and adding it to the ChocolateyConfiguration instance. When this method call was removed, it stopped setting the property value on the instance, and as a result, values that had been configured in the chocolatey.config file were ignored. They were still in play when the configuration was passed in via a command line option, but not when defining them in the chocolatey.config file. The removal of this call to the set_config_item method also explains why it was necessary to apply this bug fix in Chocolatey GUI: https://github.com/chocolatey/ChocolateyGUI/issues/1003 The method call also had the effect of setting the description on the configuration value, which would have meant that Chocolatey GUI wouldn't have thrown a null reference exception. The change in Chocolatey GUI is still valid though, as there are times when a config value can have a missing description, so it makes sense to leave that fix in place. --- .../builders/ConfigurationBuilder.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index 4925610996..eb9d3afb87 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -217,6 +217,15 @@ private static void SetMachineSources(ChocolateyConfiguration config, ConfigFile private static void SetAllConfigItems(ChocolateyConfiguration config, ConfigFileSettings configFileSettings, IFileSystem fileSystem) { + config.CacheLocation = Environment.ExpandEnvironmentVariables( + SetConfigItem( + ApplicationParameters.ConfigSettings.CacheLocation, + configFileSettings, + string.Empty, + "Cache location if not TEMP folder. Replaces `$env:TEMP` value for choco.exe process. It is highly recommended this be set to make Chocolatey more deterministic in cleanup." + ) + ); + if (string.IsNullOrWhiteSpace(config.CacheLocation)) { config.CacheLocation = fileSystem.GetTempPath(); // System.Environment.GetEnvironmentVariable("TEMP"); @@ -233,10 +242,12 @@ private static void SetAllConfigItems(ChocolateyConfiguration config, ConfigFile if (string.IsNullOrWhiteSpace(config.CacheLocation)) config.CacheLocation = fileSystem.CombinePaths(ApplicationParameters.InstallLocation, "temp"); var commandExecutionTimeoutSeconds = 0; - var commandExecutionTimeout = configFileSettings.ConfigSettings - .Where(f => f.Key.IsEqualTo(ApplicationParameters.ConfigSettings.CommandExecutionTimeoutSeconds)) - .Select(c => c.Value) - .FirstOrDefault(); + var commandExecutionTimeout = SetConfigItem( + ApplicationParameters.ConfigSettings.CommandExecutionTimeoutSeconds, + configFileSettings, + ApplicationParameters.DefaultWaitForExitInSeconds.ToStringSafe(), + "Default timeout for command execution. '0' for infinite." + ); int.TryParse(commandExecutionTimeout, out commandExecutionTimeoutSeconds); config.CommandExecutionTimeoutSeconds = commandExecutionTimeoutSeconds;