Skip to content

Keep backslashes in the pager command on Windows - #4104

Merged
keith-hall merged 3 commits into
sharkdp:masterfrom
jaeminbird:win-pager-path
Nov 3, 2026
Merged

keith-hall merged 3 commits into
sharkdp:masterfrom
jaeminbird:win-pager-path

Conversation

@jaeminbird

Copy link
Copy Markdown
Contributor

The pager command (BAT_PAGER, PAGER, --pager) is split with shell_words, which treats \ as an escape, so on Windows BAT_PAGER=C:\tools\less.exe becomes C:toolsless.exe and bat warns "Pager 'C:toolsless.exe' not found, outputting to stdout instead". The README suggests pointing an environment variable at a downloaded less.exe, which hits this directly. On Windows the command is now split on whitespace with single and double quotes, keeping \ literally; other platforms still use shell_words.

  • New integration test pager_windows_absolute_path (absolute path to the mocked echo.bat) fails before the change and passes after.
  • Unit tests for the splitter (unquoted and quoted paths, paths with spaces, quoted args, unclosed quote).
  • Reproduced on Windows 11 with BAT_PAGER=C:\Windows\System32\more.com.
  • cargo test, clippy and fmt pass.

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The premise is real and I reproduced it: POSIX-style splitters treat \ as an escape, so shlex.split("C:\\tools\\less.exe") yields C:toolsless.exe — the same escape semantics shell_words applies, which is exactly the "Pager 'C:toolsless.exe' not found" failure you describe. A backslash-preserving splitter for Windows is the right fix, and keeping shell_words on other platforms avoids changing behavior for users who legitimately rely on POSIX quoting there.

I also replicated split_command_windows line by line and checked all seven cases in your unit-test table plus the error path — every one behaves as asserted, including the trickier ones (quote opening mid-word for --prompt="a b", the empty "" argument, and the unclosed-quote ParseError). The ParseError return type is already imported at the top of pager.rs, so that's clean.

Design details worth calling out positively:

  • Using if cfg!(windows) rather than #[cfg] means the windows splitter's unit tests still compile and run on the Linux CI, where they actually get exercised — good choice.
  • Quotes doing pure grouping (no escape processing inside) is the correct simplification here; pager commands don't need $(...)-style semantics on Windows.

One nit on the integration test: pager_windows_absolute_path builds BAT_PAGER as format!("{} pager-output", pager.display()) without quoting the path. On a checkout located under a path containing spaces (local dev machines hit this, e.g. C:\Users\Some Name\...), the split yields C:\Users\Some as the binary and the test fails even though the code is correct. Wrapping it as format!("\"{}\" pager-output", pager.display()) would make the test space-proof and also exercise the quoted-path form end to end.

@keith-hall

Copy link
Copy Markdown
Collaborator

Perhaps worth fixing the nit mentioned in #4104 (review), and before I merge, I just want to sanity check something - what happens when using ie. Mintty or some other unix terminal emulator on Windows? Is it possible that then posix path separators should and escape chars should be honored but no longer would be with this PR?

A checkout under a path with spaces split the unquoted path at the
first space. Quoting it also covers the quoted-path form end to end.
@jaeminbird

Copy link
Copy Markdown
Contributor Author

Merged master (5754e83) and quoted the path in pager_windows_absolute_path (599bae4), thanks @rahul05ranjan. cargo test passes on Windows.

On Mintty: bat starts the pager with Command::new, not through a shell, so the terminal emulator doesn't change how BAT_PAGER is split. I compared master and this branch from Git Bash, the shell Mintty runs:

  • less -FX, forward-slash paths (C:/tools/less.exe), and quoted arguments ("a b", 'c d'): same on both.
  • C:\tools\less.exe: fails on master, works here. The doubled C:\tools\less.exe workaround works on both.
  • /usr/bin/less: MSYS rewrites it to C:/Program Files/Git/usr/bin/less before bat sees it, so it splits at the space on both.
  • What changes is backslash as an escape outside quotes: a\ b gives a b on master and a\, b here; a\"b gives a"b on master and a parse error here. Quoting ("a b") works on both.

@rahul05ranjan

Copy link
Copy Markdown

Thanks @jaeminbird — quoting the path in pager_windows_absolute_path addresses exactly the nit I had. And good call verifying Mintty: since Command::new bypasses the shell entirely, the terminal emulator shouldn't change BAT_PAGER tokenization.

@keith-hall
keith-hall merged commit 5f971d9 into sharkdp:master Nov 3, 2026
24 checks passed
@jaeminbird
jaeminbird deleted the win-pager-path branch November 3, 2026 21:39
keith-hall pushed a commit that referenced this pull request Nov 29, 2026
The config file and BAT_OPTS were split with shell_words, which treats
a backslash as an escape, so --pager=C:\tools\less.exe became
C:toolsless.exe. Split them with the same function as the pager command,
which keeps backslashes on Windows since #4104.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

3 participants