-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Environment variable for Cargo Workspace #3946
Comments
I'd expect the environment variable to not be set at all in that case. |
What would adding the CARGO_WORKSPACE entail. From cursory look, I see custom_build.rs has reference to Context, that has reference to Workspace, but same doesn't exist for compilation.rs. Would having CARGO_WORKSPACE only for custom builds be ok? |
Thanks for the report! I think I may not quite be following what's going on here though? Do you mean accessing the workspace directory from a build script perhaps? |
@alexcrichton Yes. I was looking for workspace directory in my custom build script. It's related to servo/html5ever#261. There is a simple workaround of taking |
Oh yeah definitely makes sense to me! Seems reasonable to basically enhance this section |
So if I understand correctly, if I expose workspace.root_manifest that would be workspace dir of all projects in workspace, correct? Then I can just: if let Some(workspace_dir) = cx.ws.root_manifest() {
cmd.env("CARGO_WORKSPACE_DIR", workspace_dir);
} |
sounds about right! I think you may not want precisely the |
@alexcrichton I am total newb, but won't adding |
oh sure yeah, it just depends on the intent of what's being conveyed (the workspace manifest or the directory of the workspace), I'm fine with either. |
Hm, while writing tests, I've noticed a peculiarity. I assume I'm using this wrong. But I wanted to double check let p = project("foo")
.file("Cargo.toml", r#"
[project]
name = "foo"
version = "0.5.0"
authors = []
[workspace]
members = ["a"]
"#)
.file("src/lib.rs", "")
.file("build.rs", r#"
fn main() {
//panic!("WILL FAIL");
}
"#)
.file("a/Cargo.toml", r#"
[project]
name = "a"
version = "0.5.0"
authors = []
links = "foo"
build = "build.rs"
"#)
.file("a/src/lib.rs", "")
.file("a/build.rs", r#"
fn main() {
panic!("PASSES?");
}
"#);
assert_that(p.cargo_process("build").arg("-v"),
execs().with_status(0)); The panic in Idea behind tests was to verify that each member |
@Ygg01 oh |
@alexcrichton Is there an alternative way to test env. variables are properly set in each member build script? |
@Ygg01 I think you'd just |
Yes, I think that is correct (one call to |
Oh you'll just want to call |
This was assigned to me to summarize why we didn't merge my PR which would have closed it #4787. We decided to punt on this feature because of the question about what happens when building a crate downloaded from crates.io, which is no longer in a workspace in that form, but might have been originally produced in a workspace and have a build script that expects to have this env var set. Its also unclear what the motivation for this variable is; my motivation was to find the lockfile, but I concluded that the best way to get the information I was getting from the lockfile was to run |
One motivation would be to find the absolute path of the resulting binary executable. How I'm currently doing it.
hey that's pretty cool: $ pwd
/home/xftroxgpx/build/2nonpkgs/rust.stuff/rustlearnage/recompile_self
$ time cargo metadata --format-version 1 | json_reformat | grep workspace_root
"workspace_root": "/home/xftroxgpx/build/2nonpkgs/rust.stuff/rustlearnage"
real 0m1.054s
user 0m0.847s
sys 0m0.214s |
@withoutboats my original motivation for this feature was when html5ever, was moving from one project per workspace to multiple. Namely some tests that were specific, became shared and not in the same directory they were left. However fact that almost no one needed this feature, and it was easily implementable by other means, kinda made me think it's not needed. I did forgot about it completely. |
I also have another use case for this feature: I'm trying to get the absolute path to the source file being compiled. I embed this as metadata from a procedural macro invocation so that source can be copied during a subsequent When using a workspace, the Of course, there could easily be a better way to get the source file's absolute path that I'm completely ignorant of, so please let me know if that's the case. Thanks! |
I also ran into this problem where |
This is the workaround i have in place now which is pretty ugly: https://github.com/mitsuhiko/insta/blob/b113499249584cb650150d2d01ed96ee66db6b30/src/runtime.rs#L67-L88 |
This provides what cargo sets as the `current_dir` for the `rustc` process. While `std::file!` is unspecified in what it is relative to, it is relatively safe, it is generally relative to `rustc`s `current_dir`. This can be useful for snapshot testing. For example, `snapbox` has been using this macro on nightly since assert-rs/snapbox#247, falling back to finding a parent of `CARGO_MANIFEST_DIR`, if present. This has been in use in Cargo since rust-lang#13441. This was added in rust-lang#12996. Relevant points discussed in that issue: - This diverged from the original proposal from the Cargo team of having a `CARGO_WORKSPACE_DIR` that is the "workspace" of the package being built (ie registry packages would map to `CARGO_MANIFEST_DIR`). In looking at the `std::file!` use case, `CARGO_MANIFEST_DIR`, no matter how we defined it, would only sort of work because no sane definition of that maps to `rustc`'s `current_dir`.a This instead focuses on the mechanism currently being used. - Using "current dir" in the name is meant to be consistent with `std::env::current_dir`. - I can go either way on `CARGO_RUSTC` vs `RUSTC`. Existing related variables: - `RUSTC` - `RUSTC_WRAPPER` - `RUSTC_WORKSPACE_WRAPPER` - `RUSTFLAGS` (no `C`) - `CARGO_CACHE_RUSTC_INFO` Note that rust-lang#3946 was overly broad and covered many use cases. One of those was for packages to look up information on their dependents. Issue rust-lang#13484 is being left open to track that. Fixes rust-lang#3946
Another usecase for this is the EDIT: It looks like the |
My use case is put data for testing in the root of our workspace so I need a simple way to do |
@Stargateur atm Cargo generally encourages a package to be self-contained, assuming it will be published to crates.io and can be tested from there (like for crater). I think it would be interesting to evaluate what would be different in Cargo without the published assumption and how we reconcile Cargo functioning under the two workflows. |
I have no strong opinion about what cargo should encourage, I guess focus on "a package should be self contained for published crate" make sense, BUT workspace is not at all about that, for example Thus I happy enough with the trick of .cargo/config |
That is different as Cargo has enough information to normalize this on publish. |
Use case: use std::path::Path;
#[track_caller]
fn print_env(){
let manifest_dir = env!("CARGO_MANIFEST_DIR");
println!("MANIFEST_DIR: {:?}", manifest_dir);
let caller_file = std::panic::Location::caller().file();
println!("CALLER FILE: {:?}", caller_file);
let root_dir = Path::new(manifest_dir);
let this_file = root_dir.join(caller_file);
println!("RESULT: {:?}", this_file);
}
fn main(){
print_env()
} MANIFEST_DIR: "/playground"
CALLER FILE: "src/main.rs"
RESULT: "/playground/src/main.rs" |
@bukowa Making |
Turns out Cargo doesn't like when materials required to build a crate are outside of the crate manifest directory. In this case, I'm talking about the protobuf definition used for the Hipcheck plugin gRPC service. There's a Cargo issue open about it, but the gist of it is that they don't really want people doing what we were trying to do, having materials in an outer directory. There may be better solutions, but moving the files is the fastest one for now. Here's the issue: rust-lang/cargo#3946 Signed-off-by: Andrew Lilley Brinker <[email protected]>
Turns out Cargo doesn't like when materials required to build a crate are outside of the crate manifest directory. In this case, I'm talking about the protobuf definition used for the Hipcheck plugin gRPC service. There's a Cargo issue open about it, but the gist of it is that they don't really want people doing what we were trying to do, having materials in an outer directory. There may be better solutions, but moving the files is the fastest one for now. Here's the issue: rust-lang/cargo#3946 Signed-off-by: Andrew Lilley Brinker <[email protected]>
From #13644
As we work towards that, we'll need to remove Another thought I had was that we could change |
Created the ACP for |
No idea how this issue deviated from getting the workspace directory, which for example is very useful for Bevy's asset directory (i.e. |
@teohhanhui there are multiple use cases discussed in this thread. A clear cut use case is being able to figure out the runtime path to a source file for snapshot testing and other workflows (#3946 (comment), #3946 (comment), #3946 (comment)). That is what That effort wouldn't close out this issue on its own as there are also other use cases covered here. For the ones that literally need the workspace root, the situation is less clear cut for moving forward atm:
|
* chore: update flake deps and bump rust nightly version * refactor: add explicit lifetime names * refactor: replace removed `CARGO_RUSTC_CURRENT_DIR` ref: rust-lang/cargo#3946 * refactor: switch naked fns to `naked_asm!` Switch to the newly required `naked_asm!` macro in all naked functions. ref: rust-lang/rust#90957 * fix: update custom target manifest files to avoid ICE apparently Rust doesn't like it when the custom target manifest file is missing the `llvm-abiname` field * silence warning about mutable static * refactor: elide lifetimes * fmt * refactor: apply `clippy::manual_div_ceil` suggestion * fix: use explicit `ptr.wrapping_byte_add` for conversions from physmem to virtmem references We previously relied on UB actually doing the wrapping behaviour, but well, that's not ideal is it
This takes the current behavior of `file!` and documents it so it is safe to make assumptions about. For example, Cargo could provide a `CARGO_RUSTC_CURRENT_DIR` as a base path for `file!`. Example use cases - Being able to look up test assets relative to the current file ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/cargo_add/add_basic/mod.rs#L34)) - Inline snapshotting libraries being able to update Rust source code ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/alt_registry.rs#L36-L45)) See rust-lang/cargo#3946 for more context. T-libs-api discussed two solutions in rust-lang/libs-team#478 - `file_absolute!`: - Has less meaning in other build tools like buck2 - Bakes in the assumption that a full path is available (e.g. with trim-paths) - Specifying `file!`s behavior (this PR): - Leaves it to the user to deal with trim-paths - Even though `file!` is currently unspecified, changing it would likely have too large of an impact on the ecosystem at this time. A future possibility is that rustc could have a flag that controls modifies the base path used for `file!`. That seems purely additive with specifying the behavior and we do not want to block on it. It would also likely be too disruptive for Cargo users (as mentioned). However, we tried to keep this in mind when specifying the behavior.
This takes the current behavior of `file!` and documents it so it is safe to make assumptions about. For example, Cargo could provide a `CARGO_RUSTC_CURRENT_DIR` as a base path for `file!`. Example use cases - Being able to look up test assets relative to the current file ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/cargo_add/add_basic/mod.rs#L34)) - Inline snapshotting libraries being able to update Rust source code ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/alt_registry.rs#L36-L45)) See rust-lang/cargo#3946 for more context. T-libs-api discussed two solutions in rust-lang/libs-team#478 - `file_absolute!`: - Has less meaning in other build tools like buck2 - Bakes in the assumption that a full path is available (e.g. with trim-paths) - Specifying `file!`s behavior (this PR): - Leaves it to the user to deal with trim-paths - Even though `file!` is currently unspecified, changing it would likely have too large of an impact on the ecosystem at this time. A future possibility is that rustc could have a flag that controls modifies the base path used for `file!`. That seems purely additive with specifying the behavior and we do not want to block on it. It would also likely be too disruptive for Cargo users (as mentioned). However, we tried to keep this in mind when specifying the behavior.
This takes the current behavior of `file!` and documents it so it is safe to make assumptions about. For example, Cargo could provide a `CARGO_RUSTC_CURRENT_DIR` as a base path for `file!`. Example use cases - Being able to look up test assets relative to the current file ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/cargo_add/add_basic/mod.rs#L34)) - Inline snapshotting libraries being able to update Rust source code ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/alt_registry.rs#L36-L45)) See rust-lang/cargo#3946 for more context. T-libs-api discussed two solutions in rust-lang/libs-team#478 - `file_absolute!`: - Has less meaning in other build tools like buck2 - Bakes in the assumption that a full path is available (e.g. with trim-paths) - Specifying `file!`s behavior (this PR): - Leaves it to the user to deal with trim-paths - Even though `file!` is currently unspecified, changing it would likely have too large of an impact on the ecosystem at this time. A future possibility is that rustc could have a flag that controls modifies the base path used for `file!`. That seems purely additive with specifying the behavior and we do not want to block on it. It would also likely be too disruptive for Cargo users (as mentioned). However, we tried to keep this in mind when specifying the behavior.
This takes the current behavior of `file!` and documents it so it is safe to make assumptions about. For example, Cargo could provide a `CARGO_RUSTC_CURRENT_DIR` as a base path for `file!`. Example use cases - Being able to look up test assets relative to the current file ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/cargo_add/add_basic/mod.rs#L34)) - Inline snapshotting libraries being able to update Rust source code ([example](https://github.com/rust-lang/cargo/blob/b9026bf654d7fac283465e58b8b76742244ef07d/tests/testsuite/alt_registry.rs#L36-L45)) See rust-lang/cargo#3946 for more context. T-libs-api discussed two solutions in rust-lang/libs-team#478 - `file_absolute!`: - Has less meaning in other build tools like buck2 - Bakes in the assumption that a full path is available (e.g. with trim-paths) - Specifying `file!`s behavior (this PR): - Leaves it to the user to deal with trim-paths - Even though `file!` is currently unspecified, changing it would likely have too large of an impact on the ecosystem at this time. A future possibility is that rustc could have a flag that controls modifies the base path used for `file!`. That seems purely additive with specifying the behavior and we do not want to block on it. It would also likely be too disruptive for Cargo users (as mentioned). However, we tried to keep this in mind when specifying the behavior.
T-cargo notes:
A
CARGO_RUSTC_CURRENT_DIR
is added as a nightly only environment variable. See #3946 (comment). Seek for feedback.Hi, while working on using workspace in html5ever, I've ran into issue of needing the
CARGO_WORKSPACE
directory, and being unable, to find it. What I resorted to is essentially,&Path(cargo_manifest).join("..")
which feels hacky.Could
CARGO_WORKSPACE
be added as environment variable? I'm not sure what it should be when there is no workspace defined, I assume it should either returnErr
or default it toCARGO_MANIFEST_DIR
.Sidenote I'm willing to work on this issue, if I could get quick pointers, to what I need to do.
The text was updated successfully, but these errors were encountered: