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

cargo metadata does not take CARGO_BUILD_TARGET into account #8791

Open
jyn514 opened this issue Oct 16, 2020 · 8 comments
Open

cargo metadata does not take CARGO_BUILD_TARGET into account #8791

jyn514 opened this issue Oct 16, 2020 · 8 comments
Labels
C-bug Category: bug

Comments

@jyn514
Copy link
Member

jyn514 commented Oct 16, 2020

Problem

cargo metadata should print the correct target directory, but when CARGO_BUILD_TARGET is set, still prints the default instead of the directory that will actually be used.

Steps

$ rm -rf target
$ export CARGO_BUILD_TARGET=x86_64-unknown-linux-gnu
$ cargo doc
 Documenting test-doc v0.1.0 (/home/joshua/src/rust/test-doc)
    Finished dev [unoptimized + debuginfo] target(s) in 0.80s
$ ls target/doc
ls: cannot access 'target/doc': No such file or directory
$ ls target/x86_64-unknown-linux-gnu/doc
ayu.css         favicon.ico            LICENSE-MIT.txt  rustdoc.css      settings.js                  source-script.js                 src
brush.svg       FiraSans-LICENSE.txt   light.css        rust-logo.png    SourceCodePro-LICENSE.txt    SourceSerifPro-Bold.ttf.woff     storage.js
COPYRIGHT.txt   FiraSans-Medium.woff   main.js          search-index.js  SourceCodePro-Regular.woff   SourceSerifPro-It.ttf.woff       test_doc
dark.css        FiraSans-Regular.woff  normalize.css    settings.css     SourceCodePro-Semibold.woff  SourceSerifPro-LICENSE.md        theme.js
down-arrow.svg  LICENSE-APACHE.txt     noscript.css     settings.html    source-files.js              SourceSerifPro-Regular.ttf.woff  wheel.svg
$ cargo metadata | jq .target_directory
warning: please specify `--format-version` flag explicitly to avoid compatibility problems
"/home/joshua/src/rust/test-doc/target"

Possible Solution(s)

Add the target directory to target_directory.

Notes

Output of cargo version:

cargo 1.48.0-nightly (9d1a4863a 2020-10-05)

@ehuss
Copy link
Contributor

ehuss commented Oct 17, 2020

CARGO_BUILD_TARGET doesn't change the target directory, though it does change the layout within the target directory. If specifying a target, then the output is in $target_dir/$target/.... This is explained a bit in https://doc.rust-lang.org/cargo/guide/build-cache.html.

@jyn514
Copy link
Member Author

jyn514 commented Oct 17, 2020

I see. Is there a way to get the doc directory from cargo metadata? I don't see target/doc anywhere in the metadata output, but I might have missed it.

I'd prefer to avoid having to mimic cargo's logic for determining the structure within the target/ directory, AFAIK that's not intended to be stable.

@ehuss
Copy link
Contributor

ehuss commented Oct 20, 2020

I think the specific layout rules are stable and not something we will likely change. That is:

  • $CARGO_TARGET_DIR/doc is the doc output if --target is not specified.
  • $CARGO_TARGET_DIR/$TARGET/doc is the doc output if --target is specified.

It doesn't look like cargo metadata gives you $TARGET, so that is a little awkward if you are using config files. If possible, you can also use JSON messages from cargo which will tell you the output path from rustdoc.

@jyn514
Copy link
Member Author

jyn514 commented Oct 20, 2020

I'm not running cargo directly, so I have to be able to find the information after the fact. It sounds like there isn't currently a way to do that - would you take a PR adding $TARGET to cargo metadata?

@ehuss
Copy link
Contributor

ehuss commented Oct 22, 2020

We are a little reluctant to add the default target to cargo metadata because Cargo is moving towards supporting multiple targets during the same build, so it is not really clear how that would be exposed. Additionally, cargo metadata is geared for pre-build information, and we don't want to go further down the road towards including things like artifact outputs.

Would it work to have something like cargo config build.target which would fetch and display the confg value if it is defined?

@jyn514
Copy link
Member Author

jyn514 commented Oct 22, 2020

I'm a little confused why this needs a new command ... cargo config build.target would work fine for me but I guess I'm not sure why this is so different from the rest of cargo metadata?

Cargo is moving towards supporting multiple targets during the same build

Where could I learn more about this?

@jyn514
Copy link
Member Author

jyn514 commented Oct 22, 2020

Also, FWIW cargo-deadlinks looks at all cargo targets, so if this information was per-target instead of global that would be perfectly fine.

@ehuss
Copy link
Contributor

ehuss commented Oct 22, 2020

cargo metadata is mostly intended for querying the workspace and resolve configuration. It's actually a bug that it looks at the target at all (#8462).

Where could I learn more about this?

https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#multitarget and #7004 and #6784

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants