-
Notifications
You must be signed in to change notification settings - Fork 235
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
Conversation
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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]>
1e0488c
to
1ec771d
Compare
@@ -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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of passing values for
LanguageMode
andExecutionPolicy
asfields that are set on an
InitialSessionState
object that's eventuallycreated, we now pass a fully initialized
InitialSessionState
objectfrom the start.
We also rename
SetExecutionPolicy
toRestoreExecutionPolicy
toreflect what it actually does: restores the user's policy after we
overrode it with
Bypass
while initializing the integrated console'srunspace. 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 isinconsistent, it is our preferred style going forward.
This replaces #1523.