diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index 5724776d5..2e0b9ad3e 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -9,7 +9,6 @@ using Microsoft.PowerShell.EditorServices.Hosting; using Microsoft.PowerShell.EditorServices.Utility; using System.Collections.Generic; -using System.IO; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility { @@ -164,14 +163,24 @@ public static void SetCorrectExecutionPolicy(this PowerShell pwsh, ILogger logge public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePaths) { - var profileVariable = new PSObject(); + // Per the documentation, "the `$PROFILE` variable stores the path to the 'Current User, + // Current Host' profile. The other profiles are saved in note properties of the + // `$PROFILE` variable. Its type is `String`. + // + // https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.1#the-profile-variable + var profileVariable = PSObject.AsPSObject(profilePaths.CurrentUserCurrentHost); - pwsh.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) - .AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + var psCommand = new PSCommand() + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts) + .AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost); + // NOTE: This must be set before the profiles are loaded. pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable); + + pwsh.InvokeCommand(psCommand); + } public static void ImportModule(this PowerShell pwsh, string moduleNameOrPath) @@ -200,22 +209,6 @@ public static string GetErrorString(this PowerShell pwsh) return sb.ToString(); } - private static PowerShell AddProfileMemberAndLoadIfExists(this PowerShell pwsh, PSObject profileVariable, string profileName, string profilePath) - { - profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); - - if (File.Exists(profilePath)) - { - var psCommand = new PSCommand() - .AddScript(profilePath, useLocalScope: false) - .AddOutputCommand(); - - pwsh.InvokeCommand(psCommand); - } - - return pwsh; - } - private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord error, int errorIndex) { sb.Append("Error #").Append(errorIndex).Append(':').AppendLine() diff --git a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs index 55c8eed20..602937db2 100644 --- a/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs +++ b/src/PowerShellEditorServices/Utility/PSCommandExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.IO; using System.Linq.Expressions; using System.Management.Automation; using System.Management.Automation.Runspaces; @@ -61,6 +62,22 @@ public static PSCommand MergePipelineResults(this PSCommand psCommand) return psCommand; } + public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObject profileVariable, string profileName, string profilePath) + { + // This path should be added regardless of the existence of the file. + profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath)); + + if (File.Exists(profilePath)) + { + psCommand + .AddCommand(profilePath, useLocalScope: false) + .AddOutputCommand() + .AddStatement(); + } + + return psCommand; + } + /// /// Get a representation of the PSCommand, for logging purposes. ///