-
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
Add CARGO_WORKSPACE_DIR env variable. #4787
Conversation
This variable refers to the workspace this package is inside of.
Weird I thought we already did this! I think I was thinking of #3946 which I think just never got landed? I'll take a look! |
The idea seems fine by me, although perhaps we could store the workspace root directly in a |
Added this to the existing env var checks. Looked into adding the path to the compilation type but how the compilation type is built confused me. |
@withoutboats what are the use-cases for Mainly, workspaces are not restricted to a single directory, you can refer to arbitrary paths from I think using |
The directory contains the root manifest as well as the lockfile, both of which may be useful for codegen purposes. |
Oh, and we do need docs for this (there's environmental variables section) =P |
The docs are in the crates.io repo, right (not sure how to handle the stable/nightly aspect of the docs, but I assume there's a policy in place)? |
Uhm, I am not sure. At least, there's still |
One more potential gotcha is the difference in building the crate locally vs building a crate as a dependency from crates.io. I.e, if you build a crate |
Also cc #4764 which might provide an alternative solution to the problem at hand: at the codegen time, you can call Does this all make sense? I certainly understand the need to know more about the package being build, but I am not sure what is the most reliable and dependable way to give this information :) |
Oh excellent points @matklad! The docs in I wonder if we should also only export this env var for members themselves of the workspace? That I think would solve the crates.io problem as well. I'm also slightly wary of giving crates.io crates access to the main directory of the build... |
That means that building a crate locally and building crate as a crates.io dependency does different things. I imagine the following situation:
Not sure how likely a similar scenario is likely to arise, but the result looks pretty bad to me. The key thing is that we can't say that something is going wrong until the very last moment. I think it's fine to have |
Hm good point! We could definitely make Hm... Unsure :( |
@withoutboats I think it's not the same as |
It seems like we should have a larger convo about motivations. My personal goal was to be able to consistently read the lockfile to be able to gain info about dependencies. @matklad mentions that the lockfile format is an implementation detail, but we have certain compatibility guarantees around it - definitely backwards compatibility and probably a certain amount of forward compatibility if you don't use any new features. I would describe the lockfile format as stable. However, likely it is better to just run cargo metadata and parse the output of that process. In that case, I'm not sure what the benefit of having this variable is, and possibly this PR should be closed because of the sharp edges @matklad has identified. But does that raise some of the same issues about being built as a library vs being built as the root? What is the correct way to invoke cargo metadata from a build script? Is this sufficient?: let cargo = env::var_os("CARGO").unwrap();
let dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let cmd = Command(cargo).arg("metadata").current_dir(dir); |
Maybe this is straying off topic, but more generally, should the cargo metadata output to be expanded to include everything in both |
Which kind of dependency information exactly? There's dependencies declared in your crate ( This sounds a bit like "an upstream dependency wants to know resolved dependencies of the downstream crate and make some decisions based on it", which is not too comfortable. Especially, upstream inquiring about downstream seems philosophically wrong?
It would be great to know which way is correct, nobody specifically designed for this use case :) I think the proposed snippet has a problem though: So, I think getting resolved dependencies is not possible in a nice way, because this info is available only downstream. Getting declared dependencies should be OK, but probably less useful.
I think this is more or less the case now.
Lockfiles generated by Cagro |
I'm just going to close this because of the edge cases and questions involved. Seems best to punt for now. Thanks for the review though @matklad and @alexcrichton :) |
This variable refers to the workspace this package is inside of.