Build.rs file does not work

I am trying to generate version hash of every build from git hash but unfortunately I cannot get build.rs file working.
For testing purposes I just put a println!() inside the main function in the build.rs but I never see the log before builds. Am I doing something wrong ?

Would you mind providing us with some more information/code about your current circumstance?

I have created a file called build.rs on the project root which has only
fn main(){println!("testing")} written in. I’ve also added build = "build.rs" in my Cargo.toml file
before every build I expect to see “testing” printed on the console but there’s nothing

I’ve found that adding the line in the Cargo.toml isn’t always needed, as long as there is a build.rs file with a fn main().
About the output of the build script, don’t try to test output with a println. Other cargo functions like test and build (Specifically build.rs) block terminal output, and don’t print it out. So, when testing things like these, panic instead. If you do panic, you will end up with a compilation error, meaning that it’s actually been run.
Read more on build.rs output here

Thanks panicking was the way to go for testing

Don’t use it in a regular production build though :sweat_smile: (As in something you do every build)

:slight_smile: I just wanted to see if it works :smiley: I will use it to generate the version string than include it during build in the main file

1 Like

try

cargo build -vv

Do you know that cargo run build.rs script by default only when build.rs file is changed?
So if you edit main.rs or lib.rs and then run cargo build then cargo doesn’t run build.rs, because of there is no changes in build.rs.