Skip to content

Commit 98098df

Browse files
test: Prepare tests for new Cargo build-dir layout (rust-lang#16966)
This PR prepares the clippy testsuite to work with the new Cargo `build-dir` layout tracked in rust-lang/cargo#16807. Context: In rust-lang/rust#155439 we attempt to enable the new Cargo `build-dir` layout in rust-lang/rust and I am working through all of the build failures caused by changing where the files are. Also note that the `ui_test` crate was bumped to include the corresponding fix in oli-obk/ui_test#368 With the changes in this PR the following test passes on my system. ```sh CARGO_UNSTABLE_BUILD_DIR_NEW_LAYOUT=true ./x test --stage 2 src/tools/clippy ``` changelog: none
2 parents 6310134 + ce93eb0 commit 98098df

2 files changed

Lines changed: 58 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ anstream = "0.6.18"
3434

3535
[dev-dependencies]
3636
cargo_metadata = "0.23"
37-
ui_test = "0.30.2"
37+
ui_test = "0.30.5"
3838
regex = "1.5.5"
3939
serde = { version = "1.0.145", features = ["derive"] }
4040
serde_json = "1.0.122"

tests/compile-test.rs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,33 @@ fn internal_extern_flags() -> Vec<String> {
8989
help: Try adding to dev-dependencies in Cargo.toml\n\
9090
help: Be sure to also add `extern crate ...;` to tests/compile-test.rs",
9191
);
92-
crates
92+
93+
let mut args: Vec<String> = crates
9394
.into_iter()
9495
.map(|(name, path)| format!("--extern={name}={path}"))
95-
.chain([format!("-Ldependency={}", deps_path.display())])
96-
.collect()
96+
.collect();
97+
98+
if deps_path.ends_with("deps") {
99+
args.push(format!("-Ldependency={}", deps_path.display()));
100+
} else {
101+
// If the dep_path does not point to `/deps` it very likely means Cargo is using the v2 build-dir
102+
// layout
103+
assert!(deps_path.ends_with("out"));
104+
105+
// Get a path to `target/<platform-profile>/build`
106+
let build_dir = {
107+
let mut d = deps_path.to_path_buf();
108+
d.pop(); // remove `out`
109+
d.pop(); // remove `<hash>`
110+
d.pop(); // remove `<pkgname>`
111+
d
112+
};
113+
114+
let out_dirs = discover_out_dirs(&build_dir);
115+
args.extend(out_dirs.iter().map(|path| format!("-Ldependency={}", path.display())));
116+
}
117+
118+
args
97119
}
98120

99121
// whether to run internal tests or not
@@ -214,8 +236,21 @@ impl TestContext {
214236
config.program.envs.push(("RUSTC_ICE".into(), Some("0".into())));
215237

216238
if let Some(host_libs) = option_env!("HOST_LIBS") {
217-
let dep = format!("-Ldependency={}", Path::new(host_libs).join("deps").display());
218-
config.program.args.push(dep.into());
239+
let deps_dir = Path::new(host_libs).join("deps");
240+
241+
if deps_dir.exists() {
242+
let dep = format!("-Ldependency={}", deps_dir.display());
243+
config.program.args.push(dep.into());
244+
} else {
245+
// If `/deps` does not exist, assume Cargo v2 build-dir layout
246+
let build_dir = Path::new(host_libs).join("build");
247+
let dependencies = discover_out_dirs(&build_dir);
248+
249+
for dep in dependencies {
250+
let dep = format!("-Ldependency={}", dep.display());
251+
config.program.args.push(dep.into());
252+
}
253+
}
219254
}
220255
if let Some(sysroot) = option_env!("TEST_SYSROOT") {
221256
config.program.args.push(format!("--sysroot={sysroot}").into());
@@ -648,3 +683,20 @@ impl LintMetadata {
648683
}
649684
}
650685
}
686+
687+
/// Gets all of the `out` dirs in a given Cargo `build-dir/<profile>/build` dir.
688+
fn discover_out_dirs(dir: &Path) -> Vec<PathBuf> {
689+
if !dir.exists() {
690+
return Vec::new();
691+
}
692+
693+
let read_dir = |path: &Path| path.read_dir().ok().into_iter().flatten().filter_map(Result::ok);
694+
dir.read_dir()
695+
.unwrap_or_else(|e| panic!("Couldn't read {}: {}", dir.display(), e))
696+
.map(|e| e.unwrap())
697+
.flat_map(|e| read_dir(&e.path()))
698+
.flat_map(|e| read_dir(&e.path()))
699+
.map(|e| e.path())
700+
.filter(|path| path.ends_with("out"))
701+
.collect::<Vec<_>>()
702+
}

0 commit comments

Comments
 (0)