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

Add CARGO_WORKSPACE_DIR env var for integration tests and benchmarks #12158

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,22 @@ fn prepare_rustc(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuilde
if unit.target.is_test() || unit.target.is_bench() {
let tmp = cx.files().layout(unit.kind).prepare_tmp()?;
base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());

if cx.bcx.config.nightly_features_allowed {
// Use a relative path and limit to integration tests and benchmarks in hopes
// that it conveys to the user that the meaning of this value is a bit fuzzy
// (very different meaning in the original repo vs once published).
let cargo_workspace_dir = if is_primary && is_workspace {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does is_primary work?

This environment variable will be set if the package being built is primary. Primary packages are the ones the user selected on the command-line, either with -p flags or the defaults based on the current directory and the default workspace members. This environment variable will not be set when building dependencies. This is only set when compiling the package (not when running binaries or tests).

  • I can run cargo test -p <transitve-dep>. This would make it primary but not something we should give my workspace dir to
  • I'm unsure if there are cases for local use where something isn't primary but is part of my workspace. I'm guessing not since this is for top-level tests but if we relied on this, its indirect and we'd want to document it.

pathdiff::diff_paths(cx.bcx.ws.root(), unit.pkg.root())
.expect("both paths are absolute")
.display()
.to_string()
} else {
// path from unit.pkg.root() to unit.pkg.root()
".".to_string()
};
base.env("CARGO_WORKSPACE_DIR", cargo_workspace_dir);
}
}

base.inherit_jobserver(&cx.jobserver);
Expand Down
5 changes: 5 additions & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ corresponding environment variable is set to the empty string, `""`.
where integration tests or benchmarks are free to put any data needed by
the tests/benches. Cargo initially creates this directory but doesn't
manage its content in any way, this is the responsibility of the test code.
* `CARGO_WORKSPACE_DIR` --- Only set when building [integration test] or benchmark code.
This is a path to the workspace directory of the package being built. When there is
no associated workspace, which includes all registry packages, this will be a path to
the directory that contains the manifest of the package.
Currently, it is implemented as a relative path.

[Cargo target]: cargo-targets.md
[binaries]: cargo-targets.md#binaries
Expand Down
Loading
Loading