Is it possible for build.rs to get "changed file names" which caused the re-compiling?

Package foo's file changed and led to recompiling of package bar. What if package bar wants to know who caused this re-compiling? Can cargo set an environment variable e.g. CARGO_FILES_CHANGED to tell the file names in package bar's build.rs ?

Currently there's no such feature.

If there are multiple things you build in build.rs, you could split the crate into multiple crates each with its own build.rs.

If you can't, and rebuilds are expensive, you'll need to invent your own incremental build mechanism without Cargo's help.


If you're proposing addition of such mechanism, then it sounds like it's possible to do. The downside I see is that currently Cargo can start rebuild on the first change it finds. OTOH supplying a list of changed files would require giving a complete list of changed files, so it'd require Cargo to keep checking all files.

It would be nice if such feature is available. Splitting the crate is not an option in my use case.

One more thing: package A's file changed and led to recompiling of package B, while package C's file changed and led to recompiling of package D, but package B's build.rs is not expected to see package C's file in CARGO_FILE_CHANGED.

A straight forward implementation in general is to write a list of inputs and their content hash to $OUT_DIR and compare them each build, but it's not clear whether what you're trying to do here (comparing sources for a dependency package?) would work with the information build.rs has.

I'm my experience, you don't want to push much of any developer experience stuff into build.rs, you should be aiming "above" cargo, ie running code that runs cargo; not "below", ie getting run by cargo.

My pattern for this is to add a make = "run --package make" alias in .cargo/config.toml, add that make crate into the workspace, then use the cargo_metadata crate to get any information about the project's paths and the like. Using crate metadata as a configuration source is particularly nice.

1 Like