Skip to content

Commit

Permalink
Fix profile loading and $PROFILE variable
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmholt authored and andyleejordan committed Nov 3, 2021
1 parent 7cc7569 commit 7ac4bab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 17 additions & 0 deletions src/PowerShellEditorServices/Utility/PSCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Get a representation of the PSCommand, for logging purposes.
/// </summary>
Expand Down

0 comments on commit 7ac4bab

Please sign in to comment.