bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
671 stars 434 forks source link

cargo-bazel: buildifier may fail for no such file or directory #2972

Open ashi009 opened 3 weeks ago

ashi009 commented 3 weeks ago

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.

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).

illicitonion commented 3 weeks ago

Good spot! Feel free to send a PR calling sync after the write!