From 71f91a1ddb28b60a545ec3e9ce7f277da4e1b53f Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 18 May 2026 18:05:56 -0700 Subject: [PATCH] Revert "[release/v7.5.7] Fix checks for local user config file paths (#27459)" This reverts commit d94e21159a2eb670dbbbd04d6acdc747592db912. --- .../host/msh/ConsoleHost.cs | 13 ++---- .../host/msh/UpdatesNotification.cs | 4 +- .../CoreCLR/CorePsPlatform.cs | 46 ++----------------- .../engine/CommandDiscovery.cs | 10 +--- .../engine/Modules/AnalysisCache.cs | 7 +-- .../engine/PSConfiguration.cs | 10 +--- .../engine/hostifaces/HostUtilities.cs | 9 ++-- .../engine/hostifaces/MshHostUserInterface.cs | 5 -- .../utils/Telemetry.cs | 5 +- 9 files changed, 19 insertions(+), 90 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index f454ab51e1d..8fc815f59e6 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -153,16 +153,13 @@ internal static int Start( try { string profileDir = Platform.CacheDirectory; - if (!string.IsNullOrEmpty(profileDir)) - { #if !UNIX - if (!Directory.Exists(profileDir)) - { - Directory.CreateDirectory(profileDir); - } -#endif - ProfileOptimization.SetProfileRoot(profileDir); + if (!Directory.Exists(profileDir)) + { + Directory.CreateDirectory(profileDir); } +#endif + ProfileOptimization.SetProfileRoot(profileDir); } catch { diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/UpdatesNotification.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/UpdatesNotification.cs index eb4557c04d2..d0b1ed4572c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/UpdatesNotification.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/UpdatesNotification.cs @@ -60,12 +60,12 @@ internal static class UpdatesNotification static UpdatesNotification() { s_notificationType = GetNotificationType(); - CanNotifyUpdates = s_notificationType != NotificationType.Off - && Platform.TryDeriveFromCache(PSVersionInfo.GitCommitId, out s_cacheDirectory); + CanNotifyUpdates = s_notificationType != NotificationType.Off; if (CanNotifyUpdates) { s_enumOptions = new EnumerationOptions(); + s_cacheDirectory = Path.Combine(Platform.CacheDirectory, PSVersionInfo.GitCommitId); // Build the template/pattern strings for the configured notification type. string typeNum = ((int)s_notificationType).ToString(); diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 530493f320a..dc5db5f2c48 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -167,13 +167,8 @@ public static bool IsStaSupported internal static readonly string ConfigDirectory = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG); #else // Gets the location for cache and config folders. - internal static readonly string CacheDirectory = SafeDeriveFromSpecialFolder( - Environment.SpecialFolder.LocalApplicationData, - @"Microsoft\PowerShell"); - - internal static readonly string ConfigDirectory = SafeDeriveFromSpecialFolder( - Environment.SpecialFolder.Personal, - @"PowerShell"); + internal static readonly string CacheDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerShell"; + internal static readonly string ConfigDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\PowerShell"; private static readonly Lazy _isStaSupported = new Lazy(() => { @@ -194,30 +189,6 @@ public static bool IsStaSupported private static bool? _isWindowsDesktop = null; #endif - internal static bool TryDeriveFromCache(string path1, out string result) - { - if (CacheDirectory is null or []) - { - result = null; - return false; - } - - result = Path.Combine(CacheDirectory, path1); - return true; - } - - internal static bool TryDeriveFromCache(string path1, string path2, out string result) - { - if (CacheDirectory is null or []) - { - result = null; - return false; - } - - result = Path.Combine(CacheDirectory, path1, path2); - return true; - } - // format files internal static readonly string[] FormatFileNames = new string[] { @@ -247,17 +218,6 @@ internal static class CommonEnvVariableNames #endif } - private static string SafeDeriveFromSpecialFolder(Environment.SpecialFolder specialFolder, string subPath) - { - string basePath = Environment.GetFolderPath(specialFolder, Environment.SpecialFolderOption.DoNotVerify); - if (string.IsNullOrWhiteSpace(basePath)) - { - return string.Empty; - } - - return Path.Join(basePath, subPath); - } - #if UNIX private static string s_tempHome = null; @@ -400,7 +360,7 @@ internal static string GetFolderPath(Environment.SpecialFolder folder) _ => throw new NotSupportedException() }; #else - return Environment.GetFolderPath(folder, Environment.SpecialFolderOption.DoNotVerify); + return Environment.GetFolderPath(folder); #endif } diff --git a/src/System.Management.Automation/engine/CommandDiscovery.cs b/src/System.Management.Automation/engine/CommandDiscovery.cs index e07520a1238..561a33ccba8 100644 --- a/src/System.Management.Automation/engine/CommandDiscovery.cs +++ b/src/System.Management.Automation/engine/CommandDiscovery.cs @@ -1218,17 +1218,11 @@ internal LookupPathCollection GetLookupDirectoryPaths() string tempDir = directory.TrimStart(); if (tempDir.EqualsOrdinalIgnoreCase("~")) { - tempDir = Environment.GetFolderPath( - Environment.SpecialFolder.UserProfile, - Environment.SpecialFolderOption.DoNotVerify); + tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); } else if (tempDir.StartsWith("~" + Path.DirectorySeparatorChar)) { - tempDir = Environment.GetFolderPath( - Environment.SpecialFolder.UserProfile, - Environment.SpecialFolderOption.DoNotVerify) - + Path.DirectorySeparatorChar - + tempDir.Substring(2); + tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + Path.DirectorySeparatorChar + tempDir.Substring(2); } _cachedPath.Add(tempDir); diff --git a/src/System.Management.Automation/engine/Modules/AnalysisCache.cs b/src/System.Management.Automation/engine/Modules/AnalysisCache.cs index 39d9b586aa6..a701b0745c8 100644 --- a/src/System.Management.Automation/engine/Modules/AnalysisCache.cs +++ b/src/System.Management.Automation/engine/Modules/AnalysisCache.cs @@ -664,11 +664,6 @@ private static byte[] GetHeader() public void QueueSerialization() { - if (string.IsNullOrEmpty(s_cacheStoreLocation)) - { - return; - } - // We expect many modules to rapidly call for serialization. // Instead of doing it right away, we'll queue a task that starts writing // after it seems like we've stopped adding stuff to write out. This is @@ -1126,7 +1121,7 @@ static AnalysisCacheData() cacheFileName = string.Create(CultureInfo.InvariantCulture, $"{cacheFileName}-{hashString}"); } - Platform.TryDeriveFromCache(cacheFileName, out s_cacheStoreLocation); + s_cacheStoreLocation = Path.Combine(Platform.CacheDirectory, cacheFileName); } } diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index 419a4cae95f..e321423f768 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -89,10 +89,7 @@ private PowerShellConfig() // Note: This directory may or may not exist depending upon the execution scenario. // Writes will attempt to create the directory if it does not already exist. perUserConfigDirectory = Platform.ConfigDirectory; - if (!string.IsNullOrEmpty(perUserConfigDirectory)) - { - perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName); - } + perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName); emptyConfig = new JObject(); configRoots = new JObject[2]; @@ -390,11 +387,6 @@ internal PSKeyword GetLogKeywords() private T ReadValueFromFile(ConfigScope scope, string key, T defaultValue = default) { string fileName = GetConfigFilePath(scope); - if (string.IsNullOrEmpty(fileName)) - { - return defaultValue; - } - JObject configData = configRoots[(int)scope]; if (configData == null) diff --git a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs index caf3c5e15d8..003625791b1 100644 --- a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs +++ b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs @@ -208,11 +208,10 @@ internal static string GetFullProfileFileName(string shellId, bool forCurrentUse else { basePath = GetAllUsersFolderPath(shellId); - } - - if (string.IsNullOrEmpty(basePath)) - { - return string.Empty; + if (string.IsNullOrEmpty(basePath)) + { + return string.Empty; + } } string profileName = useTestProfile ? "profile_test.ps1" : "profile.ps1"; diff --git a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs index 5f51ba15751..29fc5fa1f7f 100644 --- a/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs @@ -1156,11 +1156,6 @@ internal static string GetTranscriptPath(string baseDirectory, bool includeDate) } } - if (string.IsNullOrEmpty(baseDirectory)) - { - return string.Empty; - } - if (includeDate) { baseDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture)); diff --git a/src/System.Management.Automation/utils/Telemetry.cs b/src/System.Management.Automation/utils/Telemetry.cs index 02851f26f9f..cffedb7c579 100644 --- a/src/System.Management.Automation/utils/Telemetry.cs +++ b/src/System.Management.Automation/utils/Telemetry.cs @@ -164,8 +164,6 @@ public static class ApplicationInsightsTelemetry private static readonly HashSet s_knownSubsystemNames; - private static readonly string s_uuidPath; - /// Gets a value indicating whether telemetry can be sent. public static bool CanSendTelemetry { get; private set; } @@ -179,8 +177,7 @@ public static class ApplicationInsightsTelemetry static ApplicationInsightsTelemetry() { // If we can't send telemetry, there's no reason to do any of this - CanSendTelemetry = !GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false) - && Platform.TryDeriveFromCache("telemetry.uuid", out s_uuidPath); + CanSendTelemetry = !GetEnvironmentVariableAsBool(name: _telemetryOptoutEnvVar, defaultValue: false); if (CanSendTelemetry) { s_sessionId = Guid.NewGuid().ToString();