Skip to content

Commit 41de2c8

Browse files
committed
Set execution policy to Bypass when creating the initial runspace
This eliminates the need to have `SetExecutionPolicy` which is a slow function that first queries a bunch of policies and then within PowerShell itself (as in, using a cmdlet) sets it to `Bypass`. However, we just want the process scope (AKA runspace scope) to have the execution policy set to `Bypass` all the time so that we can import our bundled modules, such as PSReadLine.
1 parent 6d5e709 commit 41de2c8

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Management.Automation.Remoting;
1212
using System.Management.Automation.Runspaces;
1313
using System.Reflection;
14+
using System.Runtime.InteropServices;
1415
using System.Text;
1516
using System.Threading;
1617
using System.Threading.Tasks;
@@ -281,6 +282,15 @@ public static Runspace CreateRunspace(PSHost psHost, PSLanguageMode languageMode
281282
// should have the same LanguageMode of whatever is set by the system.
282283
initialSessionState.LanguageMode = languageMode;
283284

285+
// We set the process scope's execution policy (which is really the runspace's scope) to
286+
// Bypass so we can import our bundled modules. This is equivalent in scope to the CLI
287+
// argument `-Bypass`, which (for instance) the extension passes. Thus we emulate this
288+
// behavior for consistency such that unit tests can pass in a similar environment.
289+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
290+
{
291+
initialSessionState.ExecutionPolicy = ExecutionPolicy.Bypass;
292+
}
293+
284294
Runspace runspace = RunspaceFactory.CreateRunspace(psHost, initialSessionState);
285295

286296
// Windows PowerShell must be hosted in STA mode

test/PowerShellEditorServices.Test/PowerShellContextFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public static PowerShellContextService Create(ILogger logger)
4646
TestProfilePaths,
4747
new List<string>(),
4848
new List<string>(),
49+
// TODO: We want to replace this property with an entire initial session state,
50+
// which would then also control the process-scoped execution policy.
4951
PSLanguageMode.FullLanguage,
5052
null,
5153
0,

test/PowerShellEditorServices.Test/Session/PowerShellContextTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,9 @@ await this.powerShellContext.ExecuteCommandAsync<string>(
147147
}
148148

149149
[Trait("Category", "PSReadLine")]
150-
[SkippableFact]
150+
[Fact]
151151
public void CanGetPSReadLineProxy()
152152
{
153-
Skip.If(IsWindows, "This test doesn't work on Windows for some reason.");
154153
Assert.True(PSReadLinePromptContext.TryGetPSReadLineProxy(
155154
NullLogger.Instance,
156155
PowerShellContextFactory.initialRunspace,

0 commit comments

Comments
 (0)