Skip to content

Fix creation of InitialSessionState to use CreateDefault2() #1547

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
Aug 18, 2021
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
20 changes: 2 additions & 18 deletions PowerShellEditorServices.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ if (Get-Command git -ErrorAction SilentlyContinue) {
git update-index --assume-unchanged "$PSScriptRoot/src/PowerShellEditorServices.Hosting/BuildInfo.cs"
}

function Invoke-WithCreateDefaultHook {
param([scriptblock]$ScriptBlock)

try
{
$env:PSES_TEST_USE_CREATE_DEFAULT = 1
& $ScriptBlock
} finally {
Remove-Item env:PSES_TEST_USE_CREATE_DEFAULT
}
}

function Install-Dotnet {
param (
[string[]]$Channel
Expand Down Expand Up @@ -246,16 +234,12 @@ task TestServerWinPS -If (-not $script:IsNix) {

task TestServerPS7 -If (-not $script:IsRosetta) {
Set-Location .\test\PowerShellEditorServices.Test\
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS7 }
}
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS7 }
}

task TestServerPS72 {
Set-Location .\test\PowerShellEditorServices.Test\
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS72 }
}
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS72 }
}

task TestE2E {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,12 @@ private static RunspacePool CreatePssaRunspacePool(out PSModuleInfo pssaModuleIn
throw new FileNotFoundException("Unable to find PSScriptAnalyzer module on the module path");
}

// Now that we know where the PSScriptAnalyzer we want to use is,
// create a base session state with PSScriptAnalyzer loaded
#if DEBUG
InitialSessionState sessionState = Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1"
? InitialSessionState.CreateDefault()
: InitialSessionState.CreateDefault2();
#else
// Now that we know where the PSScriptAnalyzer we want to use is, create a base
// session state with PSScriptAnalyzer loaded
//
// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core`
// only, which is a more minimal and therefore safer state.
InitialSessionState sessionState = InitialSessionState.CreateDefault2();
#endif

sessionState.ImportPSModule(new [] { pssaModuleInfo.ModuleBase });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ internal static class PowerShellContextFactory
public static PowerShellContextService Create(ILogger logger)
{
PowerShellContextService powerShellContext = new PowerShellContextService(logger, null, isPSReadLineEnabled: false);
var initialSessionState = InitialSessionState.CreateDefault();

// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core` only,
// which is a more minimal and therefore safer state.
var initialSessionState = InitialSessionState.CreateDefault2();

// We set the process scope's execution policy (which is really the runspace's scope) to
// `Bypass` so we can import our bundled modules. This is equivalent in scope to the CLI
// argument `-ExecutionPolicy Bypass`, which (for instance) the extension passes. Thus
Expand Down