From 24e42bb302f50b6ea305ca2ca039edaa5f02d64c Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 4 Dec 2026 20:52:22 +0800 Subject: [PATCH] fix(terminal): let PowerShell run npm.ps1 and other script shims on Windows (#7939) Windows PowerShell's default execution policy is Restricted, so in the integrated terminal npm.ps1, nvm/Volta Node shims and a virtualenv's Activate.ps1 could not run. A plain PowerShell start in the terminal now gets -ExecutionPolicy Bypass (process scope) before -NoExit -Command , and MCP run_command running a line in PowerShell on Windows gets the same. Process scope changes nothing outside that PowerShell process, a Group Policy setting still takes precedence, and a shell with explicitly configured arguments starts as configured. Fixes #7920 --- crates/ide-host/src/mcp/run/agent.rs | 11 ++++++++++- crates/ide-terminal/src/integration.rs | 8 ++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ide-host/src/mcp/run/agent.rs b/crates/ide-host/src/mcp/run/agent.rs index 23c007185d..036c5d96e7 100644 --- a/crates/ide-host/src/mcp/run/agent.rs +++ b/crates/ide-host/src/mcp/run/agent.rs @@ -803,6 +803,10 @@ fn shell_invocation(shell: Option<&str>, line: &str) -> (String, Vec) { "cmd" => (line.to_owned(), Vec::new()), "pwsh" | "powershell" => { args.retain(|a| a != "-l"); + // As in the terminal: script shims such as npm.ps1 run under the default policy. + if cfg!(windows) { + args.extend(["-ExecutionPolicy".to_owned(), "Bypass".to_owned()]); + } args.extend(["-NoLogo".to_owned(), "-Command".to_owned(), line.to_owned()]); (program, args) } @@ -1141,7 +1145,12 @@ mod tests { assert_eq!(shell_invocation(Some("/bin/zsh"), "ls -a").1, ["-l", "-c", "ls -a"]); assert_eq!(shell_invocation(Some("/bin/bash --norc"), "ls").1, ["--norc", "-c", "ls"]); assert_eq!(shell_invocation(Some("/bin/tcsh"), "ls").1, ["-c", "ls"]); - assert_eq!(shell_invocation(Some("/usr/bin/pwsh"), "ls").1, ["-NoLogo", "-Command", "ls"]); + let pwsh: &[&str] = if cfg!(windows) { + &["-ExecutionPolicy", "Bypass", "-NoLogo", "-Command", "ls"] + } else { + &["-NoLogo", "-Command", "ls"] + }; + assert_eq!(shell_invocation(Some("/usr/bin/pwsh"), "ls").1, pwsh); assert_eq!(shell_invocation(Some("cmd.exe"), "dir /b"), ("dir /b".to_owned(), Vec::new())); } diff --git a/crates/ide-terminal/src/integration.rs b/crates/ide-terminal/src/integration.rs index 054ee065ae..92b4205f32 100644 --- a/crates/ide-terminal/src/integration.rs +++ b/crates/ide-terminal/src/integration.rs @@ -147,6 +147,8 @@ const FISH_INIT: &str = concat!( /// every Windows PowerShell 5.1 terminal reach its prompt ~90 ms later. const POWERSHELL_INIT: &str = r#"$global:__openideaClearHost = ${function:Clear-Host}; function global:Clear-Host { & $global:__openideaClearHost @args; [Console]::Write([char]27 + ']133;OpenIDEA;ClearScrollback' + [char]7) }"#; +const POWERSHELL_ARGS: [&str; 5] = ["-ExecutionPolicy", "Bypass", "-NoExit", "-Command", POWERSHELL_INIT]; + /// Directory holding the generated startup files (written on demand). Keyed by the scripts' /// content as well as the version, so builds with different scripts (a dev build next to a /// release, two checkouts on one machine) never rewrite each other's files under a starting shell. @@ -188,7 +190,9 @@ pub(crate) fn apply(shell_name: &str, args: &mut Vec, env: &mut BTreeMap if !args.is_empty() { return false; } - args.extend(["-NoExit".into(), "-Command".into(), POWERSHELL_INIT.into()]); + // Process-scope Bypass, so npm.ps1 and other script shims run under the default + // Restricted policy; a Group Policy setting still takes precedence. + args.extend(POWERSHELL_ARGS.map(Into::into)); return true; } // Windows: Git Bash and MSYS2's bash read the same script. Its process tree can't tell @@ -287,7 +291,7 @@ mod tests { let mut env = BTreeMap::new(); assert_eq!(apply(shell, &mut args, &mut env), cfg!(windows)); if cfg!(windows) { - assert_eq!(args, ["-NoExit", "-Command", POWERSHELL_INIT]); + assert_eq!(args, ["-ExecutionPolicy", "Bypass", "-NoExit", "-Command", POWERSHELL_INIT]); } else { assert!(args.is_empty()); }