Skip to content

Commit a1449d2

Browse files
committed
Fix creation of InitialSessionState to use CreateDefault2
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 a1449d2

File tree

3 files changed

+6
-25
lines changed

3 files changed

+6
-25
lines changed

PowerShellEditorServices.build.ps1

Lines changed: 2 additions & 18 deletions
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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,8 @@ private static RunspacePool CreatePssaRunspacePool(out PSModuleInfo pssaModuleIn
470470

471471
// Now that we know where the PSScriptAnalyzer we want to use is,
472472
// 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
473+
// We intentionally use `CreateDefault2` as it loads `Microsoft.PowerShell.Core` only.
478474
InitialSessionState sessionState = InitialSessionState.CreateDefault2();
479-
#endif
480475

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

test/PowerShellEditorServices.Test/PowerShellContextFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ 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+
// We intentionally use `CreateDefault2` as it loads `Microsoft.PowerShell.Core` only.
45+
var initialSessionState = InitialSessionState.CreateDefault2();
46+
4547
// We set the process scope's execution policy (which is really the runspace's scope) to
4648
// `Bypass` so we can import our bundled modules. This is equivalent in scope to the CLI
4749
// argument `-ExecutionPolicy Bypass`, which (for instance) the extension passes. Thus

0 commit comments

Comments
 (0)