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

Adding Workspace DIR functionality #4073

Closed
wants to merge 2 commits into from
Closed

Conversation

Ygg01
Copy link

@Ygg01 Ygg01 commented May 19, 2017

This is the basic code I did to get #3946 fixed.

The functionality is about allowing build.rs custom build script to find Workspace directory, so it could extract some data, etc. It's possible, it makes sense for this environment variable to be present even during regular cargo build.

Workspace directory variable is called CARGO_WORKSPACE_DIR and points to workspace directory (one with Cargo.toml that declares members), regardless of which crate from workspace called it. If the crate built doesn't have a workspace there will be no value in CARGO_WORKSPACE_DIR, i.e. env!("CARGO_WORKSPACE_DIR") will be Err(NotPresent).

Ygg01 added 2 commits May 15, 2017 07:14
- Adds ability to get directory containing Workspace manifest
- Adds test for the functionality
@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @alexcrichton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

/// That is, this returns the path of the directory containing the
/// `Cargo.toml` which is the root of this workspace.
pub fn workspace_dir(&self) -> Option<&Path> {
match self.root_manifest {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton I know you said use root, but I wanted it to be option so I can check if the crate has a workspace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should always be available, right? In that this function should return &Path instead of Option<&Path>? (sort of how root above cannot fail)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought @shepmaster said in #3946, that it's best it's not present if it's not a cargo workspace project?

Copy link
Member

@alexcrichton alexcrichton May 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm so in some sense all crates are a member of a workspace regardless of configuration, it's just that some workspaces only contain one member. My feeling is that code wishing to use this would basically othrewise be getting this env var unwarp_or CARGO_MANIFEST_DIR.

@shepmaster mind weighing in though? Do you have thoughts on what you'd do if a build script isn't in a workspace?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, there doesn't seem to be enough interest for this feature. I managed to bypass it for my needs.

I am willing to work on it this weekend.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's switch this to returning &Path and always exporting it to the build script?

use std::env;

fn main() {
assert!(env::var("CARGO_WORKSPACE_DIR").unwrap().ends_with("foo"));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to test it is looking at correct folder?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you could set a different env var with the expected value as part of p.cargo below perhaps?

Copy link
Author

@Ygg01 Ygg01 May 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any examples of that in tests? Ideally, I want to say, set this to be wherever the parent dir of this file is.

EDIT: Clarification, CARGO_WORKSPACE_DIR should point to folder where Cargo.toml that defines members is located (where applicable).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you'd just do something like p.cargo("foo").env("EXPECTED_DIR", "...")

Copy link
Author

@Ygg01 Ygg01 May 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was more asking, how to get "..." to point to correct folder. As far as I understand, cargotest creates temp files ?

EDIT: Would this https://github.com/rust-lang/cargo/blob/master/tests/cargotest/support/paths.rs#L49

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh p.root() points to the root path of the project, and you can use that as a PathBuf to calculate the correct value to pass in

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to resubmit with a rebase!

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

Successfully merging this pull request may close these issues.

3 participants