Skip to content

One shell reader, three dialects: read cmd steps and batch files as cmd, PowerShell scripts as PowerShell - #370

Merged
lefticus merged 4 commits into
cpp-best-practices:mainfrom
leftibot:shell-dialects
Nov 24, 2026
Merged

lefticus merged 4 commits into
cpp-best-practices:mainfrom
leftibot:shell-dialects

Conversation

@leftibot

Copy link
Copy Markdown
Collaborator

Follow-up to #300 (principle 8: the shell reader is the hub). One shell reader now reads POSIX sh, PowerShell and Windows's cmd, as three dialects of the same lexer and parser.

Why

A GitHub shell: cmd step was split by the POSIX shell's rules:

  • In cmd a backslash separates path parts, but it was read as an escape, so build\Release\app.exe ran buildReleaseapp.exe.
  • if not exist build mkdir build ran a program named not.
  • ^ continued nothing.

12 corpus projects use shell: cmd, among them fmt, Catch2, llama.cpp, opam, pgvector and h3.

Commits

  1. Dialect in place of the powershell: bool flag. The flag was threaded through parse, the lexer, the parser, split_script, script_facts and the CI readers, so a third shell would have needed every one of them to change. A Dialect now says how its shell reads a script: its escape character, compound commands, quoting, substitutions, the characters that end a word, comments, and blocks. POSIX and POWERSHELL read exactly as before. This commit changes no behaviour.
  2. CMD, a third dialect of the same reader, used for GitHub's shell: cmd:
    • Escapes: ^ escapes and continues a line, except inside double quotes, where it is text. Only " quotes.
    • Comments: REM and :: start one where a command starts.
    • Text: there is no ; operator and no $ substitution.
    • Blocks: parentheses hold blocks where cmd opens one: where a command starts, after an if's test, else or do. Elsewhere they are text, so %ProgramFiles(x86)% stays one word.
    • Commands: a line's command is found past @, an if's test, a for's loop, else and call. A label runs nothing.
  3. Batch files and PowerShell scripts that CI runs are followed by the existing script-file expander, each read in its dialect via Script.dialect. Batch files are reached as scripts\build.bat, call ci\setup.bat or cmd /c build.bat; PowerShell scripts as ./ci/test.ps1 or pwsh -File ci/test.ps1.
  4. Fixes the corpus showed after commit 3:
    • Batch files the project doesn't hold: these are mostly programs (npm.cmd) or files CI generates (Composer's vendor/bin/phpunit.bat), so they are neither followed nor reported as missing scripts.
    • Heuristic scanning: Windows jobs in Azure Pipelines and AppVeyor files, which are scanned line by line, are read as cmd, so .\Build\runbuild.ps1 keeps its backslashes.

Corpus: 10 of 235 projects differ; no check's result changes

  • glib: its GitLab Windows jobs' .gitlab-ci\test-msvc.bat is followed, showing their Python and meson Debug build.
  • ghostty: its cmd steps now show MSVC, Debug and Release builds, and the Zig version.
  • llama.cpp: the SYCL Windows job's win-build-sycl.bat is followed, showing icx (Intel) and Release. It also lists gcc, because the existing compiler detection matches its "MinGW Makefiles" generator; that detection quirk predates this PR.
  • opam: its Windows cmd steps show gcc.
  • Catch2: its AppVeyor batch scripts are followed and run Python.
  • Humanizer, luarocks, serilog, starship: PowerShell and batch scripts they run are followed.
  • docker-code-server: its Jenkinsfile is scanned heuristically, and the existing OS detection takes it for a Windows job, so it is now read as cmd and one line yields a Python run. The OS detection is the cause, and it predates this PR.

Verified: all local gates pass. Every new behaviour was test first, with the RED run seen.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt

leftibot and others added 4 commits November 23, 2026 16:53
The shell reader read PowerShell through a `powershell: bool` threaded from `parse` through the lexer, the parser,
`split_script`, `script_facts` and the CI readers, each branch asking it what it meant: the escape character, whether
compound commands are parsed, whether `$'…'` and backquoted substitution are quoting. A third shell would have had
every one of them learn of it.

A `Dialect` now says those things, and the reader asks it: `POSIX` and `POWERSHELL` read as before. Callers name the
dialect (`dialect=POWERSHELL`). No behaviour changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
…itHub's `shell: cmd`

A `shell: cmd` step was split by the POSIX shell's rules. A backslash, a path separator in cmd, escaped the next
character (`build\Release\app.exe` ran `buildReleaseapp.exe`); cmd's `if not exist build mkdir build` ran a program
named `not`; `^` continued nothing.

`CMD` is a dialect of the same reader: `^` escapes and continues a line (not in double quotes, where it is text);
only `"` quotes; `REM` and `::` start a comment where a command starts; there is no `;` operator, and no `$`
substitution. Parentheses hold blocks where cmd opens one (where a command starts, after an `if`'s test, `else` or
`do`); elsewhere (`%ProgramFiles(x86)%`) they are text. The command a line runs is found past `@`, an `if`'s test,
a `for`'s loop, `else` and `call`; a label runs nothing.

GitHub's steps are read in the dialect of their shell: `cmd`, `pwsh`/`powershell`, or a POSIX shell.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
…hell's dialect

The script-file expander followed only POSIX shell scripts. A batch file CI runs (`scripts\build.bat`,
`call ci\setup.bat`, `cmd /c build.bat`) and a PowerShell script (`./ci/test.ps1`, `pwsh -File ci/test.ps1`) are now
followed the same way, their text read in cmd's or PowerShell's dialect (`Script.dialect`), their paths written with
slashes. One the project does not hold is unknown, as a missing shell script is; one elsewhere
(`C:\BuildTools\vcvars64.bat`) is not followed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
…ned heuristically as cmd

Two things the corpus showed once batch files and PowerShell scripts were followed:
- A batch file the project does not hold is most often a program (`npm.cmd`) or generated in CI (Composer's
  `vendor/bin/phpunit.bat`), not a script that went missing: it is not followed, and not reported unknown. A missing
  POSIX shell script still is.
- A CI file scanned heuristically (Azure Pipelines, AppVeyor) was read as a POSIX script on every OS, so the
  backslashes of a Windows path were taken for escapes (`.\Build\runbuild.ps1` ran `.Buildrunbuild.ps1`). A Windows
  job's lines are read as cmd's, as they are most often written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.77778% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.70%. Comparing base (8d90b79) to head (030ae5d).

Files with missing lines Patch % Lines
project_validator/tools/shell_scripts.py 92.68% 3 Missing ⚠️
project_validator/facts/shell_syntax.py 98.80% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #370      +/-   ##
==========================================
- Coverage   99.72%   99.70%   -0.02%     
==========================================
  Files         313      313              
  Lines       19984    20097     +113     
  Branches     2337     2363      +26     
==========================================
+ Hits        19929    20038     +109     
- Misses         55       59       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lefticus
lefticus merged commit 7cf7e5c into cpp-best-practices:main Nov 24, 2026
24 of 26 checks passed
@lefticus
lefticus deleted the shell-dialects branch November 24, 2026 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants