Skip to content

Commit d648dd1

Browse files
[release/v7.5.7] Fix checks for local user config file paths (#27479)
1 parent 3122d06 commit d648dd1

9 files changed

Lines changed: 552 additions & 478 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,16 @@ internal static int Start(
153153
try
154154
{
155155
string profileDir = Platform.CacheDirectory;
156-
#if !UNIX
157-
if (!Directory.Exists(profileDir))
156+
if (!string.IsNullOrEmpty(profileDir))
158157
{
159-
Directory.CreateDirectory(profileDir);
160-
}
158+
#if !UNIX
159+
if (!Directory.Exists(profileDir))
160+
{
161+
Directory.CreateDirectory(profileDir);
162+
}
161163
#endif
162-
ProfileOptimization.SetProfileRoot(profileDir);
164+
ProfileOptimization.SetProfileRoot(profileDir);
165+
}
163166
}
164167
catch
165168
{

src/Microsoft.PowerShell.ConsoleHost/host/msh/UpdatesNotification.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ internal static class UpdatesNotification
6060
static UpdatesNotification()
6161
{
6262
s_notificationType = GetNotificationType();
63-
CanNotifyUpdates = s_notificationType != NotificationType.Off;
63+
CanNotifyUpdates = s_notificationType != NotificationType.Off
64+
&& Platform.TryDeriveFromCache(PSVersionInfo.GitCommitId, out s_cacheDirectory);
6465

6566
if (CanNotifyUpdates)
6667
{
6768
s_enumOptions = new EnumerationOptions();
68-
s_cacheDirectory = Path.Combine(Platform.CacheDirectory, PSVersionInfo.GitCommitId);
6969

7070
// Build the template/pattern strings for the configured notification type.
7171
string typeNum = ((int)s_notificationType).ToString();

src/System.Management.Automation/CoreCLR/CorePsPlatform.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,13 @@ public static bool IsStaSupported
167167
internal static readonly string ConfigDirectory = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CONFIG);
168168
#else
169169
// Gets the location for cache and config folders.
170-
internal static readonly string CacheDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerShell";
171-
internal static readonly string ConfigDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\PowerShell";
170+
internal static readonly string CacheDirectory = SafeDeriveFromSpecialFolder(
171+
Environment.SpecialFolder.LocalApplicationData,
172+
@"Microsoft\PowerShell");
173+
174+
internal static readonly string ConfigDirectory = SafeDeriveFromSpecialFolder(
175+
Environment.SpecialFolder.Personal,
176+
@"PowerShell");
172177

173178
private static readonly Lazy<bool> _isStaSupported = new Lazy<bool>(() =>
174179
{
@@ -189,6 +194,30 @@ public static bool IsStaSupported
189194
private static bool? _isWindowsDesktop = null;
190195
#endif
191196

197+
internal static bool TryDeriveFromCache(string path1, out string result)
198+
{
199+
if (CacheDirectory is null or [])
200+
{
201+
result = null;
202+
return false;
203+
}
204+
205+
result = Path.Combine(CacheDirectory, path1);
206+
return true;
207+
}
208+
209+
internal static bool TryDeriveFromCache(string path1, string path2, out string result)
210+
{
211+
if (CacheDirectory is null or [])
212+
{
213+
result = null;
214+
return false;
215+
}
216+
217+
result = Path.Combine(CacheDirectory, path1, path2);
218+
return true;
219+
}
220+
192221
// format files
193222
internal static readonly string[] FormatFileNames = new string[]
194223
{
@@ -218,6 +247,17 @@ internal static class CommonEnvVariableNames
218247
#endif
219248
}
220249

250+
private static string SafeDeriveFromSpecialFolder(Environment.SpecialFolder specialFolder, string subPath)
251+
{
252+
string basePath = Environment.GetFolderPath(specialFolder, Environment.SpecialFolderOption.DoNotVerify);
253+
if (string.IsNullOrWhiteSpace(basePath))
254+
{
255+
return string.Empty;
256+
}
257+
258+
return Path.Join(basePath, subPath);
259+
}
260+
221261
#if UNIX
222262
private static string s_tempHome = null;
223263

@@ -360,7 +400,7 @@ internal static string GetFolderPath(Environment.SpecialFolder folder)
360400
_ => throw new NotSupportedException()
361401
};
362402
#else
363-
return Environment.GetFolderPath(folder);
403+
return Environment.GetFolderPath(folder, Environment.SpecialFolderOption.DoNotVerify);
364404
#endif
365405
}
366406

src/System.Management.Automation/engine/CommandDiscovery.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,11 +1218,17 @@ internal LookupPathCollection GetLookupDirectoryPaths()
12181218
string tempDir = directory.TrimStart();
12191219
if (tempDir.EqualsOrdinalIgnoreCase("~"))
12201220
{
1221-
tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
1221+
tempDir = Environment.GetFolderPath(
1222+
Environment.SpecialFolder.UserProfile,
1223+
Environment.SpecialFolderOption.DoNotVerify);
12221224
}
12231225
else if (tempDir.StartsWith("~" + Path.DirectorySeparatorChar))
12241226
{
1225-
tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + Path.DirectorySeparatorChar + tempDir.Substring(2);
1227+
tempDir = Environment.GetFolderPath(
1228+
Environment.SpecialFolder.UserProfile,
1229+
Environment.SpecialFolderOption.DoNotVerify)
1230+
+ Path.DirectorySeparatorChar
1231+
+ tempDir.Substring(2);
12261232
}
12271233

12281234
_cachedPath.Add(tempDir);

src/System.Management.Automation/engine/Modules/AnalysisCache.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,11 @@ private static byte[] GetHeader()
664664

665665
public void QueueSerialization()
666666
{
667+
if (string.IsNullOrEmpty(s_cacheStoreLocation))
668+
{
669+
return;
670+
}
671+
667672
// We expect many modules to rapidly call for serialization.
668673
// Instead of doing it right away, we'll queue a task that starts writing
669674
// after it seems like we've stopped adding stuff to write out. This is
@@ -1121,7 +1126,7 @@ static AnalysisCacheData()
11211126
cacheFileName = string.Create(CultureInfo.InvariantCulture, $"{cacheFileName}-{hashString}");
11221127
}
11231128

1124-
s_cacheStoreLocation = Path.Combine(Platform.CacheDirectory, cacheFileName);
1129+
Platform.TryDeriveFromCache(cacheFileName, out s_cacheStoreLocation);
11251130
}
11261131
}
11271132

src/System.Management.Automation/engine/PSConfiguration.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ private PowerShellConfig()
8989
// Note: This directory may or may not exist depending upon the execution scenario.
9090
// Writes will attempt to create the directory if it does not already exist.
9191
perUserConfigDirectory = Platform.ConfigDirectory;
92-
perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName);
92+
if (!string.IsNullOrEmpty(perUserConfigDirectory))
93+
{
94+
perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName);
95+
}
9396

