Skip to content

Fix profile loading and $PROFILE variable #1604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2021
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
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