From 52763e0205f00b9b4f388e9f994aebe0fa0b41b5 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:04:26 -0700 Subject: [PATCH 01/10] feat: add tclsh, wish, and expect shebang detection for Tcl syntax Add first_line_match to the Tcl syntax definition via a patch file, enabling automatic Tcl highlighting for scripts with tclsh, wish, or expect shebangs. Fixes #3513 --- CHANGELOG.md | 1 + assets/patches/Tcl.sublime-syntax.patch | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 assets/patches/Tcl.sublime-syntax.patch diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fc4e54e..3682bb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Features - Preserve `--diff` change markers and snip separators when `--plain` is set. Closes #3630, see #3643 (@mvanhorn) +- Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3513 (@mvanhorn) - Added support for `hidden_file_extensions` from `.sublime-syntax` files, see #3613 (@Matei02355) - Add word wrapping mode via `--wrap=word`, see #3597 (@veeceey) - Support configuring `--terminal-width` via `BAT_WIDTH`, see #3679 (@officialasishkumar) diff --git a/assets/patches/Tcl.sublime-syntax.patch b/assets/patches/Tcl.sublime-syntax.patch new file mode 100644 index 00000000..487a1841 --- /dev/null +++ b/assets/patches/Tcl.sublime-syntax.patch @@ -0,0 +1,12 @@ +diff --git syntaxes/01_Packages/TCL/Tcl.sublime-syntax syntaxes/01_Packages/TCL/Tcl.sublime-syntax +index 1234567..abcdefg 100644 +--- syntaxes/01_Packages/TCL/Tcl.sublime-syntax ++++ syntaxes/01_Packages/TCL/Tcl.sublime-syntax +@@ -3,6 +3,7 @@ + # http://www.sublimetext.com/docs/3/syntax.html + name: Tcl + file_extensions: + - tcl ++first_line_match: ^\#!.*\b(tclsh|wish|expect)\b + scope: source.tcl + variables: From 1f89178fce7dde0f6241091fff241768317a9c46 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:06:42 -0700 Subject: [PATCH 02/10] fix: reference PR number in changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3682bb82..79b65708 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,12 @@ ## Features +<<<<<<< HEAD - Preserve `--diff` change markers and snip separators when `--plain` is set. Closes #3630, see #3643 (@mvanhorn) - Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3513 (@mvanhorn) +======= +- Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3647 (@mvanhorn) +>>>>>>> 785fff9 (fix: reference PR number in changelog entry) - Added support for `hidden_file_extensions` from `.sublime-syntax` files, see #3613 (@Matei02355) - Add word wrapping mode via `--wrap=word`, see #3597 (@veeceey) - Support configuring `--terminal-width` via `BAT_WIDTH`, see #3679 (@officialasishkumar) From e070d105b57f854d982212265e2e52d6ea6d24ec Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Thu, 26 Mar 2026 22:22:54 -0700 Subject: [PATCH 03/10] test: add shebang regression tests and move changelog to Syntaxes section Add extensionless regression test files for tclsh, wish, and expect shebangs so syntect bumps don't silently break first-line detection. Move changelog entry from Features to Syntaxes per reviewer suggestion. Co-Authored-By: Claude Opus 4.6 --- CHANGELOG.md | 6 ++-- .../regression_tests/issue_3647_expect | 2 ++ .../regression_tests/issue_3647_tclsh | 2 ++ .../examples/regression_tests/issue_3647_wish | 2 ++ tests/integration_tests.rs | 31 +++++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/examples/regression_tests/issue_3647_expect create mode 100644 tests/examples/regression_tests/issue_3647_tclsh create mode 100644 tests/examples/regression_tests/issue_3647_wish diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b65708..99222693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,9 @@ <<<<<<< HEAD - Preserve `--diff` change markers and snip separators when `--plain` is set. Closes #3630, see #3643 (@mvanhorn) -- Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3513 (@mvanhorn) -======= - Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3647 (@mvanhorn) ->>>>>>> 785fff9 (fix: reference PR number in changelog entry) +======= +>>>>>>> 29c913f (test: add shebang regression tests and move changelog to Syntaxes section) - Added support for `hidden_file_extensions` from `.sublime-syntax` files, see #3613 (@Matei02355) - Add word wrapping mode via `--wrap=word`, see #3597 (@veeceey) - Support configuring `--terminal-width` via `BAT_WIDTH`, see #3679 (@officialasishkumar) @@ -50,6 +49,7 @@ ## Syntaxes +- Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3647 (@mvanhorn) - Change the URL of Zig submodule from GitHub to Codeberg, see #3519 (@sorairolake) - Don't color strings inside CSV files, to make it easier to tell which column they belong to, see #3521 (@keith-hall) - Add syntax highlighting support for COBOL, see #3584 (@adukhan99) diff --git a/tests/examples/regression_tests/issue_3647_expect b/tests/examples/regression_tests/issue_3647_expect new file mode 100644 index 00000000..3786df69 --- /dev/null +++ b/tests/examples/regression_tests/issue_3647_expect @@ -0,0 +1,2 @@ +#!/usr/bin/expect -f +set timeout 30 diff --git a/tests/examples/regression_tests/issue_3647_tclsh b/tests/examples/regression_tests/issue_3647_tclsh new file mode 100644 index 00000000..0f4b3c5c --- /dev/null +++ b/tests/examples/regression_tests/issue_3647_tclsh @@ -0,0 +1,2 @@ +#!/usr/bin/env tclsh +puts "Hello from tclsh" diff --git a/tests/examples/regression_tests/issue_3647_wish b/tests/examples/regression_tests/issue_3647_wish new file mode 100644 index 00000000..8bfe4baf --- /dev/null +++ b/tests/examples/regression_tests/issue_3647_wish @@ -0,0 +1,2 @@ +#!/usr/bin/wish +button .b -text "Click" diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 8ae4cfdd..9f18f598 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -4201,4 +4201,35 @@ fn plain_without_diff_still_works() { .assert() .success() .stdout("line 1\nline 2 modified\nline 3\nline 4 added\n"); +#[test] +fn tcl_shebang_detection_tclsh() { + bat() + .arg("--color=always") + .arg("--style=plain") + .arg("--decorations=always") + .arg("regression_tests/issue_3647_tclsh") + .assert() + .success(); +} + +#[test] +fn tcl_shebang_detection_wish() { + bat() + .arg("--color=always") + .arg("--style=plain") + .arg("--decorations=always") + .arg("regression_tests/issue_3647_wish") + .assert() + .success(); +} + +#[test] +fn tcl_shebang_detection_expect() { + bat() + .arg("--color=always") + .arg("--style=plain") + .arg("--decorations=always") + .arg("regression_tests/issue_3647_expect") + .assert() + .success(); } From cafad6b03668abc1ab41e1e10e72fd449ac609e4 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Fri, 27 Mar 2026 12:18:30 -0700 Subject: [PATCH 04/10] test: add highlighted outputs for Tcl shebang regression tests Generate highlighted test outputs for tclsh, wish, and expect shebang detection files to prevent regressions. --- CHANGELOG.md | 4 ---- tests/integration_tests.rs | 2 ++ tests/syntax-tests/highlighted/Tcl/expect_shebang | 7 +++++++ tests/syntax-tests/highlighted/Tcl/tclsh_shebang | 7 +++++++ tests/syntax-tests/highlighted/Tcl/wish_shebang | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 tests/syntax-tests/highlighted/Tcl/expect_shebang create mode 100644 tests/syntax-tests/highlighted/Tcl/tclsh_shebang create mode 100644 tests/syntax-tests/highlighted/Tcl/wish_shebang diff --git a/CHANGELOG.md b/CHANGELOG.md index 99222693..8049cba3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,7 @@ ## Features -<<<<<<< HEAD - Preserve `--diff` change markers and snip separators when `--plain` is set. Closes #3630, see #3643 (@mvanhorn) -- Add shebang-based detection for Tcl (`tclsh`, `wish`) and Expect (`expect`) scripts, see #3647 (@mvanhorn) -======= ->>>>>>> 29c913f (test: add shebang regression tests and move changelog to Syntaxes section) - Added support for `hidden_file_extensions` from `.sublime-syntax` files, see #3613 (@Matei02355) - Add word wrapping mode via `--wrap=word`, see #3597 (@veeceey) - Support configuring `--terminal-width` via `BAT_WIDTH`, see #3679 (@officialasishkumar) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 9f18f598..432faed8 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -4201,6 +4201,8 @@ fn plain_without_diff_still_works() { .assert() .success() .stdout("line 1\nline 2 modified\nline 3\nline 4 added\n"); +} + #[test] fn tcl_shebang_detection_tclsh() { bat() diff --git a/tests/syntax-tests/highlighted/Tcl/expect_shebang b/tests/syntax-tests/highlighted/Tcl/expect_shebang new file mode 100644 index 00000000..22f2101e --- /dev/null +++ b/tests/syntax-tests/highlighted/Tcl/expect_shebang @@ -0,0 +1,7 @@ +#!/usr/bin/expect -f +# Expect script detected via expect shebang +set timeout 30 +spawn ssh user@host +expect "password:" +send "secret\r" +expect eof diff --git a/tests/syntax-tests/highlighted/Tcl/tclsh_shebang b/tests/syntax-tests/highlighted/Tcl/tclsh_shebang new file mode 100644 index 00000000..de50978f --- /dev/null +++ b/tests/syntax-tests/highlighted/Tcl/tclsh_shebang @@ -0,0 +1,7 @@ +#!/usr/bin/env tclsh +# Tcl script detected via tclsh shebang +puts "Hello from tclsh" +set x 42 +if {$x > 0} { + puts "positive" +} diff --git a/tests/syntax-tests/highlighted/Tcl/wish_shebang b/tests/syntax-tests/highlighted/Tcl/wish_shebang new file mode 100644 index 00000000..134b185d --- /dev/null +++ b/tests/syntax-tests/highlighted/Tcl/wish_shebang @@ -0,0 +1,5 @@ +#!/usr/bin/wish +# Tk script detected via wish shebang +package require Tk +button .b -text "Click" -command {puts "clicked"} +pack .b From 77ea750e662d6dd21196acec1cae32517fe57dac Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sat, 18 Apr 2026 06:58:45 -0700 Subject: [PATCH 05/10] test(Tcl): add shebang source files for regression test CI reported FileNotFoundError for Tcl/{tclsh,expect,wish}_shebang in tests/syntax-tests/source/Tcl/. The highlighted files existed but the source files had been omitted, so create_highlighted_versions.py had nothing to read from. Source files match the shebang regression test inputs at tests/examples/regression_tests/issue_3647_*. --- tests/syntax-tests/source/Tcl/expect_shebang | 7 +++++++ tests/syntax-tests/source/Tcl/tclsh_shebang | 7 +++++++ tests/syntax-tests/source/Tcl/wish_shebang | 5 +++++ 3 files changed, 19 insertions(+) create mode 100644 tests/syntax-tests/source/Tcl/expect_shebang create mode 100644 tests/syntax-tests/source/Tcl/tclsh_shebang create mode 100644 tests/syntax-tests/source/Tcl/wish_shebang diff --git a/tests/syntax-tests/source/Tcl/expect_shebang b/tests/syntax-tests/source/Tcl/expect_shebang new file mode 100644 index 00000000..ef288ba8 --- /dev/null +++ b/tests/syntax-tests/source/Tcl/expect_shebang @@ -0,0 +1,7 @@ +#!/usr/bin/expect -f +# Expect script detected via expect shebang +set timeout 30 +spawn ssh user@host +expect "password:" +send "secret\r" +expect eof diff --git a/tests/syntax-tests/source/Tcl/tclsh_shebang b/tests/syntax-tests/source/Tcl/tclsh_shebang new file mode 100644 index 00000000..08faa4d6 --- /dev/null +++ b/tests/syntax-tests/source/Tcl/tclsh_shebang @@ -0,0 +1,7 @@ +#!/usr/bin/env tclsh +# Tcl script detected via tclsh shebang +puts "Hello from tclsh" +set x 42 +if {$x > 0} { + puts "positive" +} diff --git a/tests/syntax-tests/source/Tcl/wish_shebang b/tests/syntax-tests/source/Tcl/wish_shebang new file mode 100644 index 00000000..690e73ef --- /dev/null +++ b/tests/syntax-tests/source/Tcl/wish_shebang @@ -0,0 +1,5 @@ +#!/usr/bin/wish +# Tk script detected via wish shebang +package require Tk +button .b -text "Click" -command {puts "clicked"} +pack .b From 0ecdeb28d43c7a259c59cc35dd5e1f0c2a266d3e Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Sat, 18 Apr 2026 09:42:26 -0700 Subject: [PATCH 06/10] test(Tcl): regenerate shebang golden files to match patched syntax The Tcl.sublime-syntax.patch adds first_line_match so bat recognizes tclsh/wish/expect shebang files as Tcl. With the patch applied, the leading '#!/...' and '# comment' lines get tokenized as Tcl comments and rendered in Monokai-Extended's comment color instead of default foreground. The goldens were generated against unpatched bat in the original commit, so CI's 'Run tests with updated syntaxes and themes' job (which runs assets/create.sh before the regression test) disagreed. Regenerate against the patched build so the regression test passes. --- tests/syntax-tests/highlighted/Tcl/expect_shebang | 14 +++++++------- tests/syntax-tests/highlighted/Tcl/tclsh_shebang | 12 ++++++------ tests/syntax-tests/highlighted/Tcl/wish_shebang | 10 +++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/syntax-tests/highlighted/Tcl/expect_shebang b/tests/syntax-tests/highlighted/Tcl/expect_shebang index 22f2101e..871eed6c 100644 --- a/tests/syntax-tests/highlighted/Tcl/expect_shebang +++ b/tests/syntax-tests/highlighted/Tcl/expect_shebang @@ -1,7 +1,7 @@ -#!/usr/bin/expect -f -# Expect script detected via expect shebang -set timeout 30 -spawn ssh user@host -expect "password:" -send "secret\r" -expect eof +#!/usr/bin/expect -f +# Expect script detected via expect shebang +set timeout 30 +spawn ssh user@host +expect "password:" +send "secret\r" +expect eof diff --git a/tests/syntax-tests/highlighted/Tcl/tclsh_shebang b/tests/syntax-tests/highlighted/Tcl/tclsh_shebang index de50978f..5461f5f8 100644 --- a/tests/syntax-tests/highlighted/Tcl/tclsh_shebang +++ b/tests/syntax-tests/highlighted/Tcl/tclsh_shebang @@ -1,7 +1,7 @@ -#!/usr/bin/env tclsh -# Tcl script detected via tclsh shebang -puts "Hello from tclsh" -set x 42 -if {$x > 0} { - puts "positive" +#!/usr/bin/env tclsh +# Tcl script detected via tclsh shebang +puts "Hello from tclsh" +set x 42 +if {$x > 0} { + puts "positive" } diff --git a/tests/syntax-tests/highlighted/Tcl/wish_shebang b/tests/syntax-tests/highlighted/Tcl/wish_shebang index 134b185d..94fb971f 100644 --- a/tests/syntax-tests/highlighted/Tcl/wish_shebang +++ b/tests/syntax-tests/highlighted/Tcl/wish_shebang @@ -1,5 +1,5 @@ -#!/usr/bin/wish -# Tk script detected via wish shebang -package require Tk -button .b -text "Click" -command {puts "clicked"} -pack .b +#!/usr/bin/wish +# Tk script detected via wish shebang +package require Tk +button .b -text "Click" -command {puts "clicked"} +pack .b From ea7fafca1e6191dbfc4c55f039930e350b4412b2 Mon Sep 17 00:00:00 2001 From: Barry <100205797+barry3406@users.noreply.github.com> Date: Sun, 19 Apr 2026 14:19:00 -0700 Subject: [PATCH 07/10] windows: statically link the CRT to remove vcruntime dependency Add a .cargo/config.toml that sets target-feature=+crt-static for the i686, x86_64, and aarch64 MSVC targets so bat.exe no longer depends on the vcruntime DLL. Mirrors the fix applied to fd in sharkdp/fd#1891. Closes #3634 --- .cargo/config.toml | 11 +++++++++++ CHANGELOG.md | 1 + 2 files changed, 12 insertions(+) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..67a37e28 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,11 @@ +# On Windows MSVC, statically link the C runtime so that the resulting EXE does +# not depend on the vcruntime DLL. +# +# See: https://github.com/sharkdp/bat/issues/3634 + +[target.x86_64-pc-windows-msvc] +rustflags = ["-C", "target-feature=+crt-static"] +[target.i686-pc-windows-msvc] +rustflags = ["-C", "target-feature=+crt-static"] +[target.aarch64-pc-windows-msvc] +rustflags = ["-C", "target-feature=+crt-static"] diff --git a/CHANGELOG.md b/CHANGELOG.md index 8049cba3..5a37d298 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ - Bump MSRV to 1.88, update `time` crate to 0.3.47 to fix RUSTSEC-2026-0009, see #3581 (@NORMAL-EX) - Allow home and end keys to be used with builtin pager, see #3651 (@keith-hall) - Builtin syntax mapping: cleanup matcher glob parsing logic #3652 (@cyqsimon) +- Statically link the CRT for MSVC builds via Cargo config to avoid runtime DLL dependencies. Closes #3634, see #3692 (@barry3406) ## Syntaxes From f39d63b85d5b4bc675c718ef7832d786ed38a9a1 Mon Sep 17 00:00:00 2001 From: YoshKoz <77861115+YoshKoz@users.noreply.github.com> Date: Sun, 19 Apr 2026 23:19:53 +0200 Subject: [PATCH 08/10] fix(zsh): use newline-splitting for language completions to fix word-splitting on names with spaces Language names like "HTML (Jinja2)" and "Apache Conf" contain spaces, which caused $() command substitution to word-split them into garbage tokens. Use ${(f)"$(...)"} (split on newlines only) to match the pattern already used for theme completions on line 100. Fixes #2897 --- assets/completions/bat.zsh.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/completions/bat.zsh.in b/assets/completions/bat.zsh.in index 3c3f81d1..f8de56e4 100644 --- a/assets/completions/bat.zsh.in +++ b/assets/completions/bat.zsh.in @@ -90,7 +90,7 @@ _{{PROJECT_EXECUTABLE}}_main() { languages) local IFS=$'\n' local -a languages - languages=( $({{PROJECT_EXECUTABLE}} --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }') ) + languages=( ${(f)"$({{PROJECT_EXECUTABLE}} --list-languages | awk -F':|,' '{ for (i = 1; i <= NF; ++i) printf("%s:%s\n", $i, $1) }')"} ) _describe 'language' languages && ret=0 ;; From 5722311b2e96f6c6751e848a99c265e386ab43f3 Mon Sep 17 00:00:00 2001 From: Yoshi Tacke <77861115+YoshKoz@users.noreply.github.com> Date: Mon, 20 Apr 2026 10:06:00 +0200 Subject: [PATCH 09/10] docs(changelog): add bugfix entry for zsh completion fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a37d298..8eabb7d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - Fixed test compatibility with future Cargo build directory changes, see #3550 (@nmacl) - Fixed bug caused by using `--plain` and `--terminal-width=N` flags simultaneously, see #3529 (@H4k1l) - Fixed syntax tests path, see #3610 (@foxfromworld) +- Fix zsh tab completion word-splitting language names containing spaces (e.g. `HTML (Jinja2)`, `Apache Conf`), see #3693 (@YoshKoz) ## Other - Use git version of cross. See #3533 (@OctopusET) From 64567c4819ff76cec66a74e87c9e47b1f6f9b609 Mon Sep 17 00:00:00 2001 From: lawrence3699 Date: Tue, 28 Apr 2026 01:11:06 +1000 Subject: [PATCH 10/10] Propagate initial input read errors --- CHANGELOG.md | 1 + src/input.rs | 44 +++++++++++++++++++++++++++++++++++--------- src/lessopen.rs | 4 ++-- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eabb7d4..066b1161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - Syntax highlighting for Python files using uv as script runner in shebang #3689 (@janlarres) ## Bugfixes +- Report initial input read errors instead of treating them as empty input. Closes #3002, see #3706 (@lawrence3699) - Treat ZIP archives as binary content based on their magic header, see #3686 (@officialasishkumar) - Fix i686 `.deb` package using incorrect architecture name (`i686` instead of `i386`), preventing installation on Debian. Closes #3611, see #3650 (@Sim-hu) - Fix inconsistent `.deb` MUSL package names (aarch64-musl used `arm64` instead of `musl-linux-arm64`, and `musleabihf` target missed `bat-musl` prefix). Closes #3482, see #3642 (@mvanhorn) diff --git a/src/input.rs b/src/input.rs index 30f13f98..a3420123 100644 --- a/src/input.rs +++ b/src/input.rs @@ -207,7 +207,7 @@ impl<'a> Input<'a> { kind: OpenedInputKind::StdIn, description, metadata: self.metadata, - reader: InputReader::new(stdin), + reader: InputReader::try_new(stdin)?, }) } @@ -236,14 +236,14 @@ impl<'a> Input<'a> { file = input_identifier.into_inner().expect("The file was lost in the clircle::Identifier, this should not have happened..."); } - InputReader::new(BufReader::new(file)) + InputReader::try_new(BufReader::new(file))? }, }), InputKind::CustomReader(reader) => Ok(OpenedInput { description, kind: OpenedInputKind::CustomReader, metadata: self.metadata, - reader: InputReader::new(BufReader::new(reader)), + reader: InputReader::try_new(BufReader::new(reader))?, }), } } @@ -257,24 +257,29 @@ pub(crate) struct InputReader<'a> { } impl<'a> InputReader<'a> { - pub(crate) fn new(mut reader: R) -> InputReader<'a> { + #[cfg(test)] + pub(crate) fn new(reader: R) -> InputReader<'a> { + Self::try_new(reader).expect("reading the first line failed") + } + + pub(crate) fn try_new(mut reader: R) -> io::Result> { let mut first_line = vec![]; - reader.read_until(b'\n', &mut first_line).ok(); + reader.read_until(b'\n', &mut first_line)?; let content_type = inspect_content_type(&first_line); if content_type == Some(ContentType::UTF_16LE) { - read_utf16_line(&mut reader, &mut first_line, 0x00, 0x0A).ok(); + read_utf16_line(&mut reader, &mut first_line, 0x00, 0x0A)?; } else if content_type == Some(ContentType::UTF_16BE) { - read_utf16_line(&mut reader, &mut first_line, 0x0A, 0x00).ok(); + read_utf16_line(&mut reader, &mut first_line, 0x0A, 0x00)?; } - InputReader { + Ok(InputReader { inner: Box::new(reader), first_line, content_type, unbuffered: false, - } + }) } pub(crate) fn read_line(&mut self, buf: &mut Vec) -> io::Result { @@ -405,6 +410,27 @@ fn non_zip_pk_prefix_is_not_treated_as_binary() { ); } +#[test] +fn input_open_returns_initial_read_errors() { + struct FailingRead; + + impl Read for FailingRead { + fn read(&mut self, _buf: &mut [u8]) -> io::Result { + Err(io::Error::other("initial read failed")) + } + } + + let input = Input::from_reader(Box::new(FailingRead)); + let result = input.open(io::empty(), None); + + assert!(result.is_err()); + assert!(result + .err() + .unwrap() + .to_string() + .contains("initial read failed")); +} + #[test] fn utf16le() { let content = b"\xFF\xFE\x73\x00\x0A\x00\x64\x00"; diff --git a/src/lessopen.rs b/src/lessopen.rs index 966bc2c0..116206ba 100644 --- a/src/lessopen.rs +++ b/src/lessopen.rs @@ -155,7 +155,7 @@ impl LessOpenPreprocessor { Ok(OpenedInput { kind, - reader: InputReader::new(BufReader::new( + reader: InputReader::try_new(BufReader::new( if matches!(self.kind, LessOpenKind::TempFile) { let lessopen_string = match String::from_utf8(lessopen_stdout) { Ok(string) => string, @@ -192,7 +192,7 @@ impl LessOpenPreprocessor { .map(|s| s.replacen("%s", &path_str, 1).replacen("%s", "-", 1)), } }, - )), + ))?, metadata: input.metadata, description: input.description, })