Skip to content

Commit e95193d

Browse files
committed
Add an FAQ for "Why is my crate rebuilt?"
In hindsight we should have added this long ago, but I feel like we get this enough that it's worth adding a deidcated entry we can point to.
1 parent 140fbc9 commit e95193d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/doc/src/faq.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,61 @@ replacement][replace].
199199
[replace]: reference/source-replacement.md
200200
[`cargo fetch`]: commands/cargo-fetch.md
201201
[offline config]: reference/config.md#netoffline
202+
203+
### Why is Cargo rebuilding my code?
204+
205+
Cargo is responsible for incrementally compiling crates in your project. This
206+
means that if you type `cargo build` twice the second one shouldn't rebuild you
207+
crates.io dependencies, for example. Nevertheless bugs arise and Cargo can
208+
sometimes rebuild code when you're not expecting it!
209+
210+
We've long [wanted to provide better diagnostics about
211+
this](https://github.com/rust-lang/cargo/issues/2904) but unfortunately haven't
212+
been able to make progress on that issue in quite some time. In the meantime,
213+
however, you can debug a rebuild at least a little by by setting the `CARGO_LOG`
214+
environment variable:
215+
216+
```sh
217+
$ CARGO_LOG=cargo::core::compiler::fingerprint=info cargo build
218+
```
219+
220+
This will cause Cargo to print out a lot of information about diagnostics and
221+
rebuilding. This can often contain clues as to why your project is getting
222+
rebuilt, although you'll often need to connect some dots yourself since this
223+
output isn't super easy to read just yet. Note that the `CARGO_LOG` needs to be
224+
set for the command that rebuilds when you think it should not. Unfortunately
225+
Cargo has no way right now of after-the-fact debugging "why was that rebuilt?"
226+
227+
Some issues we've seen historically which can cause crates to get rebuilt are:
228+
229+
* A build script prints `cargo:rerun-if-changed=foo` where `foo` is a file that
230+
doesn't exist and nothing generates it. In this case Cargo will keep running
231+
the build script thinking it will generate the file but nothing ever does. The
232+
fix is to avoid printing `rerun-if-changed` in this scenario.
233+
234+
* Two successive Cargo builds may differ in the set of features enabled for some
235+
dependencies. For example if the first build command builds the whole
236+
workspace and the second command builds only one crate, this may causes a
237+
dependency on crates.io to have a different set of features enabled, causing
238+
it and everything that depends on it to get rebuilt. There's unfortunately not
239+
really a great fix for this, although if possible it's best to have the set of
240+
features enabled on a crate constant regardless of what you're building in
241+
your workspace.
242+
243+
* Some filesystems exhibit unusual behavior around timestamps. Cargo primarily
244+
uses timestamps on files to govern whether rebuilding needs to happen, but if
245+
you're using a nonstandard filesystem it may be affecting the timestamps
246+
somehow (e.g. truncating them, causing them to drift, etc). In this scenario
247+
feel free to open an issue and we can see if we can accomodate the filesystem
248+
somehow.
249+
250+
* A concurrent build process is either deleting artifacts or modifying files.
251+
Sometimes you might have a background process that either tries to build or
252+
check your project. These background processes might surprisingly delete some
253+
build artifacts or touch files (or maybe just by accident), which can cause
254+
rebuilds to look spurious! The best fix here would be to wrangle the
255+
background process to avoid clashing with your work.
256+
257+
If after trying to debug your issue, however, you're still running into problems
258+
then feel free to [open an
259+
issue](https://github.com/rust-lang/cargo/issuses/new)!

0 commit comments

Comments
 (0)