diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index ee8e2d5879..5cfdb06765 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -187,17 +187,18 @@ namespace Microsoft.PowerShell @"\Microsoft\Windows\PowerShell"; } - else - { - //check if the user has set an XDG path in their environment variables - profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.PROFILE); - } - - if (!Directory.Exists(profileDir)) //xdg value may have been set but not a valid directory - { - Console.WriteLine("The selected directory (" + profileDir +") for the profile does not exist. Using the default path."); - profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.DEFAULT); - } + else + { + //check if the user has set an XDG path in their environment variables + profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.CACHE); + + if (!Directory.Exists(profileDir)) //xdg value may have been set but not a valid directory + { + Console.Error.WriteLine("The selected directory (" + profileDir +") for the profile does not exist. Using the default path."); + profileDir = Platform.SelectProductNameForDirectory(Platform.XDG_Type.DEFAULT); + } + } + ClrFacade.SetProfileOptimizationRoot(profileDir); } diff --git a/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs b/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs index d3fae3ee62..cd00286ac8 100644 --- a/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs +++ b/src/Microsoft.PowerShell.PSReadLine/Cmdlets.cs @@ -180,23 +180,20 @@ namespace Microsoft.PowerShell else { //PSReadline does not have access to Utils.CorePSPlatform. Must set PSReadline path seperately - string historypath = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME"); + string historypath = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); if (!String.IsNullOrEmpty(historypath)) { - historypath = System.IO.Path.Combine(historypath, "powershell"); + historypath = System.IO.Path.Combine(historypath, "powershell", "PSReadLine", hostName + "_history.txt"); HistorySavePath = historypath; } else { - historypath = System.IO.Path.Combine( - Environment.GetEnvironmentVariable("HOME"), - ".local/share/powershell/modules"); - HistorySavePath = System.IO.Path.Combine( Environment.GetEnvironmentVariable("HOME"), - historypath, + ".cache", + "powershell", "PSReadLine", hostName + "_history.txt"); } diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index ecf7d8b2a5..f2bfb4d9c7 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -48,7 +48,7 @@ namespace System.Management.Automation { PROFILE, MODULES, - HISTORY, + CACHE, DEFAULT } @@ -115,7 +115,8 @@ namespace System.Management.Automation string xdgdatahome = System.Environment.GetEnvironmentVariable("XDG_DATA_HOME"); string xdgcachehome = System.Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); string xdgConfigHomeDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".config/powershell"); - string moduleDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".local/share/powershell/modules"); + string xdgModuleDefault = Path.Combine ( System.Environment.GetEnvironmentVariable("HOME"), ".local/share/powershell/modules"); + string xdgCacheDefault = Path.Combine (System.Environment.GetEnvironmentVariable("HOME"), ".cache/powershell"); switch (dirpath){ case Platform.XDG_Type.PROFILE: @@ -134,40 +135,54 @@ namespace System.Management.Automation case Platform.XDG_Type.MODULES: //the user has set XDG_DATA_HOME corresponding to module path if (String.IsNullOrEmpty(xdgdatahome)){ - //xdg values have not been set - if (!Directory.Exists(moduleDefault)) //module folder not always guaranteed to exist - { - Directory.CreateDirectory(moduleDefault); - } - - return xdgConfigHomeDefault; + + //xdg values have not been set + if (!Directory.Exists(xdgModuleDefault)) //module folder not always guaranteed to exist + { + Directory.CreateDirectory(xdgModuleDefault); + } + return xdgConfigHomeDefault; } - else { return Path.Combine(xdgdatahome, "powershell"); } - - case Platform.XDG_Type.HISTORY: + + case Platform.XDG_Type.CACHE: //the user has set XDG_CACHE_HOME - if (String.IsNullOrEmpty(xdgcachehome)){ - return xdgConfigHomeDefault; + if (String.IsNullOrEmpty(xdgcachehome)) + { + //xdg values have not been set + if (!Directory.Exists(xdgCacheDefault)) //module folder not always guaranteed to exist + { + Directory.CreateDirectory(xdgCacheDefault); + } + + return xdgCacheDefault; } else { return Path.Combine(xdgcachehome, "powershell"); - - } - + } + case Platform.XDG_Type.DEFAULT: //default for profile location return xdgConfigHomeDefault; default: + //xdgConfigHomeDefault needs to be created in the edge case that we do not have the folder or it was deleted + //This folder is the default in the event of all other failures for data storage if (!Directory.Exists(xdgConfigHomeDefault)) { - Directory.CreateDirectory(xdgConfigHomeDefault); + try { + Directory.CreateDirectory(xdgConfigHomeDefault); + } + catch{ + + Console.Error.WriteLine("Failed to create default data directory: " + xdgConfigHomeDefault); + } } + return xdgConfigHomeDefault; } diff --git a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs index aef980161f..592c0dcaef 100644 --- a/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs +++ b/src/System.Management.Automation/engine/hostifaces/HostUtilities.cs @@ -188,11 +188,6 @@ namespace System.Management.Automation basePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); basePath = IO.Path.Combine(basePath, Utils.ProductNameForDirectory); - //If the profile path doesn't exist, create it. - if (!System.IO.Directory.Exists(basePath)) - { - System.IO.Directory.CreateDirectory(basePath); - } } else