From aee191d568f22f45ff89b8447bba79ad845bfd4e Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Tue, 10 Nov 2026 19:13:43 +0100 Subject: [PATCH] fix(doctor): a shell's own commands are no programs missing from PATH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `devstack doctor` checked the first word of each `build`, `run` and `exec` line on PATH, so `set "E2E_URL=…" && npx playwright test`, the line the stack file reference gives for Windows since 0.45.0, failed with "not on PATH: set". It now takes the first word of each command between `&&`, `||`, `;`, `|` and `&` (outside quotes), leaves out what the step's shell runs itself (cmd: `set`, `cd`, `call`, …; sh: `export`, `cd`, `.`, …) and, in sh, the assignments before a command, so `npx` is what gets checked. Closes #510 Bug-Class: shell-builtin-as-program Co-Authored-By: Claude Opus 5.5 --- crates/devstack/src/doctor.rs | 127 ++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 7 deletions(-) diff --git a/crates/devstack/src/doctor.rs b/crates/devstack/src/doctor.rs index 14593d4b..0b63987c 100644 --- a/crates/devstack/src/doctor.rs +++ b/crates/devstack/src/doctor.rs @@ -205,15 +205,12 @@ pub(crate) fn output_within(cmd: &mut Command, limit: Duration) -> Option Finding { let mut names = BTreeSet::new(); - let mut add = |cmd: &str| { - if let Some(p) = program(cmd) { - names.insert(p); - } - }; + let shell = if cfg!(windows) { Shell::Cmd } else { Shell::Sh }; + let mut add = |cmd: &str| names.extend(programs(cmd, shell)); let platform = devstack_config::model::Platform::current(); for s in stack.services.values() { if !s.platforms.is_empty() && !s.platforms.contains(&platform) { @@ -280,6 +277,96 @@ fn cloudflared(stack: &devstack_config::Stack) -> Option { /// `dotnet build x.csproj` -> `dotnet`; placeholders and paths are skipped /// (`dotnet {artifact}/X.dll` -> `dotnet`, `{artifact}/x` -> none). +/// The shell a line runs in: `cmd /d /s /c` on Windows, `sh -c` elsewhere. +#[derive(Debug, Clone, Copy)] +enum Shell { + Cmd, + Sh, +} + +/// What `cmd` runs itself (case does not matter there). +const CMD_BUILTINS: &[&str] = &[ + "assoc", "break", "call", "cd", "chdir", "cls", "color", "copy", "date", "del", "dir", "echo", + "endlocal", "erase", "exit", "for", "ftype", "goto", "if", "md", "mkdir", "mklink", "move", + "path", "pause", "popd", "prompt", "pushd", "rd", "rem", "ren", "rename", "rmdir", "set", + "setlocal", "shift", "start", "time", "title", "type", "ver", "verify", "vol", +]; + +/// What `sh` runs itself (POSIX, and `source`, `local` of the common ones). +const SH_BUILTINS: &[&str] = &[ + ".", ":", "[", "alias", "bg", "break", "cd", "command", "continue", "echo", "eval", "exec", + "exit", "export", "false", "fg", "getopts", "hash", "jobs", "kill", "local", "printf", "pwd", + "read", "readonly", "return", "set", "shift", "source", "test", "times", "trap", "true", + "type", "ulimit", "umask", "unalias", "unset", "wait", +]; + +/// The programs a command line runs: the first word of each command between +/// `&&`, `||`, `;`, `|` and `&` (outside quotes; `2>&1` is a redirection), +/// leaving out what the shell runs itself (`set` of `cmd`, `export` of `sh`, +/// #510) and, in `sh`, the assignments before a command (`X=1 npx …`). +fn programs(line: &str, shell: Shell) -> Vec { + let mut parts = Vec::new(); + let mut cur = String::new(); + let mut quote: Option = None; + let mut chars = line.chars().peekable(); + let mut prev = ' '; + while let Some(c) = chars.next() { + match (quote, c) { + (Some(q), _) if c == q => quote = None, + (Some(_), _) => {} + (None, '"') => quote = Some('"'), + (None, '\'') if matches!(shell, Shell::Sh) => quote = Some('\''), + (None, '&') if prev == '>' || prev == '<' => {} + (None, '&' | '|' | ';') => { + if chars.peek() == Some(&c) { + chars.next(); + } + parts.push(std::mem::take(&mut cur)); + prev = ' '; + continue; + } + _ => {} + } + cur.push(c); + prev = c; + } + parts.push(cur); + let mut out = Vec::new(); + for part in parts { + let mut words = part + .split_whitespace() + .map(|w| w.trim_start_matches('(').trim_end_matches(')')) + .filter(|w| !w.is_empty()); + let first = match shell { + Shell::Sh => words.find(|w| !is_assignment(w)), + Shell::Cmd => words.next(), + }; + let Some(first) = first else { continue }; + let builtin = match shell { + Shell::Cmd => CMD_BUILTINS.contains(&first.to_ascii_lowercase().as_str()), + Shell::Sh => SH_BUILTINS.contains(&first), + }; + if builtin { + continue; + } + if let Some(p) = program(first) + && !out.contains(&p) + { + out.push(p); + } + } + out +} + +/// `NAME=value` before a command in `sh`. +fn is_assignment(word: &str) -> bool { + word.split_once('=').is_some_and(|(name, _)| { + !name.is_empty() + && !name.starts_with(|c: char| c.is_ascii_digit()) + && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') + }) +} + fn program(cmd: &str) -> Option { let first = cmd.split_whitespace().next()?.trim_matches(['"', '\'']); if first.contains(['{', '$', '/', '\\']) || first.is_empty() { @@ -891,6 +978,32 @@ mod tests { assert_eq!(program("${NODE} x.js").as_deref(), None); } + /// What the shell runs itself is no program on PATH (#510); the + /// programs after `&&`, `||`, `;` and `|` are. + #[test] + fn the_programs_of_a_line_leave_out_the_shells_builtins() { + let cmd = |l: &str| programs(l, Shell::Cmd); + let sh = |l: &str| programs(l, Shell::Sh); + // The line the stack file reference gives for each shell. + assert_eq!( + cmd(r#"set "E2E_URL={service.web.url}" && npx playwright test"#), + ["npx"] + ); + assert_eq!(sh("E2E_URL={service.web.url} npx playwright test"), ["npx"]); + assert_eq!(sh("export E2E_URL=x && npx playwright test"), ["npx"]); + assert_eq!(cmd("cd web && npm start"), ["npm"]); + assert_eq!(cmd("SET X=1& CALL build.cmd"), Vec::::new()); + assert_eq!(cmd("(cd web && npm ci) || echo failed"), ["npm"]); + assert_eq!(sh("cd web; npm ci | tee log.txt"), ["npm", "tee"]); + assert_eq!(sh(". ./env.sh && dotnet run 2>&1"), ["dotnet"]); + assert_eq!(cmd("dotnet build 2>&1"), ["dotnet"]); + // Quoted separators are text. + assert_eq!(cmd(r#"node -e "a && b" && npm test"#), ["node", "npm"]); + // A builtin of the other shell is a program here. + assert_eq!(sh("set -e && echo ok && cargo build"), ["cargo"]); + assert_eq!(cmd("export X=1"), ["export"]); + } + #[test] fn a_tool_that_hangs_is_given_up_on() { let mut cmd = if cfg!(windows) {