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

Set CARGO_TARGET_DIR for build scripts #8710

Closed
wants to merge 3 commits into from

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Sep 18, 2020

In public GitHub repos one can find almost infinite creative and awful ways that crates try to guess the Cargo target dir.

Now, it's likely many of these should not be trying to poke around in the target dir in the first place, but the fact remains that there are motivating reasons for wanting to. For example, to perform some work once that will be shared/available to multiple crates' build scripts to avoid repeating the same work in each -- https://github.com/dtolnay/scratch supports this use case but it involves crates writing into a different crate's OUT_DIR, which is even more of a hack than just using a well-namespaced subdirectory of the target dir.

This PR adds an environment variable CARGO_TARGET_DIR set by Cargo for build scripts to inform them of Cargo's placement of the target dir. This provides the correct value even when your build was invoked with --target-dir or when the build script is for a crate that is not the build root so $manifest_dir/target would not be a good guess.

@rust-highfive
Copy link

r? @ehuss

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 18, 2020
@alexcrichton
Copy link
Member

Thanks for the PR here, and also thanks for gathering all those links together! I didn't realize there were so many crates doing so many different things to find the target directory.

I don't really know what to do about this though. As you've documented here it's not stable to rely on anything about the target directory. Given that, how can crates productively use this environment variable? This also doesn't even begin to touch on the concurrency aspects where there's no protection against crates concurrently using the target directory in different ways.

On one hand I don't want to be forced into a solution which only makes sense for a small subset of users, but I also don't want to prevent something from happening if there's a clear need for it.

I realize this may be difficult to do, but I think it would be useful to try to see if there's something else Cargo can do in a stable way to address the majority of these use cases. For example with your scratch crate if the main purpose is to work with cargo clean, maybe we could add a feature to build scripts to register a directory with Cargo that gets cleaned with cargo clean (outside the target directory). Or maybe we could reserve target/debug/scratch for all crates and set that for build scripts if all they need is shared global scratch space for the whole build.

I'd basically like to be able to comfortably document this as "please use this, it will remain stable" rather than having documentation of "here's a thing we were forced to give you and may break in the future, so while we say don't use it you're probably going to use it anyway"

@dtolnay
Copy link
Member Author

dtolnay commented Sep 19, 2020

Thanks Alex -- that makes perfect sense. I think I'll close this for now, look through each of those crates more carefully to categorize exactly what they need or what already supported thing they should be doing instead, and come back with an appropriate proposal.

@dtolnay dtolnay closed this Sep 19, 2020
@dtolnay dtolnay deleted the target branch September 19, 2020 03:12
@Eh2406
Copy link
Contributor

Eh2406 commented Sep 19, 2020

More power to you. That is the kind of thankless mind bending work that moves the community forward. Thank you.

@weihanglo
Copy link
Member

See also a new feature request #9661

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants