Skip to content

Commit 7ac4bab

Browse files
rjmholtandyleejordan
authored andcommitted
Fix profile loading and $PROFILE variable
1 parent 7cc7569 commit 7ac4bab

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.PowerShell.EditorServices.Hosting;
1010
using Microsoft.PowerShell.EditorServices.Utility;
1111
using System.Collections.Generic;
12-
using System.IO;
1312

1413
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
1514
{
@@ -164,14 +163,24 @@ public static void SetCorrectExecutionPolicy(this PowerShell pwsh, ILogger logge
164163

165164
public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePaths)
166165
{
167-
var profileVariable = new PSObject();
166+
// Per the documentation, "the `$PROFILE` variable stores the path to the 'Current User,
167+
// Current Host' profile. The other profiles are saved in note properties of the
168+
// `$PROFILE` variable. Its type is `String`.
169+
//
170+
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.1#the-profile-variable
171+
var profileVariable = PSObject.AsPSObject(profilePaths.CurrentUserCurrentHost);
168172

169-
pwsh.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts)
170-
.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost)
171-
.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts)
172-
.AddProfileMemberAndLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost);
173+
var psCommand = new PSCommand()
174+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersAllHosts), profilePaths.AllUsersAllHosts)
175+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.AllUsersCurrentHost), profilePaths.AllUsersCurrentHost)
176+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserAllHosts), profilePaths.CurrentUserAllHosts)
177+
.AddProfileLoadIfExists(profileVariable, nameof(profilePaths.CurrentUserCurrentHost), profilePaths.CurrentUserCurrentHost);
173178

179+
// NOTE: This must be set before the profiles are loaded.
174180
pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable);
181+
182+
pwsh.InvokeCommand(psCommand);
183+
175184
}
176185

177186
public static void ImportModule(this PowerShell pwsh, string moduleNameOrPath)
@@ -200,22 +209,6 @@ public static string GetErrorString(this PowerShell pwsh)
200209
return sb.ToString();
201210
}
202211

203-
private static PowerShell AddProfileMemberAndLoadIfExists(this PowerShell pwsh, PSObject profileVariable, string profileName, string profilePath)
204-
{
205-
profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath));
206-
207-
if (File.Exists(profilePath))
208-
{
209-
var psCommand = new PSCommand()
210-
.AddScript(profilePath, useLocalScope: false)
211-
.AddOutputCommand();
212-
213-
pwsh.InvokeCommand(psCommand);
214-
}
215-
216-
return pwsh;
217-
}
218-
219212
private static StringBuilder AddErrorString(this StringBuilder sb, ErrorRecord error, int errorIndex)
220213
{
221214
sb.Append("Error #").Append(errorIndex).Append(':').AppendLine()

src/PowerShellEditorServices/Utility/PSCommandExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.IO;
56
using System.Linq.Expressions;
67
using System.Management.Automation;
78
using System.Management.Automation.Runspaces;
@@ -61,6 +62,22 @@ public static PSCommand MergePipelineResults(this PSCommand psCommand)
6162
return psCommand;
6263
}
6364

65+
public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObject profileVariable, string profileName, string profilePath)
66+
{
67+
// This path should be added regardless of the existence of the file.
68+
profileVariable.Members.Add(new PSNoteProperty(profileName, profilePath));
69+
70+
if (File.Exists(profilePath))
71+
{
72+
psCommand
73+
.AddCommand(profilePath, useLocalScope: false)
74+
.AddOutputCommand()
75+
.AddStatement();
76+
}
77+
78+
return psCommand;
79+
}
80+
6481
/// <summary>
6582
/// Get a representation of the PSCommand, for logging purposes.
6683
/// </summary>

0 commit comments

Comments
 (0)