How to force build.rs run after a commit?

I have a build.rs that reads the current commit information and writes it to program env, and I recently discovered a problem: I cargo run to test if the changes work well before committing. Note that at this point I haven't committed this changes yet, so the "current commit" is still the previous commit of the changes that will be committed, and the program contains is also the previous commit. When I make sure the changes works and commit, I cargo run again, and cargo will use the binary that was built before committing which contains the previous commit, and this creates inconsistency.

You need to tell cargo that build.rs should be rerun if the file .git/HEAD has changed by adding this line to your build.rs:

println!("cargo:rerun-if-changed=.git/HEAD");

See "Outputs of the Build Script" for more information.

3 Likes

Alternatively, you could also re-use existing solutions for reading commit information into the program env.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.