diff --git a/crates/ide-core/src/process.rs b/crates/ide-core/src/process.rs index ff6b3dbe57..0cb6ba44e6 100644 --- a/crates/ide-core/src/process.rs +++ b/crates/ide-core/src/process.rs @@ -622,6 +622,30 @@ pub fn host_vars_in(dir: &Path) -> impl Iterator { apply_changes(std::env::vars_os().collect(), &child_env_changes(Some(dir))).into_iter() } +/// Whether `(key, value)` can go into a Windows environment block (`KEY=VALUE\0` +/// entries, then a final `\0`). `CreateProcessW` / `NtCreateUserProcess` returns +/// `ERROR_INVALID_PARAMETER` (os error 87) when an entry has an empty name or an +/// interior NUL. That happens when a `REG_MULTI_SZ` user environment value is +/// loaded into the process or re-read from the registry (#6806): the extra NUL +/// is treated as an entry separator and leaves a fragment with no `=`. +pub fn is_spawnable_env_pair(key: &OsStr, value: &OsStr) -> bool { + !key.is_empty() && !contains_ascii(key, 0) && !contains_ascii(key, b'=') && !contains_ascii(value, 0) +} + +fn contains_ascii(s: &OsStr, byte: u8) -> bool { + s.as_encoded_bytes().contains(&byte) +} + +/// [`host_vars`] without pairs that a Windows PTY spawn would reject. +pub fn spawnable_host_vars() -> impl Iterator { + host_vars().filter(|(k, v)| is_spawnable_env_pair(k, v)) +} + +/// [`host_vars_in`] without pairs that a Windows PTY spawn would reject. +pub fn spawnable_host_vars_in(dir: &Path) -> impl Iterator { + host_vars_in(dir).filter(|(k, v)| is_spawnable_env_pair(k, v)) +} + /// [`std::env::var_os`] as a child should see it (see [`host_env`]), the login shell's value /// included when this process lacks the variable. pub fn host_var_os(key: impl AsRef) -> Option { @@ -1240,6 +1264,35 @@ mod tests { assert!(host_vars_in(root).all(|(k, _)| k != "OPENIDEA_PROJECT_VAR")); } + fn os_from_bytes(bytes: &[u8]) -> OsString { + #[cfg(unix)] + { + use std::os::unix::ffi::OsStringExt; + OsString::from_vec(bytes.to_vec()) + } + #[cfg(windows)] + { + use std::os::windows::ffi::OsStringExt; + OsString::from_wide(&bytes.iter().copied().map(u16::from).collect::>()) + } + } + + #[test] + fn spawnable_env_pairs_reject_empty_keys_equals_and_interior_nuls() { + let ok_key = OsString::from("PATH"); + let ok_val = OsString::from("/bin"); + assert!(is_spawnable_env_pair(&ok_key, &ok_val)); + assert!(!is_spawnable_env_pair(OsStr::new(""), &ok_val)); + assert!(!is_spawnable_env_pair(OsStr::new("FOO=BAR"), &ok_val)); + // A REG_MULTI_SZ value's interior NUL would split the environment block, + // leaving a fragment with no `=` (#6806). An empty name is also rejected. + assert!(!is_spawnable_env_pair(&os_from_bytes(b""), &os_from_bytes(b"132786"))); + assert!(!is_spawnable_env_pair(&ok_key, &os_from_bytes(b"1788051509\x00132786"))); + assert!(!is_spawnable_env_pair(&os_from_bytes(b"132786\x00"), &ok_val)); + assert!(spawnable_host_vars().all(|(k, v)| is_spawnable_env_pair(&k, &v))); + assert!(spawnable_host_vars().any(|(k, _)| k == "PATH" || k == "Path")); + } + #[test] fn changes_apply_in_order() { let vars = apply_changes( diff --git a/crates/ide-dap/src/session.rs b/crates/ide-dap/src/session.rs index d95d6e9ef5..5279150d5c 100644 --- a/crates/ide-dap/src/session.rs +++ b/crates/ide-dap/src/session.rs @@ -1134,21 +1134,27 @@ pub(crate) fn run_in_terminal(s: &Arc, args: &Value) -> Result cmd.env(k, v), - None => cmd.env_remove(k), - } + // See #6806: do not inherit portable-pty's registry-built Windows env block. + cmd.env_clear(); + for (k, v) in ide_core::process::spawnable_host_vars_in(cwd) { + cmd.env(k, v); } for (k, v) in ide_core::process::shell_env_in(cwd) { - cmd.env(k, v); + if ide_core::process::is_spawnable_env_pair(&k, &v) { + cmd.env(k, v); + } } cmd.env("TERM", "xterm-256color"); cmd.cwd(cwd); if let Some(env) = args.get("env").and_then(Value::as_object) { for (k, v) in env { match v.as_str() { - Some(v) => cmd.env(k, v), + Some(v) + if ide_core::process::is_spawnable_env_pair(std::ffi::OsStr::new(k), std::ffi::OsStr::new(v)) => + { + cmd.env(k, v) + } + Some(_) => {} None => cmd.env_remove(k), }; } diff --git a/crates/ide-run/src/process.rs b/crates/ide-run/src/process.rs index 3ef3a8e91d..c13dcf1227 100644 --- a/crates/ide-run/src/process.rs +++ b/crates/ide-run/src/process.rs @@ -636,23 +636,24 @@ impl ProcessManager { let mut cmd = CommandBuilder::new(&inv.program); cmd.args(&inv.args); cmd.cwd(cwd); - if env.clear { - cmd.env_clear(); - } else { - for (k, v) in ide_core::process::host_env_fixes_in(cwd) { - match v { - Some(v) => cmd.env(k, v), - None => cmd.env_remove(k), - } + // See #6806: do not inherit portable-pty's registry-built Windows env block. + cmd.env_clear(); + if !env.clear { + for (k, v) in ide_core::process::spawnable_host_vars_in(cwd) { + cmd.env(k, v); } for (k, v) in ide_core::process::shell_env_in(cwd) { - cmd.env(k, v); + if ide_core::process::is_spawnable_env_pair(&k, &v) { + cmd.env(k, v); + } } } cmd.env("TERM", "xterm-256color"); cmd.env("COLORTERM", "truecolor"); for (k, v) in &env.vars { - cmd.env(k, v); + if ide_core::process::is_spawnable_env_pair(std::ffi::OsStr::new(k), std::ffi::OsStr::new(v)) { + cmd.env(k, v); + } } #[cfg(unix)] let wait_writable = ide_core::input::nonblocking( diff --git a/crates/ide-terminal/src/lib.rs b/crates/ide-terminal/src/lib.rs index fb9925d039..06041cc37b 100644 --- a/crates/ide-terminal/src/lib.rs +++ b/crates/ide-terminal/src/lib.rs @@ -344,15 +344,18 @@ impl TerminalManager { resolve_cwd(&options.cwd) }; cmd.cwd(&cwd); - // In a project folder: the project's environment (its direnv `.envrc`) too. - for (k, v) in ide_core::process::host_env_fixes_in(&cwd) { - match v { - Some(v) => cmd.env(k, v), - None => cmd.env_remove(k), - } + // portable-pty starts from a base env that re-reads HKCU\Environment on + // Windows. A REG_MULTI_SZ value's interior NULs split the environment + // block and CreateProcessW fails with os error 87 (#6806). Build the + // child's environment from sanitized host vars instead. + cmd.env_clear(); + for (k, v) in ide_core::process::spawnable_host_vars_in(&cwd) { + cmd.env(k, v); } for (k, v) in env { - cmd.env(k, v); + if ide_core::process::is_spawnable_env_pair(std::ffi::OsStr::new(&k), std::ffi::OsStr::new(&v)) { + cmd.env(k, v); + } } apply_powershell_env(&shell, &mut cmd);