Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear which environment variables are passed to build.rs #9403

Closed
Hawk777 opened this issue Apr 24, 2021 · 1 comment · Fixed by #9405
Closed

Unclear which environment variables are passed to build.rs #9403

Hawk777 opened this issue Apr 24, 2021 · 1 comment · Fixed by #9405
Labels
A-documenting-cargo-itself Area: Cargo's documentation A-environment-variables Area: environment variables C-bug Category: bug E-easy Experience: Easy

Comments

@Hawk777
Copy link
Contributor

Hawk777 commented Apr 24, 2021

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

@Hawk777 Hawk777 added the C-bug Category: bug label Apr 24, 2021
@ehuss
Copy link
Contributor

ehuss commented Apr 24, 2021

Hi! Sorry about the confusion. Indeed the CARGO_PKG_* environment variables are also set when running the build script. It looks like those are just missing from the list in the Environment variables Cargo sets for build scripts section. I think just a single bullet point could be added to the docs indicating that the CARGO_PKG_* environment variables listed in the Environment variables Cargo sets for crates section are also set when running the build script (as as to avoid duplicating that long list).

@ehuss ehuss added A-documenting-cargo-itself Area: Cargo's documentation E-easy Experience: Easy A-environment-variables Area: environment variables labels Apr 24, 2021
@bors bors closed this as completed in d1baf0d Apr 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documenting-cargo-itself Area: Cargo's documentation A-environment-variables Area: environment variables C-bug Category: bug E-easy Experience: Easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants