-
Notifications
You must be signed in to change notification settings - Fork 234
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
Feature/constrained runspace support #1282
Conversation
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. |
Excellent. I'm going to resolve the issues you found and rebase. |
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.
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); |
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.
Can you revert this? The build is supposed to mark this with git to not change... But it's not always worked.
src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs
Show resolved
Hide resolved
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Properties\" /> |
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 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. |
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 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)
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.
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 = |
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.
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.
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.
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) |
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.
So my change before this and now this one technically introduced a breaking change to this public API.
Can you fix this for me?
- Create a
CreateRunspace(PSHost psHost)
overload that calls the otherCreateRunspace
with eitherCreateDefault
orCreateDefault2
based on the environment variable. - Make the overload with ISS internal
@dkattan looks like you have some test failures |
@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 |
1ab4ad9
to
798f429
Compare
…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.
…ox in a constrained runspace
…ed-runspace-support
af3299a
to
18dd6df
Compare
Issues
======
+ Solved 1
See the complete overview on Codacy |
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. |
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.
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.
…to ImportPSModulesFromPath. PSScriptAnalyzer load fails with "The following error occurred while loading the extended type data file: Error in TypeData "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.RuleInfo": "
This reverts commit bdd993c.
…ttan/PowerShellEditorServices into feature/constrained-runspace-support
@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.