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 <init>, 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
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-04 20:52:22 +08:00
1 джерело 67c00a986a
коміт 24e42bb302
2 files changed
+16 -3

No files matched your search

+10 -1
Переглянути файл
@@ -803,6 +803,10 @@ fn shell_invocation(shell: Option<&str>, line: &str) -> (String, Vec<String>) {
"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()));
}
+6 -2
Переглянути файл
@@ -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<String>, 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());
}