ipetkov / crane

A Nix library for building cargo projects. Never build twice thanks to incremental artifact caching.
https://crane.dev
MIT License
959 stars 92 forks source link

Dependencies with C bindings being rebuilt every time #682

Closed bonofiglio closed 3 months ago

bonofiglio commented 3 months ago

Hi, I'm running into rebuilds for some of my dependencies, but not all of them. Specifically, it seems like the cargoArtifacts are missing the dependencies with C bindings. In this case surrealdb with thespeedb feature enabled (a C++ project).

I summarized the logs a little to show the problem more concisely:

[...]
   Compiling dotenvy v0.15.7
   Compiling surrealdb v1.5.4
   Compiling my_crate v0.1.0 (/build/source)
    Finished `release` profile [optimized] target(s) in 9m 51s
buildPhase completed in 18 minutes 9 seconds
[...]
Running phase: installPhase
no previous artifacts found, compressing and installing full archive of target to /nix/store/5yqm4ziffm4idxgcm8cgy0i3vgxw0j96-my_crate-deps-0.1.0/target.tar.zst
[...]
decompressing cargo artifacts from /nix/store/5yqm4ziffm4idxgcm8cgy0i3vgxw0j96-my_crate-deps-0.1.0/target.tar.zst to target
[...]
++ command cargo build --release --message-format json-render-diagnostics --locked
   Compiling bindgen v0.65.1
   Compiling libspeedb-sys v0.0.4+2.7.0
   Compiling speedb v0.0.4
   Compiling surrealdb-core v1.5.1
   Compiling surrealdb-core v2.0.0-1.5.4
   Compiling surrealdb v1.5.4
   Compiling my_crate v0.1.0 (/build/source)
    Finished `release` profile [optimized] target(s) in 11m 54s

As you can see, there are 6 crates other than my-crate being built on the second step, but they were present on the first build, so they are being built twice. This wouldn't be such an issue in most cases, but these dependencies are about 80~% of the compile time of my crate, so caching them is essential for CI in my case.

I tried looking around the docs to see if there was an option for this but I couldn't find anything.

Thank you for your time and for crane. Loving the project so far.

dpc commented 3 months ago

Use cargo -vvv (or something like it) to figure out the reason for the rebuild.

ipetkov commented 3 months ago

Hi @bonofiglio thanks for the report! I see bindgen showing up in the build log so you might be hitting this situation: https://github.com/ipetkov/crane/blob/7ce92819802bc583b7e82ebc08013a530f22209f/docs/faq/rebuilds-bindgen.md

If that's not the culprit I would double check whether the same buildInputs and nativeBuildInputs are being used across the derivation producing cargoArtifacts (e.g. craneLib.buildDepsOnly) and the derivation that is consuming it. I know that sometimes adding different inputs causes certain environment variables/paths to be updated, and a number of crate build scripts configure themselves to be re-run if those variables change.

I'm afraid I can't suggest anything beyond that without a flake which reproduces the problem!

bonofiglio commented 3 months ago

Thank you for your both your answers @ipetkov and @dpc. The issue was exactly the one linked by @ipetkov. That is a very hard one to debug 😅