Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ internal static int Start(
try
{
string profileDir = Platform.CacheDirectory;
#if !UNIX
if (!Directory.Exists(profileDir))
if (!string.IsNullOrEmpty(profileDir))
{
Directory.CreateDirectory(profileDir);
}
#if !UNIX
if (!Directory.Exists(profileDir))
{
Directory.CreateDirectory(profileDir);
}
#endif
ProfileOptimization.SetProfileRoot(profileDir);
ProfileOptimization.SetProfileRoot(profileDir);
}
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ internal static class UpdatesNotification
static UpdatesNotification()
{
s_notificationType = GetNotificationType();
CanNotifyUpdates = s_notificationType != NotificationType.Off;
CanNotifyUpdates = s_notificationType != NotificationType.Off
&& Platform.TryDeriveFromCache(PSVersionInfo.GitCommitId, out s_cacheDirectory);

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();
Expand Down
46 changes: 43 additions & 3 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,13 @@ 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 = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerShell";
internal static readonly string ConfigDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\PowerShell";
internal static readonly string CacheDirectory = SafeDeriveFromSpecialFolder(
Environment.SpecialFolder.LocalApplicationData,
@"Microsoft\PowerShell");

internal static readonly string ConfigDirectory = SafeDeriveFromSpecialFolder(
Environment.SpecialFolder.Personal,
@"PowerShell");

private static readonly Lazy<bool> _isStaSupported = new Lazy<bool>(() =>
{
Expand All @@ -189,6 +194,30 @@ 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[]
{
Expand Down Expand Up @@ -218,6 +247,17 @@ 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;

Expand Down Expand Up @@ -360,7 +400,7 @@ internal static string GetFolderPath(Environment.SpecialFolder folder)
_ => throw new NotSupportedException()
};
#else
return Environment.GetFolderPath(folder);
return Environment.GetFolderPath(folder, Environment.SpecialFolderOption.DoNotVerify);
#endif
}

Expand Down
10 changes: 8 additions & 2 deletions src/System.Management.Automation/engine/CommandDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,11 +1221,17 @@ internal LookupPathCollection GetLookupDirectoryPaths()
string tempDir = directory.TrimStart();
if (tempDir.EqualsOrdinalIgnoreCase("~"))
{
tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
tempDir = Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile,
Environment.SpecialFolderOption.DoNotVerify);
}
else if (tempDir.StartsWith("~" + Path.DirectorySeparatorChar))
{
tempDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + Path.DirectorySeparatorChar + tempDir.Substring(2);
tempDir = Environment.GetFolderPath(
Environment.SpecialFolder.UserProfile,
Environment.SpecialFolderOption.DoNotVerify)
+ Path.DirectorySeparatorChar
+ tempDir.Substring(2);
}

_cachedPath.Add(tempDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ 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
Expand Down Expand Up @@ -1121,7 +1126,7 @@ static AnalysisCacheData()
cacheFileName = string.Create(CultureInfo.InvariantCulture, $"{cacheFileName}-{hashString}");
}

s_cacheStoreLocation = Path.Combine(Platform.CacheDirectory, cacheFileName);
Platform.TryDeriveFromCache(cacheFileName, out s_cacheStoreLocation);
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/System.Management.Automation/engine/PSConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ 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;
perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName);
if (!string.IsNullOrEmpty(perUserConfigDirectory))
{
perUserConfigFile = Path.Combine(perUserConfigDirectory, ConfigFileName);
}
Comment on lines +92 to +95

emptyConfig = new JObject();
configRoots = new JObject[2];
Expand Down Expand Up @@ -387,6 +390,11 @@ internal PSKeyword GetLogKeywords()
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ 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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,11 @@ internal static string GetTranscriptPath(string baseDirectory, bool includeDate)
}
}

if (string.IsNullOrEmpty(baseDirectory))
{
return string.Empty;
}
Comment on lines +1159 to +1162

if (includeDate)
{
baseDirectory = Path.Combine(baseDirectory, DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture));
Expand Down
Loading
Loading