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
This commit is contained in:
Fabian Boehm 2024-09-16 21:07:53 +02:00
parent 9fafae7a8a
commit e27f4a3744
2 changed files with 10 additions and 1 deletions

View File

@ -979,7 +979,10 @@ fn throwing_main() -> i32 {
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) => {

View File

@ -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