When generating many build files with cargo-bazel in crates_vendor() rule. We can constantly see errors when running crates_vendor as part of pre-commit hook:
buildifier: open /.../third_party/rust/crates/BUILD.percent-encoding-1.0.1.bazel: no such file or directory
Error: Failed to run buildifier on /.../third_party/rust/crates/BUILD.percent-encoding-1.0.1.bazel
However, when run crates_vendor directly won't trigger this.
After looking into the source code, and relevant rust issues, fs::write should be the culprit.
As pointed out in https://github.com/rust-lang/rust/issues/51775, fs::write doesn't force a sync in the end, so the newly created file may still sit in the os cache, which cannot be accessed by another process.
When generating many build files with
cargo-bazel
incrates_vendor()
rule. We can constantly see errors when runningcrates_vendor
as part of pre-commit hook:However, when run
crates_vendor
directly won't trigger this.After looking into the source code, and relevant rust issues,
fs::write
should be the culprit.https://github.com/bazelbuild/rules_rust/blob/4e593b6cc3e1dab70eed5fdec239ffc0b4179b91/crate_universe/src/rendering.rs#L820C13-L820C22
As pointed out in https://github.com/rust-lang/rust/issues/51775,
fs::write
doesn't force a sync in the end, so the newly created file may still sit in the os cache, which cannot be accessed by another process.edit:
We are also seeing random errors from
https://github.com/bazelbuild/rules_rust/blob/4e593b6cc3e1dab70eed5fdec239ffc0b4179b91/crate_universe/src/cli/vendor.rs#L184
Directory not empty (os error 66)
.