-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
r? @ehuss (rust_highfive has picked a reviewer for you, use r? to override) |
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 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" |
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. |
More power to you. That is the kind of thankless mind bending work that moves the community forward. Thank you. |
See also a new feature request #9661 |
In public GitHub repos one can find almost infinite creative and awful ways that crates try to guess the Cargo target dir.
$manifest_dir/target
way, which does not work when built with--target-dir
or when building as a dependency of a different cratecargo metadata | jq .target_directory
way, which does not work when built with--target-dir
$out_dir/../../..
way, which is an assumption about how deeply nested Cargo's placement of out dirs will be in the future$current_dir/target
way, which does not work when built with--target-dir
or as a dependency$manifest_dir/../target
way, as above but hardcoding this crate's placement in its workspace.cargo/config
and.cargo/config.toml
before falling back to one of the previous$current_exe/../..
way$current_exe/../../conditional ..
waycargo/crates/cargo-test-support/src/paths.rs
Lines 18 to 29 in e4b65bd
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.