anki_proto's build script reruns on every cargo invocation, recompiling the engine
Since #4439, write_file_if_changed() prints cargo:rerun-if-changed for every file that the anki_proto and anki_i18n build scripts write through it (rslib/io/src/lib.rs#L352-L358). For anki_proto, that includes the prost output in OUT_DIR, which the build script rewrites on every run:
- rslib/proto/rust.rs#L52 runs prost-build into
OUT_DIR. prost-build writes a file only if its content differs from what is on disk, and the copy on disk carries the#[must_use]lines added on the previous run, so every file is written. - rslib/proto/rust.rs#L64 then calls
add_must_use_annotations(), which adds the#[must_use]lines and writes each file again throughwrite_file_if_changed()(rslib/proto_gen/src/lib.rs#L253), which registers it.
The content ends up byte-identical, but each file is newer than the start of the run, and that is what cargo compares rerun-if-changed paths against. So the next cargo invocation reruns the build script, which rewrites the files again. anki_proto, anki and everything that depends on them are rebuilt on every invocation, with nothing changed.
This costs the most in crates that depend on anki as a git dependency: every cargo build, check, test or clippy recompiles anki_proto and anki. With a crate depending on anki through a local git = "file://..." source on a Linux machine, a cargo build with nothing changed took 35-49 s.
Steps to reproduce
- In a checkout of
main, withPROTOCpointing at a protoc binary, runcargo build -p anki_proto. - Run
cargo build -v -p anki_prototwice more without changing anything. From the second of these on, cargo reportsDirty anki_proto v0.0.0 (...): the file `.../debug/build/anki_proto-<hash>/out/anki.deck_config.rs` has changed (...), reruns the build script and recompilesanki_proto. (The first rerun may name the descriptors file instead, because that file is created during the first build.) - In a crate that depends on
ankias a git dependency,cargo build -vreports the same reason foranki_proto, thenDirty anki v0.0.0 (...): the dependency `anki_proto` was rebuilt, and recompiles the engine on every invocation.
Possible fix
Stop registering paths that are inside the build script's OUT_DIR. Cargo owns that folder, so watching it gains nothing. The files outside it that #4439 was about, such as out/pylib/anki/_backend_generated.py, stay registered, so deleting one still makes cargo regenerate it.
With that change, repeated builds of anki_proto are Fresh (about 0.2 s here), and the git-dependency case above drops to about 0.3 s. I can open a PR with it: a small change to rslib/io, with tests.
Source: ankitects/anki