Problem
The Cargo book has a list of environment variables that are passed to build.rs (“Environment variables Cargo sets for build scripts”). That list does not mention any variables starting with CARGO_PKG_. However, in reality, Cargo does set the CARGO_PKG_ variables when calling build.rs. Those variables are only mentioned in “Environment variables Cargo sets for crates”. I initially thought that meant that all variables in the latter section would also be available to build.rs; however, that is also not true: CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_*, and CARGO_PRIMARY_PACKAGE are not set when build.rs runs.
Steps
Write a build.rs that prints out all environment variables prefixed with cargo:warning= and run cargo build.
Possible Solution(s)
I don’t know what the behaviour is supposed to be:
- If only the variables listed in “sets for build scripts” are supposed to be exposed to
build.rs, then there is a bug because CARGO_PKG_ are not in that list so they should be removed from the environment.
- If all the variables in “sets for crates” are also supposed to be exposed to
build.rs, then there is a bug because CARGO_CRATE_NAME (and probably a few others) are not exposed; also, I did not find it clear from the book that those variables were intended to be exposed to build.rs so perhaps the wording could be cleaned up.
- If
CARGO_PKG_ specifically, but not all the variables from “sets for crates”, are supposed to be exposed to build.rs, then the book should say so in the “sets for build scripts” section.
Output of cargo version: 1.49.0
Problem
The Cargo book has a list of environment variables that are passed to
build.rs(“Environment variables Cargo sets for build scripts”). That list does not mention any variables starting withCARGO_PKG_. However, in reality, Cargo does set theCARGO_PKG_variables when callingbuild.rs. Those variables are only mentioned in “Environment variables Cargo sets for crates”. I initially thought that meant that all variables in the latter section would also be available tobuild.rs; however, that is also not true:CARGO_CRATE_NAME,CARGO_BIN_NAME,CARGO_BIN_EXE_*, andCARGO_PRIMARY_PACKAGEare not set whenbuild.rsruns.Steps
Write a
build.rsthat prints out all environment variables prefixed withcargo:warning=and runcargo build.Possible Solution(s)
I don’t know what the behaviour is supposed to be:
build.rs, then there is a bug becauseCARGO_PKG_are not in that list so they should be removed from the environment.build.rs, then there is a bug becauseCARGO_CRATE_NAME(and probably a few others) are not exposed; also, I did not find it clear from the book that those variables were intended to be exposed tobuild.rsso perhaps the wording could be cleaned up.CARGO_PKG_specifically, but not all the variables from “sets for crates”, are supposed to be exposed tobuild.rs, then the book should say so in the “sets for build scripts” section.Output of
cargo version: 1.49.0