Skip to content

Commit d9c437e

Browse files
committed
rust: Add option not to cache path = cargo dependencies
Due to Earthly's layer cache code added with `COPY` (even with `--keep-ts`) can end up with timestamps (`mtime`) corresponding to the point of creation of the cache entry, not the current time. However on a following build the `target` mount cache may contain builds from other branches, with different code for those dependencies, which have a newer `mtime`. In this case `cargo` will think it can use the cached dependency instead of rebuilding (because the code appears older than the cached entry under `target`). This only affects `path` dependencies since other dependencies are versioned (either from crates.io or from git dependencies) so cargo's cache key will differ. This should become unnecessary with rust-lang/cargo#14136
1 parent 7544585 commit d9c437e

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

rust/Earthfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ VERSION 0.8
33
# Arguments:
44
# - cache_prefix: Overrides cache prefix for cache IDS. Its value is exported to the build environment under the entry: $EARTHLY_CACHE_PREFIX. By default ${EARTHLY_TARGET_PROJECT_NO_TAG}#${OS_RELEASE}#earthly-cargo-cache
55
# - keep_fingerprints (false): Instructs the following +CARGO calls to not remove the Cargo fingerprints of the source packages. Use only when source packages have been COPYed with --keep-ts option.
6+
# - do_not_cache_path_based_dependencies (false): Instructs the following +CARGO calls to remove any path based dependencies from the target cache.
67
# - sweep_days (4): +CARGO uses cargo-sweep to clean build artifacts that haven't been accessed for this number of days.
78
INIT:
89
FUNCTION
@@ -28,6 +29,10 @@ INIT:
2829
ARG keep_fingerprints=false
2930
ENV EARTHLY_KEEP_FINGERPRINTS=$keep_fingerprints
3031

32+
# $EARTHLY_DO_NOT_CACHE_PATH_BASED_DEPENDENCIES
33+
ARG do_not_cache_path_based_dependencies=false
34+
ENV EARTHLY_DO_NOT_CACHE_PATH_BASED_DEPENDENCIES=$do_not_cache_path_based_dependencies
35+
3136
# $EARTHLY_SWEEP_DAYS
3237
ARG sweep_days=4
3338
ENV EARTHLY_SWEEP_DAYS=$sweep_days
@@ -62,6 +67,9 @@ CARGO:
6267
RUN --mount=$EARTHLY_RUST_CARGO_HOME_CACHE --mount=$EARTHLY_RUST_TARGET_CACHE \
6368
set -e; \
6469
cargo $args; \
70+
if [ "$EARTHLY_DO_NOT_CACHE_PATH_BASED_DEPENDENCIES" = "true" ]; then \
71+
cargo metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.id | startswith("path+file://")) | .name' | xargs -I{} cargo clean -p {}; \
72+
fi; \
6573
cargo sweep -r -t $EARTHLY_SWEEP_DAYS; \
6674
cargo sweep -r -i; \
6775
$EARTHLY_FUNCTIONS_HOME/copy-output.sh "$output";

0 commit comments

Comments
 (0)