From cac4ed500a6dd8b654525ef555f28725e54ff1f6 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Wed, 21 Oct 2026 09:07:11 +0000 Subject: [PATCH 1/5] test(xtask): read $LASTEXITCODE in any case, and know more pwsh keywords PowerShell variable names are case-insensitive, so a check spelled `$LastExitCode` reads the same status as `$LASTEXITCODE`; the pwsh contract only matched the upper-case spelling and would flag the native command before it. `break`, `continue`, `begin`, `process`, `end`, `param` and `trap` are pwsh keywords, not programs, but a statement opening with one read as a native command. They join PWSH_KEYWORDS, all of whose entries pwsh's tokenizer reports as keywords. Signed-off-by: Roman Inflianskas --- xtask/src/workflow_contract.rs | 59 ++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/xtask/src/workflow_contract.rs b/xtask/src/workflow_contract.rs index 6a8b1a7e3d..2945c767fc 100644 --- a/xtask/src/workflow_contract.rs +++ b/xtask/src/workflow_contract.rs @@ -7506,9 +7506,14 @@ permissions: } /// PowerShell words that open a statement but are not a program to run. - const PWSH_KEYWORDS: [&str; 15] = [ + /// + /// Every entry is a keyword to pwsh's own tokenizer + /// (`TokenTraits::HasTrait(kind, TokenFlags::Keyword)`), so none can name + /// a program at the start of a statement. + const PWSH_KEYWORDS: [&str; 22] = [ "if", "elseif", "else", "foreach", "for", "while", "do", "switch", "function", "try", - "catch", "finally", "throw", "exit", "return", + "catch", "finally", "throw", "exit", "return", "break", "continue", "begin", "process", + "end", "param", "trap", ]; /// Whether a trimmed, comment-free pwsh statement runs an external @@ -7651,12 +7656,19 @@ permissions: is_native_command_line(line) && statements .get(i + 1) - .is_some_and(|next| !next.contains("$LASTEXITCODE")) + .is_some_and(|next| !reads_last_exit_code(next)) }) .map(|(_, line)| line.clone()) .collect() } + /// Whether a pwsh statement mentions `$LASTEXITCODE`. PowerShell variable + /// names are case-insensitive, so `$LastExitCode` and `$lastexitcode` are + /// the same variable and count just the same. + fn reads_last_exit_code(statement: &str) -> bool { + statement.to_ascii_lowercase().contains("$lastexitcode") + } + /// [`unchecked_native_commands`] over a pwsh step's script: its `run: |` /// block, or its one-line `run:` value, which can chain statements with /// `;` just the same. @@ -8023,4 +8035,45 @@ permissions: .collect(); assert_eq!(names, ["Package Windows bundle", "Plain"]); } + + /// PowerShell variable names are case-insensitive: a check spelled + /// `$LastExitCode` reads the same variable as `$LASTEXITCODE`. + #[test] + fn unchecked_native_commands_reads_last_exit_code_in_any_case() { + for check in [ + "if ($LastExitCode -ne 0) { exit $LastExitCode }", + "if ($lastexitcode -ne 0) { exit 1 }", + ] { + let body = ["cargo build", check, "cargo xtask e2e"]; + assert!( + unchecked_native_commands(&body).is_empty(), + "{check:?} reads the status: {:?}", + unchecked_native_commands(&body) + ); + } + } + + /// A statement that opens with a PowerShell keyword runs no program, so + /// it is not a native command left unchecked before `cargo xtask e2e`. + #[test] + fn a_statement_opening_with_a_pwsh_keyword_is_not_a_native_command() { + for statement in [ + "break", + "continue", + "begin {", + "process {", + "end {", + "param([string] $Target)", + "trap { Write-Output 'trapped' }", + ] { + assert!( + !is_native_command_line(statement), + "{statement:?} reads as a native command" + ); + assert!( + unchecked_native_commands(&[statement, "cargo xtask e2e"]).is_empty(), + "{statement:?}" + ); + } + } } From 2bd7c2023dd1cdab84e6de4bb153236c596c7ac9 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Wed, 21 Oct 2026 09:09:20 +0000 Subject: [PATCH 2/5] test(xtask): check a pwsh step whatever block form its run: takes `run_block` only recognised the literal `run: |` header, so a pwsh step written with `run: |-`, `run: >` or `run: >-` fell through to the one-line reading, whose value is the bare indicator, and escaped the contract whatever its script did. Every block-scalar header now counts, with any chomping or indentation indicator and an optional trailing comment. A folded `>` block is read as YAML folds it before the statements are split: two lines at the block's indent are one line of script, so they are one command, not a native command followed by an unchecked one. Signed-off-by: Roman Inflianskas --- xtask/src/workflow_contract.rs | 155 +++++++++++++++++++++++++++++---- 1 file changed, 139 insertions(+), 16 deletions(-) diff --git a/xtask/src/workflow_contract.rs b/xtask/src/workflow_contract.rs index 2945c767fc..6073cfdd95 100644 --- a/xtask/src/workflow_contract.rs +++ b/xtask/src/workflow_contract.rs @@ -1602,9 +1602,9 @@ jobs: assert!( run_block(step).is_some_and(|body| body.iter().any(|l| !l.trim().is_empty())), "{workflow} job `{job}` names a GPU preflight step but no non-empty \ - `run: |` block was found in it, so as far as this check can tell it \ - waits for nothing. Only the literal `run: |` form is recognised: a \ - `run: |-`, `run: >` or one-line `run:` script reads as missing here" + block-scalar `run:` was found in it, so as far as this check can tell \ + it waits for nothing. Only a block-scalar `run:` (`|`, `|-`, `>`, …) \ + is read: a one-line `run:` script reads as missing here" ); } } @@ -2214,20 +2214,22 @@ jobs: step } - /// The lines of a step's `run: |` block, still indented. + /// The lines of a step's block-scalar `run:` (`run: |`, `|-`, `>`, `>-`, + /// …), still indented and, for a folded `>` block, not yet folded: see + /// [`run_script`] for the script the shell is handed. /// - /// Only the literal `run: |` form is recognised. `run: |-`, `run: >` and a - /// one-line `run: cmd` all read as no block, so a check that asserts a step - /// *has* a script relies on every workflow in this repo using `run: |`, and - /// has to say so in its failure message rather than claim the step is empty. + /// Every block-scalar header is recognised (see [`run_block_header`]), but a + /// one-line `run: cmd` reads as no block, so a check that asserts a step + /// *has* a script relies on every such step in this repo using a block + /// scalar, and has to say so in its failure message rather than claim the + /// step is empty. /// /// Trailing blank lines are dropped: they are the separator `step_block` /// lets through, not part of the script, and keeping them would make two /// identical scripts compare unequal over the spacing between steps. fn run_block(step: &str) -> Option> { let lines: Vec<&str> = step.lines().collect(); - let run_at = lines.iter().position(|l| l.trim() == "run: |")?; - let run_indent = indent_of(lines[run_at]); + let (run_at, run_indent, _) = run_block_header(&lines)?; let mut body: Vec<&str> = lines[run_at + 1..] .iter() .take_while(|l| l.trim().is_empty() || indent_of(l) > run_indent) @@ -2239,6 +2241,68 @@ jobs: Some(body) } + /// Where a step's block-scalar `run:` opens: the index of its `run:` line + /// in `lines`, that line's indent, and whether the block is folded (`>`) + /// rather than literal (`|`). + /// + /// Any block-scalar header counts — `|` or `>`, with or without a + /// chomping (`-`/`+`) or indentation (digit) indicator, and with or + /// without a trailing comment — since every one of them hands the step a + /// multi-line script. + fn run_block_header(lines: &[&str]) -> Option<(usize, usize, bool)> { + lines.iter().enumerate().find_map(|(at, line)| { + let indicator = strip_comment(line).trim().strip_prefix("run:")?.trim(); + let folded = match indicator.chars().next()? { + '|' => false, + '>' => true, + _ => return None, + }; + indicator[1..] + .chars() + .all(|c| c == '-' || c == '+' || c.is_ascii_digit()) + .then_some((at, indent_of(line), folded)) + }) + } + + /// The script a step's block-scalar `run:` hands its shell, one entry per + /// line: [`run_block`]'s lines, folded the way YAML folds a `>` block. + /// + /// In a folded block a line break between two lines at the block's own + /// indent becomes a space, so two such lines are ONE line of script; a + /// blank line ends the line, and a more-indented line keeps the breaks + /// around it. A literal `|` block is the script as written. + fn run_script(step: &str) -> Option> { + let lines: Vec<&str> = step.lines().collect(); + let (_, _, folded) = run_block_header(&lines)?; + let body = run_block(step)?; + if !folded { + return Some(body.iter().map(|line| (*line).to_owned()).collect()); + } + let indent = body + .iter() + .find(|line| !line.trim().is_empty()) + .map_or(0, |line| indent_of(line)); + let mut script: Vec = Vec::new(); + let mut folds_onto_last = false; + for line in body { + if line.trim().is_empty() { + script.push(String::new()); + folds_onto_last = false; + continue; + } + let at_block_indent = indent_of(line) == indent; + match script.last_mut() { + Some(last) if at_block_indent && folds_onto_last => { + last.push(' '); + last.push_str(line.trim_start()); + } + _ => script.push(line.to_owned()), + } + folds_onto_last = at_block_indent; + } + Some(script) + } + /// The non-comment lines of a step's `run: |` block, joined. /// /// A preflight step's body is the only place a lane can still spell its own @@ -2422,7 +2486,8 @@ jobs: /// Two shapes carry shell: a plain `run: |` step, and the WSL lanes, which /// hand a here-string to `Invoke-WslBash.ps1`. A step that is PowerShell end /// to end yields `None`; `preflight_powershell_script` carries those. Like - /// every caller of [`run_block`], only the literal `run: |` form is read. + /// every caller of [`run_block`], only a block-scalar `run:` is read, and a + /// folded `>` one line by line, without folding. fn preflight_shell_script(step: &str) -> Option { let body = run_block(step)?; let body = match body @@ -7669,12 +7734,15 @@ permissions: statement.to_ascii_lowercase().contains("$lastexitcode") } - /// [`unchecked_native_commands`] over a pwsh step's script: its `run: |` - /// block, or its one-line `run:` value, which can chain statements with - /// `;` just the same. + /// [`unchecked_native_commands`] over a pwsh step's script: its + /// block-scalar `run:` (`|`, `|-`, `>`, `>-`, …, folded as YAML folds it), + /// or its one-line `run:` value, which can chain statements with `;` just + /// the same. fn unchecked_native_commands_in_step(step: &str) -> Vec { - match run_block(step) { - Some(body) => unchecked_native_commands(&body), + match run_script(step) { + Some(script) => { + unchecked_native_commands(&script.iter().map(String::as_str).collect::>()) + } None => unchecked_native_commands(&[step_key(step, "run").as_str()]), } } @@ -8076,4 +8144,59 @@ permissions: ); } } + + /// Every block-scalar form of `run:` hands a pwsh step a script, so every + /// form is checked. Read only as `run: |`, a `run: |-`, `run: >` or + /// `run: >-` step fell through to the one-line reading, whose value is the + /// bare indicator, and so escaped the contract whatever its script did. + #[test] + fn unchecked_native_commands_reads_every_block_scalar_run_form() { + // Literal forms: each line is a line of script. + for header in ["run: |", "run: |-", "run: |+", "run: |2", "run: |- # note"] { + let step = format!( + " - name: Self-tests\n shell: pwsh\n {header}\n \ + python a.py --self-test\n python b.py --self-test\n" + ); + assert_eq!( + unchecked_native_commands_in_step(&step), + ["python a.py --self-test"], + "{header}" + ); + } + // Folded forms: a blank line separates two lines of script. + for header in ["run: >", "run: >-", "run: >+"] { + let step = format!( + " - name: Self-tests\n shell: pwsh\n {header}\n \ + python a.py --self-test\n\n python b.py --self-test\n" + ); + assert_eq!( + unchecked_native_commands_in_step(&step), + ["python a.py --self-test"], + "{header}" + ); + } + } + + /// A folded `run: >` block is read as YAML folds it: lines at the block's + /// indent join with a space into one line of script, a blank line starts + /// a new one, and a more-indented line keeps its own line. Taken line by + /// line instead, the first two lines below would read as two commands and + /// the first as unchecked, where pwsh is handed one `cargo` command. + #[test] + fn a_folded_run_block_is_read_as_yaml_folds_it() { + let step = " - shell: pwsh\n name: Build\n run: >-\n \ + cargo build\n --locked\n\n if ($LASTEXITCODE) {\n \ + exit 1\n }\n"; + assert_eq!( + run_script(step).expect("the step has a block-scalar run"), + [ + " cargo build --locked", + "", + " if ($LASTEXITCODE) {", + " exit 1", + " }", + ] + ); + assert!(unchecked_native_commands_in_step(step).is_empty()); + } } From f58d2f04eaef50571dfcb2d9328835df5f67a326 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Wed, 21 Oct 2026 09:10:33 +0000 Subject: [PATCH 3/5] test(xtask): find a pwsh step whose first key is shell: or run: The pwsh contract matched `shell: pwsh` and `run: |` only as sibling keys. Written as the step's first key, `- shell: pwsh` was never found as a pwsh step, and `- run: |` read as no script at all; both took the step out of the contract. A quoted `shell: 'pwsh'` is matched too. Signed-off-by: Roman Inflianskas --- xtask/src/workflow_contract.rs | 71 ++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/xtask/src/workflow_contract.rs b/xtask/src/workflow_contract.rs index 6073cfdd95..de328a0f9f 100644 --- a/xtask/src/workflow_contract.rs +++ b/xtask/src/workflow_contract.rs @@ -2242,8 +2242,9 @@ jobs: } /// Where a step's block-scalar `run:` opens: the index of its `run:` line - /// in `lines`, that line's indent, and whether the block is folded (`>`) - /// rather than literal (`|`). + /// in `lines`, the column of the `run` key, and whether the block is + /// folded (`>`) rather than literal (`|`). The key may be the step's + /// first (`- run: |`), two columns in from the dash. /// /// Any block-scalar header counts — `|` or `>`, with or without a /// chomping (`-`/`+`) or indentation (digit) indicator, and with or @@ -2251,7 +2252,12 @@ jobs: /// multi-line script. fn run_block_header(lines: &[&str]) -> Option<(usize, usize, bool)> { lines.iter().enumerate().find_map(|(at, line)| { - let indicator = strip_comment(line).trim().strip_prefix("run:")?.trim(); + let code = strip_comment(line).trim(); + let (key, key_column) = match code.strip_prefix("- ") { + Some(rest) => (rest.trim_start(), indent_of(line) + 2), + None => (code, indent_of(line)), + }; + let indicator = key.strip_prefix("run:")?.trim(); let folded = match indicator.chars().next()? { '|' => false, '>' => true, @@ -2260,7 +2266,7 @@ jobs: indicator[1..] .chars() .all(|c| c == '-' || c == '+' || c.is_ascii_digit()) - .then_some((at, indent_of(line), folded)) + .then_some((at, key_column, folded)) }) } @@ -7548,20 +7554,34 @@ permissions: /// Every `shell: pwsh` step in `text` as `(name, step block)`. /// - /// The step is found from its `shell: pwsh` line by walking up to the - /// nearest `- ` item shallower than it, so the key order inside the step - /// does not matter. + /// The step is found from its `shell: pwsh` line: that line itself when + /// `shell:` is the step's first key (`- shell: pwsh`), and otherwise by + /// walking up to the nearest `- ` item shallower than it, so the key order + /// inside the step does not matter. fn pwsh_steps(text: &str) -> Vec<(String, String)> { let lines: Vec<&str> = text.lines().collect(); let mut steps = Vec::new(); for (at, line) in lines.iter().enumerate() { - if strip_comment(line).trim() != "shell: pwsh" { + let code = strip_comment(line).trim(); + let (key, is_item) = match code.strip_prefix("- ") { + Some(rest) => (rest.trim_start(), true), + None => (code, false), + }; + if key + .strip_prefix("shell:") + .is_none_or(|shell| strip_quotes(shell.trim()) != "pwsh") + { continue; } let shell_indent = indent_of(line); - let Some(start) = (0..at).rev().find(|&i| { - indent_of(lines[i]) < shell_indent && lines[i].trim_start().starts_with("- ") - }) else { + let start = if is_item { + Some(at) + } else { + (0..at).rev().find(|&i| { + indent_of(lines[i]) < shell_indent && lines[i].trim_start().starts_with("- ") + }) + }; + let Some(start) = start else { continue; }; let step = step_block(&lines, start); @@ -8199,4 +8219,33 @@ permissions: ); assert!(unchecked_native_commands_in_step(step).is_empty()); } + + /// `shell:` and `run:` can each be a step's first key, on its `- ` line; + /// the step is pwsh, and its script checked, just the same. Matched only + /// as sibling keys, `- shell: pwsh` was never found as a pwsh step and + /// `- run: |` read as no script at all. + #[test] + fn a_pwsh_step_is_checked_whichever_key_comes_first() { + let script = " python a.py --self-test\n \ + python b.py --self-test\n"; + for step in [ + format!(" - shell: pwsh\n name: Self-tests\n run: |\n{script}"), + format!(" - shell: 'pwsh'\n name: Self-tests\n run: |\n{script}"), + format!(" - run: |\n{script} shell: pwsh\n name: Self-tests\n"), + ] { + let text = format!("jobs:\n win:\n runs-on: windows-latest\n steps:\n{step}"); + let found: Vec<(String, Vec)> = pwsh_steps(&text) + .into_iter() + .map(|(name, step)| (name, unchecked_native_commands_in_step(&step))) + .collect(); + assert_eq!( + found, + [( + "Self-tests".to_owned(), + vec!["python a.py --self-test".to_owned()] + )], + "{step}" + ); + } + } } From db2b44c0e3fe104107bbe5dba980f1ad2d49afb4 Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Wed, 21 Oct 2026 12:10:29 +0000 Subject: [PATCH 4/5] test(xtask): accept a later $LASTEXITCODE check that nothing can pre-empt The pwsh contract accepted an exit-code check only in the statement right after a native command, so a `Write-Output` or a plain assignment before the check flagged a command whose status was in fact read. A check further on now counts when every statement between leaves `$LASTEXITCODE` alone. That is defined narrowly: outside single quotes, no `(`, `{`, `&` or expanding `@"` here-string, and a pipeline of a plain value or assignment of one, followed only by cmdlets that write output or files. An assignment to `$LASTEXITCODE` is not a read. Running off the end of the body is one, since GitHub's wrapper exits with `$LASTEXITCODE`, so a last native command followed only by a `Write-Output` is no longer flagged either. Every case in the tests, on both sides of the rule, was run through pwsh first. Signed-off-by: Roman Inflianskas --- xtask/src/workflow_contract.rs | 258 +++++++++++++++++++++++++++++++-- 1 file changed, 248 insertions(+), 10 deletions(-) diff --git a/xtask/src/workflow_contract.rs b/xtask/src/workflow_contract.rs index de328a0f9f..cb62ac53bd 100644 --- a/xtask/src/workflow_contract.rs +++ b/xtask/src/workflow_contract.rs @@ -7730,23 +7730,172 @@ permissions: } /// The native-command statements in a pwsh body whose exit status - /// nothing reads: not the body's last statement, and not followed by a - /// statement that inspects `$LASTEXITCODE`. + /// nothing reads (see [`exit_status_is_read`]). fn unchecked_native_commands(body: &[&str]) -> Vec { let statements = pwsh_statements(body); statements .iter() .enumerate() .filter(|(i, line)| { - is_native_command_line(line) - && statements - .get(i + 1) - .is_some_and(|next| !reads_last_exit_code(next)) + is_native_command_line(line) && !exit_status_is_read(&statements[i + 1..]) }) .map(|(_, line)| line.clone()) .collect() } + /// Whether a native command's exit status is still in `$LASTEXITCODE` + /// when something reads it, given the statements that follow it. + /// + /// The statements are walked in order: + /// + /// - one that assigns `$LASTEXITCODE` overwrites the status: unread; + /// - one that otherwise mentions `$LASTEXITCODE` reads it: read; + /// - one that [leaves it alone](leaves_last_exit_code_alone) is passed + /// over; + /// - anything else might run another program, or leave the script, before + /// the status is read: unread. + /// + /// Running off the end is a read: GitHub's pwsh wrapper appends + /// `if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE }`, + /// so the step itself exits with the status. + fn exit_status_is_read(after: &[String]) -> bool { + for statement in after { + if assigns_last_exit_code(statement) { + return false; + } + if reads_last_exit_code(statement) { + return true; + } + if !leaves_last_exit_code_alone(statement) { + return false; + } + } + true + } + + /// Whether a pwsh statement assigns `$LASTEXITCODE` (`= 0`, `+= 1`, with + /// or without a `global:`/`script:`/`local:` scope), so the status it + /// held is gone. + fn assigns_last_exit_code(statement: &str) -> bool { + let lower = statement.trim().to_ascii_lowercase(); + let Some(variable) = lower.strip_prefix('$') else { + return false; + }; + let variable = ["global:", "script:", "local:"] + .iter() + .find_map(|scope| variable.strip_prefix(scope)) + .unwrap_or(variable); + variable.strip_prefix("lastexitcode").is_some_and(|rest| { + let rest = rest.trim_start(); + ["=", "+=", "-=", "*=", "/=", "%=", "??="] + .iter() + .any(|operator| rest.starts_with(operator)) + }) + } + + /// Cmdlets that only write output, a stream or a file: none of them runs + /// a program or sets `$LASTEXITCODE`. + const PWSH_INERT_CMDLETS: [&str; 11] = [ + "Write-Output", + "Write-Host", + "Write-Information", + "Write-Verbose", + "Write-Debug", + "Write-Warning", + "Out-File", + "Out-Null", + "Out-Host", + "Add-Content", + "Set-Content", + ]; + + /// Whether a pwsh statement can neither run a program nor touch + /// `$LASTEXITCODE`, so a native command's status survives it. + /// + /// Deliberately narrow, and erring towards "might": outside single + /// quotes, which are literal, the statement has no `(` (a subexpression + /// `$(…)` or a `(…)` argument runs a command, even inside double quotes), + /// no `{` (a script block), no `&` (the call operator, `&&`) and no + /// expanding `@"` here-string. What is left is a pipeline whose first + /// element is a value — a variable, a string or a number — or an + /// assignment of one, or one of [`PWSH_INERT_CMDLETS`], and whose every + /// later element is one of those cmdlets. Anything else, a keyword + /// (`exit`, a loop's closing `}`) or a function call included, is not. + fn leaves_last_exit_code_alone(statement: &str) -> bool { + let code = without_single_quoted_text(statement); + if code.contains(['(', '{', '&']) || code.contains("@\"") { + return false; + } + code.split('|').enumerate().all(|(at, element)| { + let element = element.trim(); + let first = element.split_whitespace().next().unwrap_or_default(); + PWSH_INERT_CMDLETS + .iter() + .any(|cmdlet| cmdlet.eq_ignore_ascii_case(first)) + || (at == 0 && is_value_or_assignment(element)) + }) + } + + /// Whether a pipeline element is a plain value (`$x`, `"text"`, `'text'`, + /// `42`, a literal here-string) or `$name = …` of one, with a target other + /// than `$LASTEXITCODE`. A `+=`-style operator counts as `=`. + fn is_value_or_assignment(element: &str) -> bool { + let value = match element.split_once('=') { + Some((target, value)) if element.starts_with('$') => { + let target = target + .trim_end() + .trim_end_matches(['+', '-', '*', '/', '%', '?']) + .trim_end(); + let name = target.trim_start_matches('$'); + if name.is_empty() + || !name + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == ':') + || assigns_last_exit_code(element) + { + return false; + } + // `$a = $b = cargo …` chains: the value must pass in turn. + return is_value_or_assignment(value.trim()); + } + _ => element, + }; + value.starts_with(['$', '"', '\'']) + || value.starts_with("@'") + || value.starts_with(|c: char| c.is_ascii_digit()) + } + + /// `statement` with the text inside its single-quoted strings removed: + /// that text is literal, so nothing in it can run. Double-quoted strings + /// are kept whole, since `$(…)` inside one does run, and a `'` inside one + /// is just a character. + fn without_single_quoted_text(statement: &str) -> String { + let mut kept = String::new(); + let mut quote: Option = None; + let mut chars = statement.chars(); + while let Some(c) = chars.next() { + match (quote, c) { + (Some('\''), '\'') | (Some('"'), '"') => { + quote = None; + kept.push(c); + } + (Some('\''), _) => {} + (_, '`') => { + kept.push(c); + if let Some(escaped) = chars.next() { + kept.push(escaped); + } + } + (None, '\'' | '"') => { + quote = Some(c); + kept.push(c); + } + _ => kept.push(c), + } + } + kept + } + /// Whether a pwsh statement mentions `$LASTEXITCODE`. PowerShell variable /// names are case-insensitive, so `$LastExitCode` and `$lastexitcode` are /// the same variable and count just the same. @@ -7802,9 +7951,10 @@ permissions: /// python b.py --self-test # exits 0 /// ``` /// - /// the step is green. Every native command that is not the body's last - /// statement must therefore have its `$LASTEXITCODE` read on the next - /// line, or be split into its own step (#633), or be allow-listed above + /// the step is green. Every native command must therefore have its + /// `$LASTEXITCODE` read before anything that could run another program or + /// reset it (see [`exit_status_is_read`]; the end of the body counts as a + /// read), or be split into its own step (#633), or be allow-listed above /// with the reason its failure still surfaces. #[test] fn no_pwsh_step_lets_a_failing_native_command_pass_unseen() { @@ -7850,7 +8000,8 @@ permissions: "these `shell: pwsh` steps run a native command whose failure the step \ would not report, because pwsh carries on and the step takes only the last \ command's exit code — split each into its own step, or check \ - `$LASTEXITCODE` right after it:\n {}", + `$LASTEXITCODE` before anything that could run another program or \ + reset it:\n {}", offenders.join("\n ") ); // An allow-list entry that no longer matches anything exempts nothing @@ -8248,4 +8399,91 @@ permissions: ); } } + + /// A `$LASTEXITCODE` check need not be the very next statement: what + /// sits between may only be statements that can neither run a program + /// nor touch `$LASTEXITCODE`. Each case below was run through pwsh after + /// a native command exiting 3, under GitHub's wrapper: in every accepted + /// case the check still saw 3. + #[test] + fn a_later_last_exit_code_check_counts_when_nothing_between_can_reset_it() { + let check = "if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }"; + for between in [ + &["Write-Output 'built'"][..], + &["Write-Host \"built $target\""], + &["$artifact = 'dist/rocm.zip'"], + &["$env:RESULT = $artifact", "$count += 1"], + &["\"k=v\" | Out-File -FilePath $env:GITHUB_OUTPUT -Append"], + &[ + "@'", + "## Summary (cargo build; git status)", + "'@ | Add-Content $env:GITHUB_STEP_SUMMARY", + ], + &["Write-Output 'one'", "$x = 'two'", "Write-Host $x"], + &["Write-Output 'done (all targets) & {more}'"], + ] { + let body: Vec<&str> = std::iter::once("cargo build") + .chain(between.iter().copied()) + .chain([check, "cargo xtask e2e"]) + .collect(); + assert!( + unchecked_native_commands(&body).is_empty(), + "{between:?} leaves the status for the check: {:?}", + unchecked_native_commands(&body) + ); + } + // The end of the body is a read too: GitHub's wrapper exits with + // `$LASTEXITCODE`, which a trailing cmdlet leaves alone. + assert!(unchecked_native_commands(&["cargo build", "Write-Output 'done'"]).is_empty()); + } + + /// The other side of the rule: a statement that can run a program, leave + /// the script, or set `$LASTEXITCODE` between a native command and the + /// check leaves that command unchecked. Run through pwsh after a native + /// command exiting 3 (`.\scripts\collect.ps1` standing for a script + /// that ends `exit 0`), each of these lost the 3 before the check — + /// another status took its place, or the step left first — except the + /// two marked: one this check cannot tell from a statement that does, + /// and a loop's closing brace, which is not a statement on its own. + #[test] + fn a_later_last_exit_code_check_does_not_count_past_a_statement_that_can_reset_it() { + let check = "if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }"; + for between in [ + &["git rev-parse HEAD"][..], + &["Write-Output (git rev-parse HEAD)"], + &["Write-Output \"sha: $(git rev-parse HEAD)\""], + &["Write-Output \"it's $(git rev-parse HEAD)\""], + &["& .\\scripts\\collect.ps1"], + &["$LASTEXITCODE = 0"], + &["$global:LastExitCode = 0"], + &["$sha = git rev-parse HEAD"], + &["$a = $b = git rev-parse HEAD"], + &["Write-Output 'x' | git hash-object --stdin"], + // Conservative: runs git only if Write-Output fails. + &["Write-Output 'x' || git rev-parse HEAD"], + &["@\"", "$(git rev-parse HEAD)", "\"@ | Out-File summary.md"], + &["Invoke-Expression 'git rev-parse HEAD'"], + &["Write-Output 'x' && git rev-parse HEAD"], + // A hashtable value is a statement, and runs. + &["Write-Output @{ sha = git rev-parse HEAD }"], + &["Write-Output @\"", "$(git rev-parse HEAD)", "\"@"], + &["exit 0"], + // A loop's closing brace: the check reads the last pass only. + &["}"], + &["Write-Output 'fine'", "git rev-parse HEAD"], + ] { + let body: Vec<&str> = std::iter::once("cargo build") + .chain(between.iter().copied()) + .chain([check, "cargo xtask e2e"]) + .collect(); + assert!( + unchecked_native_commands(&body).contains(&"cargo build".to_owned()), + "{between:?} can reset the status, yet `cargo build` reads as checked" + ); + } + assert_eq!( + unchecked_native_commands(&["cargo build", "exit 0"]), + ["cargo build"] + ); + } } From 0e13ba75f3afa10f8eb608dfc14e27be423efc8c Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Fri, 6 Nov 2026 10:23:05 +0200 Subject: [PATCH 5/5] test(xtask): treat an open quote as unknown, and honour a >N indicator From review of the pwsh contract: - `without_single_quoted_text` dropped everything after a `'` that never closed, so a stray `'@ | git hash-object --stdin` reduced to a lone `'`, read as a plain value, and a native command before it counted as checked although git resets `$LASTEXITCODE`. A statement with a quote left open is now never inert, and never a read. - `$LASTEXITCODE` counted as a read inside single quotes and as the prefix of a longer name (`$LASTEXITCODEX`). Only the whole name outside single quotes counts now. - A folded `run: >2` block folded at its first line's indent. It now folds at the `run` key's column plus the indicator, as YAML does; PyYAML reads the new fixtures the same way. - The statements and messages that #598 wrote while `run_block` read only `run: |` now say that every block-scalar form is read, and `run_block_header` notes that it takes a `run:` key at any depth. No workflow in the repository changes verdict: the contract still finds the same pwsh steps and the same allow-listed commands. Signed-off-by: Roman Inflianskas --- xtask/src/workflow_contract.rs | 127 ++++++++++++++++++++++++++------- 1 file changed, 103 insertions(+), 24 deletions(-) diff --git a/xtask/src/workflow_contract.rs b/xtask/src/workflow_contract.rs index cb62ac53bd..e3149139dc 100644 --- a/xtask/src/workflow_contract.rs +++ b/xtask/src/workflow_contract.rs @@ -1228,8 +1228,8 @@ jobs: .find_map(|(name, step)| (name == "Decide whether to run").then_some(step)) .expect("ci.yml's `e2e` job has a \"Decide whether to run\" step"); let mut script = dedent(&run_block(&step).expect( - "the E2E gate step has no `run: |` block; `run_block` reads only that \ - literal form, not `run: |-`, `run: >` or a one-line `run:`", + "the E2E gate step has no block-scalar `run:`; `run_block` reads `run: |`, \ + `run: |-`, `run: >` and the like, but not a one-line `run:`", )) .expect("the gate step's script is not empty"); let values = [ @@ -2229,7 +2229,7 @@ jobs: /// identical scripts compare unequal over the spacing between steps. fn run_block(step: &str) -> Option> { let lines: Vec<&str> = step.lines().collect(); - let (run_at, run_indent, _) = run_block_header(&lines)?; + let (run_at, run_indent, _, _) = run_block_header(&lines)?; let mut body: Vec<&str> = lines[run_at + 1..] .iter() .take_while(|l| l.trim().is_empty() || indent_of(l) > run_indent) @@ -2242,15 +2242,21 @@ jobs: } /// Where a step's block-scalar `run:` opens: the index of its `run:` line - /// in `lines`, the column of the `run` key, and whether the block is - /// folded (`>`) rather than literal (`|`). The key may be the step's - /// first (`- run: |`), two columns in from the dash. + /// in `lines`, the column of the `run` key, whether the block is folded + /// (`>`) rather than literal (`|`), and its indentation indicator, if it + /// has one. The key may be the step's first (`- run: |`), two columns in + /// from the dash. /// /// Any block-scalar header counts — `|` or `>`, with or without a /// chomping (`-`/`+`) or indentation (digit) indicator, and with or /// without a trailing comment — since every one of them hands the step a /// multi-line script. - fn run_block_header(lines: &[&str]) -> Option<(usize, usize, bool)> { + /// + /// The first `run:` key at any depth is taken, so a `run:` nested under + /// some other key (an action's `with:` input) would be read as the + /// step's script. No workflow here has one; a step that does would need + /// this to match only at the step's own key column. + fn run_block_header(lines: &[&str]) -> Option<(usize, usize, bool, Option)> { lines.iter().enumerate().find_map(|(at, line)| { let code = strip_comment(line).trim(); let (key, key_column) = match code.strip_prefix("- ") { @@ -2263,10 +2269,18 @@ jobs: '>' => true, _ => return None, }; - indicator[1..] + let modifiers = &indicator[1..]; + if !modifiers .chars() .all(|c| c == '-' || c == '+' || c.is_ascii_digit()) - .then_some((at, key_column, folded)) + { + return None; + } + let indent = modifiers + .chars() + .find_map(|c| c.to_digit(10)) + .map(|digit| digit as usize); + Some((at, key_column, folded, indent)) }) } @@ -2277,17 +2291,25 @@ jobs: /// indent becomes a space, so two such lines are ONE line of script; a /// blank line ends the line, and a more-indented line keeps the breaks /// around it. A literal `|` block is the script as written. + /// + /// The block's indent is the `run` key's column plus its indentation + /// indicator (`>2`) when it has one, as YAML reads it; otherwise the + /// indent of its first non-blank line. fn run_script(step: &str) -> Option> { let lines: Vec<&str> = step.lines().collect(); - let (_, _, folded) = run_block_header(&lines)?; + let (_, run_indent, folded, indicator) = run_block_header(&lines)?; let body = run_block(step)?; if !folded { return Some(body.iter().map(|line| (*line).to_owned()).collect()); } - let indent = body - .iter() - .find(|line| !line.trim().is_empty()) - .map_or(0, |line| indent_of(line)); + let indent = indicator.map_or_else( + || { + body.iter() + .find(|line| !line.trim().is_empty()) + .map_or(0, |line| indent_of(line)) + }, + |digit| run_indent + digit, + ); let mut script: Vec = Vec::new(); let mut folds_onto_last = false; for line in body { @@ -2309,7 +2331,7 @@ jobs: Some(script) } - /// The non-comment lines of a step's `run: |` block, joined. + /// The non-comment lines of a step's block-scalar `run:`, joined. /// /// A preflight step's body is the only place a lane can still spell its own /// floor (the WSL `${…:-8}` default, the PowerShell `else { 8 }`), and a @@ -2924,8 +2946,8 @@ jobs: .and_then(|block| dedent(&block)) .unwrap_or_else(|| { panic!( - "{workflow}'s clock-settling step has no `run: |` body (only the literal \ - `run: |` form is read; `run: |-`, `run: >` and one-line `run:` are not)" + "{workflow}'s clock-settling step has no block-scalar `run:` body \ + (`run: |`, `run: >` and the like are read; a one-line `run:` is not)" ) }); bodies.push((workflow, body)); @@ -7624,7 +7646,7 @@ permissions: && !PWSH_KEYWORDS.contains(&first.to_ascii_lowercase().as_str()) } - /// The statements of a pwsh `run: |` body, in order. + /// The statements of a pwsh `run:` script, in order. /// /// A line is split at its top-level `;`s (see /// [`split_top_level_statements`]), so `Write-Output 'ok'; sccache @@ -7812,8 +7834,9 @@ permissions: /// Whether a pwsh statement can neither run a program nor touch /// `$LASTEXITCODE`, so a native command's status survives it. /// - /// Deliberately narrow, and erring towards "might": outside single - /// quotes, which are literal, the statement has no `(` (a subexpression + /// Deliberately narrow, and erring towards "might": every quote in the + /// statement closes, and outside single quotes, which are literal, it + /// has no `(` (a subexpression /// `$(…)` or a `(…)` argument runs a command, even inside double quotes), /// no `{` (a script block), no `&` (the call operator, `&&`) and no /// expanding `@"` here-string. What is left is a pipeline whose first @@ -7822,7 +7845,9 @@ permissions: /// later element is one of those cmdlets. Anything else, a keyword /// (`exit`, a loop's closing `}`) or a function call included, is not. fn leaves_last_exit_code_alone(statement: &str) -> bool { - let code = without_single_quoted_text(statement); + let Some(code) = without_single_quoted_text(statement) else { + return false; + }; if code.contains(['(', '{', '&']) || code.contains("@\"") { return false; } @@ -7869,7 +7894,11 @@ permissions: /// that text is literal, so nothing in it can run. Double-quoted strings /// are kept whole, since `$(…)` inside one does run, and a `'` inside one /// is just a character. - fn without_single_quoted_text(statement: &str) -> String { + /// + /// `None` when a quote is still open at the end: what it hides cannot be + /// told from literal text, so the caller must assume the worst. A stray + /// `'@ | git …` would otherwise reduce to a lone `'`, a plain value. + fn without_single_quoted_text(statement: &str) -> Option { let mut kept = String::new(); let mut quote: Option = None; let mut chars = statement.chars(); @@ -7893,14 +7922,24 @@ permissions: _ => kept.push(c), } } - kept + quote.is_none().then_some(kept) } /// Whether a pwsh statement mentions `$LASTEXITCODE`. PowerShell variable /// names are case-insensitive, so `$LastExitCode` and `$lastexitcode` are /// the same variable and count just the same. + /// + /// Only a mention outside single quotes counts (`'$LASTEXITCODE'` is + /// literal text), and only of the whole name (`$LASTEXITCODEX` is another + /// variable). A statement with a quote left open reads nothing. fn reads_last_exit_code(statement: &str) -> bool { - statement.to_ascii_lowercase().contains("$lastexitcode") + let Some(code) = without_single_quoted_text(statement) else { + return false; + }; + let code = code.to_ascii_lowercase(); + code.match_indices("$lastexitcode").any(|(at, name)| { + !code[at + name.len()..].starts_with(|c: char| c.is_ascii_alphanumeric() || c == '_') + }) } /// [`unchecked_native_commands`] over a pwsh step's script: its @@ -8371,6 +8410,37 @@ permissions: assert!(unchecked_native_commands_in_step(step).is_empty()); } + /// A folded block with an indentation indicator (`>2`) sets its own + /// indent: the run key's column plus the digit, not the indent of its + /// first line. Here the first line is more indented than that, so it + /// keeps its own line, and the two lines at the block's indent fold into + /// one. PyYAML reads both fixtures this way. + #[test] + fn a_folded_run_block_honours_its_indentation_indicator() { + let step = " - shell: pwsh\n name: Build\n run: >2\n \ + python a.py --self-test\n python b.py --self-test\n \ + python c.py\n"; + assert_eq!( + run_script(step).expect("the step has a block-scalar run"), + [ + " python a.py --self-test", + " python b.py --self-test python c.py", + ] + ); + assert_eq!( + unchecked_native_commands_in_step(step), + ["python a.py --self-test"] + ); + // `run:` as the step's first key: the key, not the dash, sets the + // column the digit counts from. + let step = + " - run: >2\n a1\n b\n c\n shell: pwsh\n"; + assert_eq!( + run_script(step).expect("the step has a block-scalar run"), + [" a1", " b c"] + ); + } + /// `shell:` and `run:` can each be a step's first key, on its `- ` line; /// the step is pwsh, and its script checked, just the same. Matched only /// as sibling keys, `- shell: pwsh` was never found as a pwsh step and @@ -8471,6 +8541,15 @@ permissions: // A loop's closing brace: the check reads the last pass only. &["}"], &["Write-Output 'fine'", "git rev-parse HEAD"], + // An unclosed `'` hides the rest of the statement, so the + // statement is not known to be inert: here a stray closing + // marker would leave just `'`, which reads as a plain value. + &["'@ | git hash-object --stdin"], + &["Write-Output 'open | git hash-object --stdin"], + // `$LASTEXITCODE` inside single quotes is literal text, and + // `$LASTEXITCODEX` is another variable: neither reads the status. + &["git log --grep '$LASTEXITCODE'"], + &["Invoke-Thing $LASTEXITCODEX"], ] { let body: Vec<&str> = std::iter::once("cargo build") .chain(between.iter().copied())