Is it supported to run two `cargo build` in parallel in same workspace?

Suppose, I have two packages foo and bar in the same workspace.
They share some common dependencies, whch are also in the same workspace

Is running two cargo in parallel supported?
Is no file conflict guranteed?

To be specific, run the following two commands in parallel

cargo build -p foo
cargo build -p bar

Using strace, I can see cargo calls many flock syscalls on Linux,
and there are many .lock files inside CARGO_TARGET_DIR
but I am not sure if cargo supports this such parallism, not seen in Cargo documentation.

Yes, this is safe. For example, it happens all the time when people use tools which run automated checks/builds, like rust-analyzer or cargo-watch, and they also run manual commands.

However, the two processes won't work at the same time — Cargo uses a file lock so that only one Cargo process is manipulating target/ at once. You'll see the message “Blocking waiting for file lock on build directory” while one process is waiting for the other.

If your goal is to build both packages at once, then cargo build -p foo -p bar will achieve that with parallelism, so it's the fastest option.

5 Likes

Don't think this is true, after changing my cmake script.

Previously, I wrap cargo inside flock to ensure it is run in serial.

After removing the flock, the following errors occur in my CI.
All of them seems to be caused missing proper file locking

error: failed to run custom build command for `quote v1.0.29`
Caused by:
  could not execute process `XXXXXX/target/release/build/quote-fb2d7e989f9b47f6/build-script-build` (never executed)
Caused by:
  No such file or directory (os error 2)
error: failed to run custom build command for `serde v1.0.167`
error: failed to run custom build command for `serde v1.0.167`
Caused by:
  could not execute process `XXXXX/target/release/build/serde-8f20938a6fc124bb/build-script-build` (never executed)
Caused by:
  Text file busy (os error 26)
error: failed to build archive: No such file or directory

and also errors from linker

"-o" "XXXXX/target/debug/build/doc-comment-dc57ebc3b6cb89ee/build_script_build-dc57ebc3b6cb89ee" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: /usr/bin/ld: failed to set dynamic section sizes: File truncated

Not sure if generated build script is properly locked

I don't think support for two cargo build is supported.

This is NOT documented any where for cargo,
or at least this is at least not properly tested.

I don't observe rust-analyzer that itself runs multiple cargo build in parallel.

I ask this question because I am running cargo in CMake.

I think the only proper solutions are one of:

  1. Wrap cargo inside flock, to ensure only one instance is run
  2. Compile each cargo crate in separate target directory, so cargo build is safe to run in parallel
    This is what Corrosion does. But this will duplicate lots of compilation, especially when there are lots of shared build script dependencies, making compilation slow

I want to avoid the second method,
because I want to ensure cmake --build and cargo build in the terminal,
shares the same output path

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.