Description
Problem
I am building multiple binaries from the same repo for docker compose
services with docker RUN cache mounting. I depend on a crate as a git repository. When these services are built simultaneously, they share the same /usr/local/cargo
directory. The builds fail for all but the last service to finish downloading the dependency crate repo with the error "error: failed to get <dep crate>
as a dependency package of <my crate>
- I can share the full error if it's helpful.
I have also reproduced this issue outside of docker by manually starting 2 builds in close succession, and I see the same behavior.
Steps
Manual repro
- Setup a project that depends on a crate via
git
- the longer the dependency takes to download, the easier this will be to repro. - Clean all cargo files - I am using
sudo rm -rf /usr/local/cargo/git/ /usr/local/cargo/registry/ /usr/local/cargo/.crates* && cargo clean
- Run
cargo build
twice simultaneously - the intent here is to get the git clones to overlap
Expected outcome: One cargo build
does the clone, the other waits for it to finish and then uses the result, as with other dependency types.
Actual outcome: Both cargo build
s try to do the git clone at the same time, the first one ends up getting stomped on by the second one and so the first cargo build
fails while the second one succeeds.
Possible Solution(s)
In my docker case, I am able to switch the sharing type to private
to spend some disk space to keep parallel builds. That looks something like this:
RUN --mount=type=cache,target=/usr/local/cargo/git/,sharing=private \
--mount=type=cache,target=/usr/local/cargo/registry/,sharing=private \
--mount=type=cache,target=/app/repo/target/ \
cargo build
Notes
No response
Version
$ cargo version --verbose
cargo 1.85.0 (d73d2caf9 2024-12-31)
release: 1.85.0
commit-hash: d73d2caf9e41a39daf2a8d6ce60ec80bf354d2a7
commit-date: 2024-12-31
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w 11 Sep 2023
os: Ubuntu 24.4.0 (noble) [64-bit]