Cargo downloading crates when target dir exists inside Gitlab Docker pipeline
Problem
Hello everyone!
I'm trying to create a simple Gitlab pipeline with a basic lib, right after cargo new --lib with a single dependency - chrono
Pipeline is containing two parts:
- building lib with command
cargo build - running tests with command
cargo teston the builded code with thetargetdir
Every part is running inside the rust:alpine3.16 image
target dir is shared between parts via artifacts
For preventing crates download, I'm using the commands --locked --offline, but getting an error:
error: no matching package named `chrono` found
location searched: registry `crates-io`
required by package `temp_lib v0.1.0 (/builds/test_lib/libs/rust/templib)`
As a reminder, you're using offline mode...
I have a commited Cargo.lock file in repo, so as far as I understand, the command --locked should work, but it doesn't
I'm also running ls command before the script and all files inside the target dir are existing:
$ cd target/debug/deps
$ ls -la
total 24008
drwxr-xr-x 2 root root 4096 Nov 1 21:38 .
drwxr-xr-x 7 root root 4096 Nov 1 21:38 ..
...
-rw-r--r-- 1 root root 10322 Nov 1 21:38 chrono-9b23c9d5e91b34f0.d
...
So as you can see, the chrono dependency exists, but cargo ignores this fact and trying to download
And if I've remove the --offline command, I can see this result:
$ cargo test --locked
Updating crates.io index
Downloading crates ...
Downloaded autocfg v1.1.0
Downloaded iana-time-zone v0.1.53
Downloaded libc v0.2.137
Downloaded time v0.1.44
Downloaded chrono v0.4.22
Downloaded num-traits v0.2.15
Downloaded num-integer v0.1.45
Compiling temp_lib v0.1.0 (/builds/test_lib/libs/rust/templib)
Finished test [unoptimized + debuginfo] target(s) in 1m 58s
Running unittests src/lib.rs (temp_lib-c650e41a48a31713)
As you can see, the dependencies just downloaded, but not compiled, because of existing target dir I guess
Here is my .gitlab-ci.yml:
image: rust:alpine3.16
stages:
- testing
before_script:
- apk add musl-dev openssl-dev protobuf-dev protoc git
building:
stage: testing
artifacts:
paths:
- target/
script:
- cargo build
tests:
needs:
- building
stage: testing
dependencies:
- building
script:
- cd target/debug/deps
- ls -la
- cargo test --locked --offline
Steps
- Create a fresh lib
- Add
chrono - Add
.gitlab-ci.ymlfrom my post above - Push it to Gitlab
Possible Solution(s)
No response
Notes
No response
Version
cargo 1.64.0 (387270bc7 2022-09-16)
release: 1.64.0
commit-hash: 387270bc7f446d17869c7f208207c73
commit-date: 2022-09-16
host: x86_64-unknown-linux-musl
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1q)
os: Alpine Linux 3.16.2 [64-bit]
I'm not terribly familiar with GitLab, but I suspect the issue is that each stage is independent (other than the "artifacts" passed between them).
Cargo requires its cache directory (CARGO_HOME, usually ~/.cargo) in order to operate (even if offline). I suspect that cargo is populating the cache in your first stage, but it is missing in the second.
I'm not sure of the best way to work with that in GitLab, but I think somehow you need to also carry over the cache in the cargo home directory. The relevant directory is ~/.cargo/registry/.
Alternatively, you can look at vendoring with source replacement, typically with cargo vendor, which places all the source files in your local tree. That does not require the stuff cached in the home directory.
BTW, you may get better advice on one of the user forums such as https://users.rust-lang.org/, where there may be more people with experience with GitLab.
@ehuss, thanks!
cargo vendor is what I need