Skip to content

Commit e616ed0

Browse files
committed
Move cargo vendor output to prevent conflicts and acquire file lock beforehand
This change modifies where we save the cargo configuration from cargo vendor to avoid interfering with manual cargo operations in subdirectories. In addition, we now acquire a file lock before running cargo vendor to prevent parallel builds from running cargo vendor at the same time. Key Changes: * Generate cargo vendor config to `.cargo/twoliter_cargo_config.toml` instead of `.cargo/config.toml` * Update build.Dockerfile to mount `.cargo/twoliter_cargo_config.toml` to `.cargo/config.toml` internally * Update all docker run commands in Makefile.toml to do the same as above * Acquire a file lock on `.cargo/vendor.lock` before running cargo vendor to prevent parallel runs of cargo vendor These changes resolve two issues: * Prevents `cargo vendor` runs from occuring at the same time causing mutations to the vendored sources during parallel builds. * Prevents manual cargo commands in sub-workspaces from entering a dependency resolution loop because of the vendored sources
1 parent 23e2dcf commit e616ed0

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

twoliter/embedded/Makefile.toml

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,31 @@ for ws in sources variants .; do
362362
[ -s "${BUILDSYS_ROOT_DIR}/${ws}/Cargo.lock" ] || continue
363363
CARGO_MANIFESTS+=(--sync "${BUILDSYS_ROOT_DIR}/${ws}/Cargo.toml")
364364
done
365-
cargo vendor --locked --no-delete --versioned-dirs --respect-source-config ${CARGO_MANIFESTS[@]} .cargo/vendor > .cargo/config.toml 2> .cargo/vendor.log || {
366-
echo "vendoring of cargo dependencies failed"
367-
cat .cargo/vendor.log
365+
exec 9<>.cargo/vendor.lock
366+
if ! flock -w 90 9; then
367+
echo "failed to obtain lock" >&2
368+
exit 1
369+
fi
370+
371+
set +e
372+
cargo vendor \
373+
--locked \
374+
--no-delete \
375+
--versioned-dirs \
376+
--respect-source-config \
377+
${CARGO_MANIFESTS[@]} \
378+
.cargo/vendor \
379+
> .cargo/twoliter_cargo_config.toml \
380+
2> .cargo/vendor.log
381+
382+
if [ "${?}" -ne 0 ]; then
383+
echo "vendoring of cargo dependencies failed" >&2
384+
cat .cargo/vendor.log >&2
368385
exit 1
369-
}
370-
rm .cargo/vendor.log
386+
fi
371387
388+
set -e
389+
rm .cargo/vendor.log
372390
chmod -R o+r ${CARGO_HOME}
373391
'''
374392
]
@@ -459,7 +477,8 @@ done
459477
if ! docker run --rm \
460478
-u $(id -u):$(id -g) \
461479
-e CARGO_HOME="/tmp/.cargo" \
462-
-v "${CARGO_HOME}":/tmp/.cargo \
480+
-v "${CARGO_HOME}/twoliter_cargo_config.toml":/tmp/.cargo/config.toml \
481+
-v "${CARGO_HOME}/vendor":/tmp/.cargo/vendor \
463482
-v "${BUILDSYS_ROOT_DIR}/sources":/tmp/sources \
464483
"${TLPRIVATE_SDK_IMAGE}" \
465484
cargo fmt \
@@ -495,7 +514,8 @@ export VARIANT="${BUILDSYS_VARIANT}"
495514
if ! docker run --rm \
496515
-u $(id -u):$(id -g) \
497516
-e CARGO_HOME="/tmp/.cargo" \
498-
-v "${CARGO_HOME}":/tmp/.cargo \
517+
-v "${CARGO_HOME}/twoliter_cargo_config.toml":/tmp/.cargo/config.toml \
518+
-v "${CARGO_HOME}/vendor":/tmp/.cargo/vendor \
499519
-v "${BUILDSYS_ROOT_DIR}/sources":/tmp/sources \
500520
-e VARIANT \
501521
"${TLPRIVATE_SDK_IMAGE}" \
@@ -978,7 +998,8 @@ docker run --rm \
978998
--user "$(id -u):$(id -g)" \
979999
--security-opt="label=disable" \
9801000
-e CARGO_HOME="/tmp/.cargo" \
981-
-v "${CARGO_HOME}":/tmp/.cargo \
1001+
-v "${CARGO_HOME}/twoliter_cargo_config.toml":/tmp/.cargo/config.toml \
1002+
-v "${CARGO_HOME}/vendor":/tmp/.cargo/vendor \
9821003
-v "${BUILDSYS_ROOT_DIR}/sources":/tmp/sources \
9831004
"${TLPRIVATE_SDK_IMAGE}" \
9841005
bash -c "${run_cargo_deny}"

twoliter/embedded/build.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ USER builder
112112
# We only mount the config.toml and the vendor directory instead of the whole .cargo directory
113113
# because the .git and .registry directories are specific to the version of cargo and can cause
114114
# incapatibility between the host cargo and the cargo in the sdk
115-
RUN --mount=source=.cargo/config.toml,target=/home/builder/rpmbuild/.cargo/config.toml \
115+
RUN --mount=source=.cargo/twoliter_cargo_config.toml,target=/home/builder/rpmbuild/.cargo/config.toml \
116116
--mount=source=.cargo/vendor,target=/home/builder/rpmbuild/.cargo/vendor \
117117
--mount=type=cache,target=/home/builder/.cache,from=cache,source=/cache \
118118
--mount=source=sources,target=/home/builder/rpmbuild/BUILD/sources \

0 commit comments

Comments
 (0)