Skip to content

Commit 69a0b04

Browse files
Fix creation of InitialSessionState to use CreateDefault2() (#1547)
We erroneously changed our code to use `CreateDefault()` instead of `CreateDefault2()`. There existed old but defunct code for testing the use of `CreateDefault()` instead, but it was never intentionally moved into production. The latter function loads only `Microsoft.PowerShell.Core`, leaving us to load the exact modules we intend to load ourselves, which is the safer behavior (and the previous behavior).
1 parent 3d0fb29 commit 69a0b04

File tree

3 files changed

+12
-27
lines changed

3 files changed

+12
-27
lines changed

PowerShellEditorServices.build.ps1

+2-18
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ if (Get-Command git -ErrorAction SilentlyContinue) {
5151
git update-index --assume-unchanged "$PSScriptRoot/src/PowerShellEditorServices.Hosting/BuildInfo.cs"
5252
}
5353

54-
function Invoke-WithCreateDefaultHook {
55-
param([scriptblock]$ScriptBlock)
56-
57-
try
58-
{
59-
$env:PSES_TEST_USE_CREATE_DEFAULT = 1
60-
& $ScriptBlock
61-
} finally {
62-
Remove-Item env:PSES_TEST_USE_CREATE_DEFAULT
63-
}
64-
}
65-
6654
function Install-Dotnet {
6755
param (
6856
[string[]]$Channel
@@ -246,16 +234,12 @@ task TestServerWinPS -If (-not $script:IsNix) {
246234

247235
task TestServerPS7 -If (-not $script:IsRosetta) {
248236
Set-Location .\test\PowerShellEditorServices.Test\
249-
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
250-
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS7 }
251-
}
237+
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS7 }
252238
}
253239

254240
task TestServerPS72 {
255241
Set-Location .\test\PowerShellEditorServices.Test\
256-
Invoke-WithCreateDefaultHook -NewModulePath $script:PSCoreModulePath {
257-
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS72 }
258-
}
242+
exec { & $script:dotnetExe $script:dotnetTestArgs $script:NetRuntime.PS72 }
259243
}
260244

261245
task TestE2E {

src/PowerShellEditorServices/Services/Analysis/PssaCmdletAnalysisEngine.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,12 @@ private static RunspacePool CreatePssaRunspacePool(out PSModuleInfo pssaModuleIn
468468
throw new FileNotFoundException("Unable to find PSScriptAnalyzer module on the module path");
469469
}
470470

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

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

test/PowerShellEditorServices.Test/PowerShellContextFactory.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ internal static class PowerShellContextFactory
4141
public static PowerShellContextService Create(ILogger logger)
4242
{
4343
PowerShellContextService powerShellContext = new PowerShellContextService(logger, null, isPSReadLineEnabled: false);
44-
var initialSessionState = InitialSessionState.CreateDefault();
44+
45+
// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core` only,
46+
// which is a more minimal and therefore safer state.
47+
var initialSessionState = InitialSessionState.CreateDefault2();
48+
4549
// We set the process scope's execution policy (which is really the runspace's scope) to
4650
// `Bypass` so we can import our bundled modules. This is equivalent in scope to the CLI
4751
// argument `-ExecutionPolicy Bypass`, which (for instance) the extension passes. Thus

0 commit comments

Comments
 (0)