Revert "[release/v7.5.7] Fix checks for local user config file paths" (#27476)

This commit is contained in:
Aditya Patwardhan authored and GitHub committed 2026-05-19 08:08:16 -07:00
1 parent 948ffc0c48
commit 3122d06b5e
9 files changed
+19 -90

No files matched your search

@@ -153,16 +153,13 @@ namespace Microsoft.PowerShell
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
{
@@ -60,12 +60,12 @@ namespace Microsoft.PowerShell
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();
@@ -167,13 +167,8 @@ namespace System.Management.Automation
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<bool> _isStaSupported = new Lazy<bool>(() =>
{
@@ -194,30 +189,6 @@ namespace System.Management.Automation
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 @@ namespace System.Management.Automation
#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 @@ namespace System.Management.Automation
_ => throw new NotSupportedException()
};
#else
return Environment.GetFolderPath(folder, Environment.SpecialFolderOption.DoNotVerify);
return Environment.GetFolderPath(folder);
#endif
}
@@ -1218,17 +1218,11 @@ namespace System.Management.Automation
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);
@@ -664,11 +664,6 @@ namespace System.Management.Automation
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 @@ namespace System.Management.Automation
cacheFileName = string.Create(CultureInfo.InvariantCulture, $"{cacheFileName}-{hashString}");
}
Platform.TryDeriveFromCache(cacheFileName, out s_cacheStoreLocation);
s_cacheStoreLocation = Path.Combine(Platform.CacheDirectory, cacheFileName);
}
}
@@ -89,10 +89,7 @@ namespace System.Management.Automation.Configuration
// 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 @@ namespace System.Management.Automation.Configuration
private T ReadValueFromFile<T>(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)
@@ -208,11 +208,10 @@ namespace System.Management.Automation
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";
@@ -1156,11 +1156,6 @@ namespace System.Management.Automation.Host
}
}
if (string.IsNullOrEmpty(baseDirectory))
{
return string.Empty;
}
if (includeDate)
{
baseDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture));
@@ -164,8 +164,6 @@ namespace Microsoft.PowerShell.Telemetry
private static readonly HashSet<string> s_knownSubsystemNames;
private static readonly string s_uuidPath;
/// <summary>Gets a value indicating whether telemetry can be sent.</summary>
public static bool CanSendTelemetry { get; private set; }
@@ -179,8 +177,7 @@ namespace Microsoft.PowerShell.Telemetry
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();