Skip to content

Initialize runspaces with InitialSessionState object #1526

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
Jul 20, 2021

Conversation

andyleejordan
Copy link
Member

Instead of passing values for LanguageMode and ExecutionPolicy as
fields that are set on an InitialSessionState object that's eventually
created, we now pass a fully initialized InitialSessionState object
from the start.

We also rename SetExecutionPolicy to RestoreExecutionPolicy to
reflect what it actually does: restores the user's policy after we
overrode it with Bypass while initializing the integrated console's
runspace. We override it because we need to be able to load PSReadLine
etc. without policy issues.

Finally, we fix the EditorConfig setting to enforce spaces after
control-flow keywords (such as if) because, while our codebase is
inconsistent, it is our preferred style going forward.

This replaces #1523.

@andyleejordan andyleejordan added Issue-Enhancement A feature request (enhancement). Area-Startup labels Jul 20, 2021
@andyleejordan andyleejordan requested a review from rjmholt July 20, 2021 00:02
@@ -11,7 +11,7 @@ insert_final_newline = true
indent_size = 4
trim_trailing_whitespace = true
csharp_space_before_open_square_brackets = true
csharp_space_after_keywords_in_control_flow_statements = false
csharp_space_after_keywords_in_control_flow_statements = true
Copy link
Member Author

Choose a reason for hiding this comment

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

@rjmholt I have no idea why this was false previously.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure either -- might be an autogenerated setting from a long time ago

{
InitialSessionState initialSessionState;
if (Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1") {
Copy link
Member Author

Choose a reason for hiding this comment

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

@rjmholt FYI this code is gone but I don't believe we were using it for anything at the moment.

}
// Finally, restore the runspace's execution policy to the user's policy instead of
// Bypass.
this.RestoreExecutionPolicy();
Copy link
Member Author

Choose a reason for hiding this comment

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

@rjmholt I moved the "if windows" to "if not windows" inside the function itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

@dkattan This still needs to be called because it restores the process scope policy when it was set on the CLI (such as by the extension).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I learned this the hard way in my constrained runspace branch. I makes a lot more sense now that it is called RestoreExecutionPolicy.

/// execution policy hierarchy. We do this because the process policy will always be set to
/// Bypass when initializing our runspaces.
/// </summary>
internal void RestoreExecutionPolicy()
Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for renaming this @dkattan!

// environment.
if (VersionUtils.IsWindows)
{
initialSessionState.ExecutionPolicy = ExecutionPolicy.Bypass;
Copy link
Member Author

Choose a reason for hiding this comment

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

Yay! This if finally just in the test path.

Instead of passing values for `LanguageMode` and `ExecutionPolicy` as
fields that are set on an `InitialSessionState` object that's eventually
created, we now pass a fully initialized `InitialSessionState` object
from the start.

We also rename `SetExecutionPolicy` to `RestoreExecutionPolicy` to
reflect what it actually does: restores the user's policy after we
overrode it with `Bypass` while initializing the integrated console's
runspace. We override it because we need to be able to load `PSReadLine`
etc. without policy issues.

Finally, we fix the EditorConfig setting to enforce spaces after
control-flow keywords (such as `if`) because, while our codebase is
inconsistent, it is our preferred style going forward.

Co-authored-by: Andrew Schwartzmeyer <[email protected]>
@andyleejordan andyleejordan force-pushed the andschwa/initial-session-state branch from 1e0488c to 1ec771d Compare July 20, 2021 00:16
@andyleejordan andyleejordan enabled auto-merge (squash) July 20, 2021 16:14
@@ -11,7 +11,7 @@ insert_final_newline = true
indent_size = 4
trim_trailing_whitespace = true
csharp_space_before_open_square_brackets = true
csharp_space_after_keywords_in_control_flow_statements = false
csharp_space_after_keywords_in_control_flow_statements = true
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure either -- might be an autogenerated setting from a long time ago

@@ -349,15 +349,24 @@ private EditorServicesConfig CreateConfigObject()
var profile = (PSObject)GetVariableValue("profile");

var hostInfo = new HostInfo(HostName, HostProfileId, HostVersion);
var editorServicesConfig = new EditorServicesConfig(hostInfo, Host, SessionDetailsPath, bundledModulesPath, LogPath)

var initialSessionState = Runspace.DefaultRunspace.InitialSessionState;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var initialSessionState = Runspace.DefaultRunspace.InitialSessionState;
InitialSessionState initialSessionState = Runspace.DefaultRunspace.InitialSessionState;

@@ -135,7 +135,7 @@ public sealed class HostStartupInfo
/// <param name="currentUsersProfilePath">The path to the user specific profile.</param>
/// <param name="featureFlags">Flags of features to enable.</param>
/// <param name="additionalModules">Names or paths of additional modules to import.</param>
/// <param name="languageMode">The language mode inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same language mode.</param>
/// <param name="initialSessionState">The language mode inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same initialSessionState including allowed modules, cmdlets and language mode.</param>
Copy link
Contributor

Choose a reason for hiding this comment

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

The initial part of this description is no longer accurate

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh dang, I thought I double-checked all of these. Ah well. For the documentation project.

@andyleejordan andyleejordan merged commit fc3d4a9 into master Jul 20, 2021
@andyleejordan andyleejordan deleted the andschwa/initial-session-state branch July 20, 2021 17:50
@@ -40,6 +41,16 @@ internal static class PowerShellContextFactory
public static PowerShellContextService Create(ILogger logger)
{
PowerShellContextService powerShellContext = new PowerShellContextService(logger, null, isPSReadLineEnabled: false);
var initialSessionState = InitialSessionState.CreateDefault();
Copy link
Member Author

Choose a reason for hiding this comment

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

Wait, @rjmholt @dkattan shouldn't this have been CreateDefault2()???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Startup Issue-Enhancement A feature request (enhancement).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants