Skip to content

Feature/constrained runspace support #1282

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

Closed

Conversation

dkattan
Copy link
Contributor

@dkattan dkattan commented May 4, 2020

@TylerLeonhardt I’d like some feedback on this approach since I essentially usurped your initial implementation. It works fine when InitialSessionState is created using CreateDefault2() but I‘m likely missing a few required cmdlets in my InitialSessionState.

I’m pretty confident it will work once I identify and add the prerequisite commands to the InitialSessionState.

@dkattan dkattan requested a review from rjmholt as a code owner May 4, 2020 04:18
@msftclas
Copy link

msftclas commented May 4, 2020

CLA assistant check
All CLA requirements met.

@TylerLeonhardt
Copy link
Member

Talked it over with @rjmholt and I think we are OK going in this direction.

I think you need to rebase on master though as you are reverting a change I made recently to an AST Visitor.

@dkattan
Copy link
Contributor Author

dkattan commented May 6, 2020

Excellent. I'm going to resolve the issues you found and rebase.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

Looking good! Some comments.

@@ -6,6 +6,6 @@ public static class BuildInfo
{
public static readonly string BuildVersion = "<development-build>";
public static readonly string BuildOrigin = "<development>";
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2019-12-06T21:43:41", CultureInfo.InvariantCulture.DateTimeFormat);
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2020-05-03T16:39:35", CultureInfo.InvariantCulture.DateTimeFormat);
Copy link
Member

Choose a reason for hiding this comment

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

Can you revert this? The build is supposed to mark this with git to not change... But it's not always worked.

</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
Copy link
Member

Choose a reason for hiding this comment

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

I think maybe Visual Studio did this... Can you revert it?

// TODO: This can be moved to the point after the $psEditor object
// gets initialized when that is done earlier than LanguageServer.Initialize
foreach (string module in hostStartupInfo.AdditionalModules)
// Darren Kattan: I haven't tested it, but I have a feeling this entire bit of logic can be replaced with the bit above for non-FullLanguage mode. I think that was is cleaner anyway.
Copy link
Member

Choose a reason for hiding this comment

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

I think my concerns with using ImportPSModule is that I'm not sure the user will get the output if their module doesn't import correctly.

Thats why I think we just used Install-Module before.

I don't have enough data to say that they won't get an error with ImportPSModule so if you happen to know that they would, then I say move this up.

Otherwise, move it all back to Import-Module.

Import-Module will gracefully handle when the path to the module doesn't exist (with an error in the console)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After extensive testing, it turns out that the only way to get the required modules to load properly in a Constrained Runspace (specifically PowerShellEditorServices.Commands and other modules that need access to the file system at load time) is to use InitialSessionState.ImportPSModuleFromPath

Loading it with ImportPSModule, or using Import-Module from within the already opened Runspace both fail with System.Management.Automation.DriveNotFoundException: 'Cannot find drive. A drive with the name 'C' does not exist.'

(int startColumnNumber, int startLineNumber) = GetStartColumnAndLineNumbersFromAst(functionDefinitionAst);
// Get the start column number of the function name,
// instead of the the start column of 'function' and create new extent for the functionName
int startColumnNumber =
Copy link
Member

Choose a reason for hiding this comment

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

This is the change that's in master that's being reverted. If you rebase on master this should go away and the tests will (likely) pass.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

One other thing to prevent a breaking change

/// <returns></returns>
public static Runspace CreateRunspace(PSHost psHost, PSLanguageMode languageMode)
public static Runspace CreateRunspace(PSHost psHost, InitialSessionState initialSessionState)
Copy link
Member

Choose a reason for hiding this comment

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

So my change before this and now this one technically introduced a breaking change to this public API.

Can you fix this for me?

  1. Create a CreateRunspace(PSHost psHost) overload that calls the other CreateRunspace with either CreateDefault or CreateDefault2 based on the environment variable.
  2. Make the overload with ISS internal

@TylerLeonhardt
Copy link
Member

@dkattan looks like you have some test failures

@dkattan
Copy link
Contributor Author

dkattan commented May 20, 2020

@TylerLeonhardt I'm still working on the desired functionality. I pulled the latest PowerShell code and got it such that I can step through PSES and PowerShell source but now I can't even get PSES to initialize correctly. The show-stopper for me at the moment is that the AddSerilog extension method not being found by PSES when spawned in-process by my app. I don't think this is the root cause of my issue, as this is preventing PSES from initializing at all whereas before it initialized but returned empty completion results.

Interestingly enough I think I found a legitimate bug in PowerShell: PowerShell/PowerShell#12734
I was able to work around it by not importing any modules

@dkattan dkattan force-pushed the feature/constrained-runspace-support branch from 1ab4ad9 to 798f429 Compare June 1, 2020 05:52
dkattan added 17 commits June 2, 2020 07:15
…space based on InitialSessionState instead of just the LanguageMode of the InitialSessionState.
… instead provide null as the InitialSessionState which will result in the previous initialSessionStateLogic running as it was in FullLanguage mode.

Updated incorrect ProjectReference path in PowerShellEditorServices.Test.Host.csproj but the test suite still doesn't run correctly.
…ionState.

Renamed initialRunspace to runspace
Typecasted AdditionalModules instead of using .ToArray() to prevent cannot convert from 'System.Collections.Generic.IReadOnlyList<string>' to 'System.Collections.Generic.IEnumerable<Microsoft.PowerShell.Commands.ModuleSpecification>' compile time error.
This is not ready to be merged as it needs to be rebased on master to fix test issues, and the primary objective of the changes has not been met as  CommandCompletion.CompleteInput returns 0 CompletionResults when spawned from a Constrained Runspace. This may or may not require a fix from the PowerShell team as we are spawning CompleteInput from an overload of CompleteCommand that uses TabExpansion2 and there is a comment in their code indicating that this (or something related to it) is currently broken: https://github.com/PowerShell/PowerShell/blob/f4382202ae4622bf26795e29a7b39b9d7cdfb3fb/src/System.Management.Automation/engine/CommandCompletion/CommandCompletion.cs#L547
…space based on InitialSessionState instead of just the LanguageMode of the InitialSessionState.
…ionState.

Renamed initialRunspace to runspace
Typecasted AdditionalModules instead of using .ToArray() to prevent cannot convert from 'System.Collections.Generic.IReadOnlyList<string>' to 'System.Collections.Generic.IEnumerable<Microsoft.PowerShell.Commands.ModuleSpecification>' compile time error.
This is not ready to be merged as it needs to be rebased on master to fix test issues, and the primary objective of the changes has not been met as  CommandCompletion.CompleteInput returns 0 CompletionResults when spawned from a Constrained Runspace. This may or may not require a fix from the PowerShell team as we are spawning CompleteInput from an overload of CompleteCommand that uses TabExpansion2 and there is a comment in their code indicating that this (or something related to it) is currently broken: https://github.com/PowerShell/PowerShell/blob/f4382202ae4622bf26795e29a7b39b9d7cdfb3fb/src/System.Management.Automation/engine/CommandCompletion/CommandCompletion.cs#L547
…ntinuously attempts to resolve missing cmdlets.
@dkattan dkattan force-pushed the feature/constrained-runspace-support branch from af3299a to 18dd6df Compare June 2, 2020 12:26
@TylerLeonhardt
Copy link
Member

Codacy Here is an overview of what got changed by this pull request:

Issues
======
+ Solved 1
           

See the complete overview on Codacy

@andyleejordan
Copy link
Member

Hey @dkattan I see you're still working on this. Can you get me up to speed on what this is, and if it's compatible with the ongoing work in #1459?

@dkattan
Copy link
Contributor Author

dkattan commented Jun 3, 2021

Hey @dkattan I see you're still working on this. Can you get me up to speed on what this is, and if it's compatible with the ongoing work in #1459?

I just looked over #1459 and I think the changes being made there will actually help with a threading exception I get quite frequently when debugging this branch. Additionally there isn't much overlap on the modified files in the two branches.

I'll rebase off async-ps-consumer to verify.

My objectives with these code changes are to ensure PSES can run in a Constrained Runspace in addition to Constrained Language Mode. This means PSES needs to run with no file system access.

Copy link
Contributor

@rjmholt rjmholt left a comment

Choose a reason for hiding this comment

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

This means PSES needs to run with no file system access.

I'm not really sure if that's possible, given things like the session file and needing to load modules and assemblies on the fly. But it probably depends on what exactly you mean.

@dkattan dkattan changed the base branch from master to async-ps-consumer June 18, 2021 17:43
@dkattan dkattan changed the base branch from async-ps-consumer to master June 18, 2021 17:43
@dkattan dkattan changed the base branch from master to async-ps-consumer June 18, 2021 19:43
@dkattan dkattan changed the base branch from async-ps-consumer to master June 18, 2021 19:44
@dkattan dkattan closed this Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants