What happens when you output the same variable with `cargo::metadata` in a build script?

⚓ Rust    📅 2025-09-24    👤 surdeus    👁️ 7      

surdeus

Warning

This post was published 37 days ago. The information described in this article may have changed.

The links documentation doesn't mention what happens when the same variable is set with cargo::metadata in a build script.

For example in a build.rs file:

println!("cargo::metadata=MYVAR=1");
println!("cargo::metadata=MYVAR=2");
// ...

Scanning the cargo code, it looks like the latest print statement will overrule all others before it and my experimentation appears to confirm this; downstream build scripts will see DEP_<LINKS_NAME>_MYVAR set to 2 only. Perhaps this gap should be documented in the cargo documentation?

I noticed the openssl-sys crate had a scenario where this cargo directive was being printed in a loop which I found confusing (I know it isn't explicitly printing cargo::metadata but this is just the old way to set metadata). Why not just print the last one since the last one overrules all the others?

1 post - 1 participant

Read full topic

🏷️ Rust_feed