From e27f4a3744bd9c7ca38a323fc0ab1bd86d16ec2e Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Mon, 16 Sep 2024 21:07:53 +0200 Subject: [PATCH] fish_indent: Truncate file to the size of the text This can happen in case the formatted script is shorter, e.g. because we ditched superfluous quotes. Fixes #10724 --- src/bin/fish_indent.rs | 5 ++++- tests/checks/indent.fish | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/bin/fish_indent.rs b/src/bin/fish_indent.rs index d37a8803f..141e3637f 100644 --- a/src/bin/fish_indent.rs +++ b/src/bin/fish_indent.rs @@ -979,7 +979,10 @@ enum OutputType { Ok(mut file) => { // If the output is the same as the input, don't write it. if output_wtext != src { - let _ = file.write_all(&wcs2string(&output_wtext)); + let text = wcs2string(&output_wtext); + let _ = file.write_all(&text); + // Truncate the file in case it shrunk. + let _ = file.set_len(text.len().try_into().unwrap()); } } Err(err) => { diff --git a/tests/checks/indent.fish b/tests/checks/indent.fish index e05e8679a..9648042cd 100644 --- a/tests/checks/indent.fish +++ b/tests/checks/indent.fish @@ -525,3 +525,9 @@ end # CHECK: {{^}}" # CHECK: {{^}} end # CHECK: {{^}}) + +set -l tmpdir (mktemp -d) +echo 'echo "foo" "bar"' > $tmpdir/indent_test.fish +$fish_indent --write $tmpdir/indent_test.fish +cat $tmpdir/indent_test.fish +# CHECK: echo foo bar