rust-lang / rust-lang/cargo

The `TARGET` environment variable does not point to the file when a target JSON file is used

Open
#8,646 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-build-scripts A-environment-variables A-target-spec C-bug S-needs-design
Dominant language
Rust
Stars
15.5k
Forks
3k
Avg merge
23h 30m
Merged PRs (30d)
51

Description

Steps

  1. cargo new repro && cd repro

  2. rustc +nightly -Z unstable-options --print target-spec-json --target x86_64-apple-darwin > x86_64-apple-darwin.json

    The JSON filename should match the target to avoid some strange cases in the compiler. This should occur on other platforms as well.

  3. Create build.rs

    fn main() {
        dbg!(std::env::var("TARGET"));
    }
    
  4. cargo +nightly build --target $PWD/x86_64-apple-darwin.json -Z build-std --verbose --verbose --verbose

    The build-std isn't really important to this issue, it's only used to support the non-standard target specification.

You'll see the TARGET variable no longer has the path and .json extension in the output:

[repro 0.1.0] [build.rs:2] std::env::var("TARGET") = Ok(
[repro 0.1.0]     "x86_64-apple-darwin",
[repro 0.1.0] )

This caused an issue in the rust-lang/rust repository while I was trying to port to aarch64-apple-darwin by using a JSON target file. Specifically, indexmap uses autocfg which uses TARGET to build things.

This was failing because my target didn't yet exist in the compiler, so autocfg decided it was the wrong version of rustc.

Possible Solution(s)

It seems like it should include the full path to the JSON file in the environment variable, but I'm not sure what the impact of such a change would be.

Notes

% cargo +nightly version
cargo 1.47.0-nightly (51b66125b 2020-08-19)
% rustc +nightly --version
rustc 1.47.0-nightly (5180f3da5 2020-08-23)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with the reported cargo command and the minimal build.rs that reads TARGET. Trace where Cargo sets TARGET for JSON target specifications, then define and test the expected path semantics, including the impact on consumers such as autocfg.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.