Skip to content

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

@Hawk777

Description

@Hawk777

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions