Skip to content

Ignore a UTF-8 BOM at the start of the config file - #4103

Merged
keith-hall merged 2 commits into
sharkdp:masterfrom
jaeminbird:config-bom
Nov 2, 2026
Merged

keith-hall merged 2 commits into
sharkdp:masterfrom
jaeminbird:config-bom

Conversation

@jaeminbird

Copy link
Copy Markdown
Contributor

Config files saved by Windows PowerShell 5.1 (Set-Content -Encoding UTF8) start with a UTF-8 BOM. line.trim() keeps U+FEFF, so the first line becomes an argument like \u{feff}--style=plain, or a # comment line turns into file arguments, and every invocation fails with '\u{feff}#': The system cannot find the file specified. A leading BOM is now stripped from each config file (system and user) before parsing.

  • New integration test config_file_with_utf8_bom (BOM before an option and before a comment, with CRLF) fails before the change and passes after.
  • Reproduced and verified on Windows 11 with a PowerShell 5.1-written config.
  • 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.

Verified the failure mechanism end to end:

  • read_to_string doesn't drop the BOM bytes, so the content starts with U+FEFF.
  • U+FEFF has Unicode category Cf, not White_Space, so the line.trim() in get_args_from_str (config.rs) keeps it (I checked the whitespace semantics against the Unicode property — both Rust's trim and other languages following White_Space keep it).
  • That means a \u{feff}# comment line fails the starts_with('#') filter (it starts with U+FEFF) and gets split into bogus file arguments, and \u{feff}--style=... becomes an unknown option. Both match the reported symptom.

Stripping at each fs::read_to_string call site is the right placement: it's applied to the system config and the user config independently, which matters because the two contents are concatenated — a single strip after concatenation would miss a BOM on the user config that lands mid-string. A BOM in the middle of a file (not at offset 0) stays untouched, which is correct for strip_prefix.

One observation, not a blocker: BAT_OPTS goes through get_args_from_str directly, so a hypothetical BOM there would still break — but env vars are set programmatically, so the file-read sites are the only realistic source. If you ever wanted belt-and-braces, moving the strip into get_args_from_str would cover both paths at once.

The two-case test (BOM before an option, BOM before a comment line) plus the \r\n line endings mirrors what Set-Content -Encoding UTF8 actually produces. LGTM.

@keith-hall
keith-hall enabled auto-merge November 2, 2026 10:54
@keith-hall
keith-hall merged commit 6ca658c into sharkdp:master Nov 2, 2026
24 checks passed
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Nov 2, 2026
Resolve the CHANGELOG conflict with sharkdp#4102 and sharkdp#4103: keep their new
entries, keep this PR's sharkdp#4084 entry, and point the UTF-16BE entry back
at sharkdp#4076 (the merge of sharkdp#4102 had filled its placeholder with sharkdp#4102).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VVn4SeAXPsUajXT2rtHQfo
@jaeminbird
jaeminbird deleted the config-bom branch November 3, 2026 05:30
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