#5720·anki

anki_proto 的构建脚本在每次 cargo 调用时都会重新运行,重新编译引擎

作者: RexRenatus创建于 2026年9月28日更新于 2026年9月28日

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 through write_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 1. In a checkout of main, with PROTOC pointing at a protoc binary, run cargo build -p anki_proto. 2. Run cargo build -v -p anki_proto twice more without changing anything. From the second of these on, cargo reports Dirty anki_proto v0.0.0 (...): the file `.../debug/build/anki_proto-<hash>/out/anki.deck_config.rs` has changed (...), reruns the build script and recompiles anki_proto. (The first rerun may name the descriptors file instead, because that file is created during the first build.) 3. In a crate that depends on anki as a git dependency, cargo build -v reports the same reason for anki_proto, then Dirty 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 10 s on a Linux machine with a fast CPU).