cross-rs / cross

“Zero setup” cross compilation and “cross testing” of Rust crates
Apache License 2.0
6.49k stars 365 forks source link

CACHEDIR.TAG is not written when using custom docker image #1324

Closed vaffeine closed 1 year ago

vaffeine commented 1 year ago

Checklist

Describe your issue

Cross doesn't write CACHEDIR.TAG file when using a custom docker image, e.g. having a pre-build step in Cross.toml. The file is created if no custom image is used.

Steps to reproduce:

  1. Create an empty cargo project
  2. Create a Cross.toml with the following content
    [target.x86_64-unknown-linux-gnu]
    pre-build = ["echo"]
  3. Run cross build --target x86_64-unknown-linux-gnu
  4. There is no CACHEDIR.TAG in target/x86_64-unknown-linux-gnu/

I have encountered this issue while investigating why https://github.com/Swatinem/rust-cache was not caching cross-compilation targets during CI builds. rust-cache uses CACHEDIR.TAG to detect sub-target directories that need to be cached and stopped doing that after we have switched from cargo to cross.

I had a brief look into it and it looks like the code that writes CACHETAG.DIR (added in https://github.com/cross-rs/cross/pull/836) doesn't write it if the target directory already exists (assumption is that it was created by cargo). However, from my debugging it looks like this directory is created before this check by Dockerfile::build. Which causes the target directory to only have Dockerfile.*-custom file.

I can submit a PR that changes the check in fn create_target_dir from if !path.exists() to if !cachedir_file.exists(), but wanted to check if there is a better way to fix it since I only did a very shallow analysis of the issue.

Relates to https://github.com/cross-rs/cross/issues/835.

What target(s) are you cross-compiling for?

aarch64-unknown-linux-gnu, x86_64-unknown-linux-gnu

Which operating system is the host (e.g computer cross is on) running?

What architecture is the host?

What container engine is cross using?

cross version

cross 0.2.5

Example

No response

Additional information / notes

No response

Emilgardis commented 1 year ago

Well spotted! This should probably be solved by writing the tag when writing the Dockerfile, feel free to create a PR :)