Skip to content

Fix startup bug when zero profiles are present #1807

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
May 19, 2022
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 @@ -113,12 +113,12 @@ public PsesInternalHost(
_cancellationContext = new CancellationContext();

// Default stack size on .NET Framework is 524288 (512KB) (as reported by GetProcessDefaultStackSize)
// this leaves very little room in the stack. Windows PowerShell internally sets the value based on
// PipelineMaxStackSizeMB as seen here: https://github.com/PowerShell/PowerShell/issues/1187,
// this leaves very little room in the stack. Windows PowerShell internally sets the value based on
// PipelineMaxStackSizeMB as seen here: https://github.com/PowerShell/PowerShell/issues/1187,
// which has default of 10 and multiplies that by 1_000_000, so the default stack size is
// 10_000_000 (~10MB) when starting in normal console host.
// 10_000_000 (~10MB) when starting in normal console host.
//
// For PS7 the value is ignored by .NET because settings the stack size is not supported, but we can
// For PS7 the value is ignored by .NET because settings the stack size is not supported, but we can
// still provide 0, which means fallback to the default in both .NET and .NET Framework.
int maxStackSize = VersionUtils.IsPS5 ? 10_000_000 : 0;
_pipelineThread = new Thread(Run, maxStackSize)
Expand Down Expand Up @@ -433,7 +433,7 @@ internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
// NOTE: This is a special task run on startup!
return ExecuteDelegateAsync(
"LoadProfiles",
new PowerShellExecutionOptions { ThrowOnError = false },
executionOptions: null,
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Utility;
using System.Collections.Generic;

namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
{
Expand Down Expand Up @@ -211,7 +211,13 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat
// NOTE: This must be set before the profiles are loaded.
pwsh.Runspace.SessionStateProxy.SetVariable("PROFILE", profileVariable);

pwsh.InvokeCommand(psCommand);
// NOTE: Because it's possible there are no profiles defined, we might have an empty
// command. Since this is being executed directly, we can't rely on `ThrowOnError =
// false` to avoid an exception here. Instead, we must just not execute it.
if (psCommand.Commands.Count > 0)
{
pwsh.InvokeCommand(psCommand);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: @SeeminglyScience we should check everywhere else we directly invoke too.

}
}

public static void ImportModule(this PowerShell pwsh, string moduleNameOrPath)
Expand Down
5 changes: 1 addition & 4 deletions src/PowerShellEditorServices/Utility/PSCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ public static PSCommand AddProfileLoadIfExists(this PSCommand psCommand, PSObjec

if (File.Exists(profilePath))
{
psCommand
.AddCommand(profilePath, useLocalScope: false)
.AddOutputCommand()
.AddStatement();
psCommand.AddCommand(profilePath, useLocalScope: false).AddOutputCommand().AddStatement();
}

return psCommand;
Expand Down