9497
emptyConfig = new JObject();
9598
configRoots = new JObject[2];
@@ -387,6 +390,11 @@ internal PSKeyword GetLogKeywords()
387390
private T ReadValueFromFile<T>(ConfigScope scope, string key, T defaultValue = default)
388391
{
389392
string fileName = GetConfigFilePath(scope);
393+
if (string.IsNullOrEmpty(fileName))
394+
{
395+
return defaultValue;
396+
}
397+
390398
JObject configData = configRoots[(int)scope];
391399

392400
if (configData == null)

src/System.Management.Automation/engine/hostifaces/HostUtilities.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ internal static string GetFullProfileFileName(string shellId, bool forCurrentUse
208208
else
209209
{
210210
basePath = GetAllUsersFolderPath(shellId);
211-
if (string.IsNullOrEmpty(basePath))
212-
{
213-
return string.Empty;
214-
}
211+
}
212+
213+
if (string.IsNullOrEmpty(basePath))
214+
{
215+
return string.Empty;
215216
}
216217

217218
string profileName = useTestProfile ? "profile_test.ps1" : "profile.ps1";

src/System.Management.Automation/engine/hostifaces/MshHostUserInterface.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,11 @@ internal static string GetTranscriptPath(string baseDirectory, bool includeDate)
11561156
}
11571157
}
11581158

1159+
if (string.IsNullOrEmpty(baseDirectory))
1160+
{
1161+
return string.Empty;
1162+
}
1163+
11591164
if (includeDate)
11601165
{
11611166
baseDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture));

0 commit comments

Comments
 (0)