Skip to content

Commit b70a008

Browse files
committed
Ensure error reporting when using buffered file writes
Quoting from the BufWriter docs: >It is critical to call flush before BufWriter<W> is dropped. Though >dropping will attempt to flush the contents of the buffer, any errors >that happen in the process of dropping will be ignored. Calling flush >ensures that the buffer is empty and thus dropping will not even >attempt file operations.
1 parent f28a158 commit b70a008

File tree

13 files changed

+37
-10
lines changed

13 files changed

+37
-10
lines changed

api/cpp/cbindgen.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ namespace slint::platform::key_codes {{
9999
i_slint_common::for_each_special_keys!(print_key_codes);
100100
writeln!(enums_pub, "}}")?;
101101

102+
enums_priv.flush()?;
103+
enums_pub.flush()?;
102104
Ok(())
103105
}
104106

@@ -184,6 +186,8 @@ fn builtin_structs(path: &Path) -> anyhow::Result<()> {
184186
i_slint_common::for_each_builtin_structs!(print_structs);
185187
writeln!(structs_priv, "}}")?;
186188
writeln!(structs_pub, "}}")?;
189+
structs_priv.flush()?;
190+
structs_pub.flush()?;
187191
Ok(())
188192
}
189193

api/rs/build/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,8 @@ pub fn compile_with_output_path(
522522
}
523523
}
524524

525+
code_formatter.sink.flush().map_err(CompileError::SaveError)?;
526+
525527
Ok(dependencies)
526528
}
527529

internal/compiler/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fn widget_library() -> &'static [(&'static str, &'static BuiltinDirectory<'stati
3838
}
3939

4040
writeln!(file, "]\n}}")?;
41+
file.flush()?;
4142

4243
println!("cargo:rustc-env=SLINT_WIDGETS_LIBRARY={}", output_file_path.display());
4344

internal/compiler/generator/cpp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,9 @@ pub fn generate(
873873

874874
for (cpp_file_name, cpp_file) in config.cpp_files.iter().zip(cpp_files) {
875875
use std::io::Write;
876-
write!(&mut BufWriter::new(std::fs::File::create(&cpp_file_name)?), "{cpp_file}")?;
876+
let mut cpp_writer = BufWriter::new(std::fs::File::create(&cpp_file_name)?);
877+
write!(&mut cpp_writer, "{cpp_file}")?;
878+
cpp_writer.flush()?;
877879
}
878880

879881
Ok(file)

internal/compiler/generator/cpp_live_reload.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ pub fn generate(
5252
let cpp_files = file.split_off_cpp_files(config.header_include, config.cpp_files.len());
5353
for (cpp_file_name, cpp_file) in config.cpp_files.iter().zip(cpp_files) {
5454
use std::io::Write;
55-
write!(&mut BufWriter::new(std::fs::File::create(&cpp_file_name)?), "{cpp_file}")?;
55+
let mut cpp_writer = BufWriter::new(std::fs::File::create(&cpp_file_name)?);
56+
write!(&mut cpp_writer, "{cpp_file}")?;
57+
cpp_writer.flush()?;
5658
}
5759

5860
Ok(file)

tests/doctests/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
8686
println!("cargo:rerun-if-changed={}", path.display());
8787
}
8888

89+
tests_file.flush()?;
90+
8991
println!("cargo:rustc-env=TEST_FUNCTIONS={}", tests_file_path.to_string_lossy());
9092
println!("cargo:rustc-env=SLINT_ENABLE_EXPERIMENTAL_FEATURES=1");
9193

tests/driver/cpp/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6969
)?;
7070
}
7171

72+
tests_file.flush()?;
73+
7274
println!("cargo:rustc-env=TEST_FUNCTIONS={}", tests_file_path.to_string_lossy());
7375
println!("cargo:rustc-env=SLINT_ENABLE_EXPERIMENTAL_FEATURES=1");
7476
Ok(())

tests/driver/rust/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ fn main() -> std::io::Result<()> {
6666
x.source.replace('\n', "\n ")
6767
)?;
6868
}
69+
70+
output.flush()?;
6971
}
7072

73+
generated_file.flush()?;
74+
7175
// By default resources are embedded. The WASM example builds provide test coverage for that. This switch
7276
// provides test coverage for the non-embedding case, compiling tests without embedding the images.
7377
if !live_reload {

tests/screenshots/build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,12 @@ fn main() -> std::io::Result<()> {
156156
Ok(())
157157
}}",
158158
)?;
159+
160+
output.flush()?;
159161
}
160162

163+
generated_file.flush()?;
164+
161165
//Make sure to use a consistent style
162166
println!("cargo:rustc-env=SLINT_STYLE=fluent");
163167
println!("cargo:rustc-env=SLINT_ENABLE_EXPERIMENTAL_FEATURES=1");

tools/compiler/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,9 @@ fn main() -> std::io::Result<()> {
198198
if args.output == std::path::Path::new("-") {
199199
generator::generate(format, &mut std::io::stdout(), &doc, &loader.compiler_config)?;
200200
} else {
201-
generator::generate(
202-
format,
203-
&mut BufWriter::new(std::fs::File::create(&args.output)?),
204-
&doc,
205-
&loader.compiler_config,
206-
)?;
201+
let mut file_writer = BufWriter::new(std::fs::File::create(&args.output)?);
202+
generator::generate(format, &mut file_writer, &doc, &loader.compiler_config)?;
203+
file_writer.flush()?;
207204
}
208205

209206
if let Some(depfile) = args.depfile {
@@ -223,6 +220,7 @@ fn main() -> std::io::Result<()> {
223220
}
224221

225222
writeln!(f)?;
223+
f.flush()?;
226224
}
227225
diag.print_warnings_and_exit_on_error();
228226
Ok(())

0 commit comments

Comments
 (0)