Skip to content

Commit 78a7093

Browse files
committed
Fix x test --no-deps
- Use `cargo metadata` to determine whether a crate has a library package or not - Collect metadata for all workspaces, not just the root workspace and cargo - Don't pass `--lib` for crates without a library - Use `run_cargo_test` for rust-installer - Don't build documentation in `lint-docs` if `--no-doc` is passed
1 parent 2a75607 commit 78a7093

File tree

3 files changed

+58
-34
lines changed

3 files changed

+58
-34
lines changed

src/bootstrap/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ struct Crate {
246246
name: Interned<String>,
247247
deps: HashSet<Interned<String>>,
248248
path: PathBuf,
249+
has_lib: bool,
249250
}
250251

251252
impl Crate {

src/bootstrap/metadata.rs

+26-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde_derive::Deserialize;
55

66
use crate::cache::INTERNER;
77
use crate::util::output;
8-
use crate::{Build, Crate};
8+
use crate::{t, Build, Crate};
99

1010
/// For more information, see the output of
1111
/// <https://doc.rust-lang.org/nightly/cargo/commands/cargo-metadata.html>
@@ -22,6 +22,7 @@ struct Package {
2222
source: Option<String>,
2323
manifest_path: String,
2424
dependencies: Vec<Dependency>,
25+
targets: Vec<Target>,
2526
}
2627

2728
/// For more information, see the output of
@@ -32,6 +33,11 @@ struct Dependency {
3233
source: Option<String>,
3334
}
3435

36+
#[derive(Debug, Deserialize)]
37+
struct Target {
38+
kind: Vec<String>,
39+
}
40+
3541
/// Collects and stores package metadata of each workspace members into `build`,
3642
/// by executing `cargo metadata` commands.
3743
pub fn build(build: &mut Build) {
@@ -46,11 +52,16 @@ pub fn build(build: &mut Build) {
4652
.filter(|dep| dep.source.is_none())
4753
.map(|dep| INTERNER.intern_string(dep.name))
4854
.collect();
49-
let krate = Crate { name, deps, path };
55+
let has_lib = package.targets.iter().any(|t| t.kind.iter().any(|k| k == "lib"));
56+
let krate = Crate { name, deps, path, has_lib };
5057
let relative_path = krate.local_path(build);
5158
build.crates.insert(name, krate);
5259
let existing_path = build.crate_paths.insert(relative_path, name);
53-
assert!(existing_path.is_none(), "multiple crates with the same path");
60+
assert!(
61+
existing_path.is_none(),
62+
"multiple crates with the same path: {}",
63+
existing_path.unwrap()
64+
);
5465
}
5566
}
5667
}
@@ -60,29 +71,28 @@ pub fn build(build: &mut Build) {
6071
/// Note that `src/tools/cargo` is no longer a workspace member but we still
6172
/// treat it as one here, by invoking an additional `cargo metadata` command.
6273
fn workspace_members(build: &Build) -> impl Iterator<Item = Package> {
63-
let cmd_metadata = |manifest_path| {
74+
let collect_metadata = |manifest_path| {
6475
let mut cargo = Command::new(&build.initial_cargo);
6576
cargo
6677
.arg("metadata")
6778
.arg("--format-version")
6879
.arg("1")
6980
.arg("--no-deps")
7081
.arg("--manifest-path")
71-
.arg(manifest_path);
72-
cargo
82+
.arg(build.src.join(manifest_path));
83+
let metadata_output = output(&mut cargo);
84+
let Output { packages, .. } = t!(serde_json::from_str(&metadata_output));
85+
packages
7386
};
7487

75-
// Collects `metadata.packages` from the root workspace.
76-
let root_manifest_path = build.src.join("Cargo.toml");
77-
let root_output = output(&mut cmd_metadata(&root_manifest_path));
78-
let Output { packages, .. } = serde_json::from_str(&root_output).unwrap();
79-
80-
// Collects `metadata.packages` from src/tools/cargo separately.
81-
let cargo_manifest_path = build.src.join("src/tools/cargo/Cargo.toml");
82-
let cargo_output = output(&mut cmd_metadata(&cargo_manifest_path));
83-
let Output { packages: cargo_packages, .. } = serde_json::from_str(&cargo_output).unwrap();
88+
// Collects `metadata.packages` from all workspaces.
89+
let packages = collect_metadata("Cargo.toml");
90+
let cargo_packages = collect_metadata("src/tools/cargo/Cargo.toml");
91+
let ra_packages = collect_metadata("src/tools/rust-analyzer/Cargo.toml");
92+
let bootstrap_packages = collect_metadata("src/bootstrap/Cargo.toml");
8493

8594
// We only care about the root package from `src/tool/cargo` workspace.
8695
let cargo_package = cargo_packages.into_iter().find(|pkg| pkg.name == "cargo").into_iter();
87-
packages.into_iter().chain(cargo_package)
96+
97+
packages.into_iter().chain(cargo_package).chain(ra_packages).chain(bootstrap_packages)
8898
}

src/bootstrap/test.rs

+31-18
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ impl Step for CrateBootstrap {
103103
path,
104104
bootstrap_host,
105105
));
106-
run_cargo_test(cargo, &[], &[], compiler, bootstrap_host, builder);
106+
let crate_name = path.rsplit_once('/').unwrap().1;
107+
run_cargo_test(cargo, &[], &[], crate_name, compiler, bootstrap_host, builder);
107108
}
108109
}
109110

@@ -152,7 +153,11 @@ You can skip linkcheck with --exclude src/tools/linkchecker"
152153
SourceType::InTree,
153154
&[],
154155
);
155-
run_cargo_test(cargo, &[], &[], compiler, bootstrap_host, builder);
156+
run_cargo_test(cargo, &[], &[], "linkchecker", compiler, bootstrap_host, builder);
157+
158+
if builder.doc_tests == DocTests::No {
159+
return;
160+
}
156161

157162
// Build all the default documentation.
158163
builder.default_doc(&[]);
@@ -300,7 +305,7 @@ impl Step for Cargo {
300305
);
301306

302307
// NOTE: can't use `run_cargo_test` because we need to overwrite `PATH`
303-
let mut cargo = prepare_cargo_test(cargo, &[], &[], compiler, self.host, builder);
308+
let mut cargo = prepare_cargo_test(cargo, &[], &[], "cargo", compiler, self.host, builder);
304309

305310
// Don't run cross-compile tests, we may not have cross-compiled libstd libs
306311
// available.
@@ -368,7 +373,7 @@ impl Step for RustAnalyzer {
368373
cargo.env("SKIP_SLOW_TESTS", "1");
369374

370375
cargo.add_rustc_lib_path(builder, compiler);
371-
run_cargo_test(cargo, &[], &[], compiler, host, builder);
376+
run_cargo_test(cargo, &[], &[], "rust-analyzer", compiler, host, builder);
372377
}
373378
}
374379

@@ -417,7 +422,7 @@ impl Step for Rustfmt {
417422

418423
cargo.add_rustc_lib_path(builder, compiler);
419424

420-
run_cargo_test(cargo, &[], &[], compiler, host, builder);
425+
run_cargo_test(cargo, &[], &[], "rustfmt", compiler, host, builder);
421426
}
422427
}
423428

@@ -465,7 +470,7 @@ impl Step for RustDemangler {
465470
cargo.env("RUST_DEMANGLER_DRIVER_PATH", rust_demangler);
466471
cargo.add_rustc_lib_path(builder, compiler);
467472

468-
run_cargo_test(cargo, &[], &[], compiler, host, builder);
473+
run_cargo_test(cargo, &[], &[], "rust-demangler", compiler, host, builder);
469474
}
470475
}
471476

@@ -602,7 +607,7 @@ impl Step for Miri {
602607

603608
// This can NOT be `run_cargo_test` since the Miri test runner
604609
// does not understand the flags added by `add_flags_and_try_run_test`.
605-
let mut cargo = prepare_cargo_test(cargo, &[], &[], compiler, target, builder);
610+
let mut cargo = prepare_cargo_test(cargo, &[], &[], "miri", compiler, target, builder);
606611
{
607612
let _time = util::timeit(&builder);
608613
builder.run(&mut cargo);
@@ -679,7 +684,7 @@ impl Step for CompiletestTest {
679684
&[],
680685
);
681686
cargo.allow_features("test");
682-
run_cargo_test(cargo, &[], &[], compiler, host, builder);
687+
run_cargo_test(cargo, &[], &[], "compiletest", compiler, host, builder);
683688
}
684689
}
685690

@@ -722,17 +727,13 @@ impl Step for Clippy {
722727
&[],
723728
);
724729

725-
if !builder.fail_fast {
726-
cargo.arg("--no-fail-fast");
727-
}
728-
729730
cargo.env("RUSTC_TEST_SUITE", builder.rustc(compiler));
730731
cargo.env("RUSTC_LIB_PATH", builder.rustc_libdir(compiler));
731732
let host_libs = builder.stage_out(compiler, Mode::ToolRustc).join(builder.cargo_dir());
732733
cargo.env("HOST_LIBS", host_libs);
733734

734735
cargo.add_rustc_lib_path(builder, compiler);
735-
let mut cargo = prepare_cargo_test(cargo, &[], &[], compiler, host, builder);
736+
let mut cargo = prepare_cargo_test(cargo, &[], &[], "clippy", compiler, host, builder);
736737

737738
if builder.try_run(&mut cargo) {
738739
// The tests succeeded; nothing to do.
@@ -2048,11 +2049,13 @@ fn run_cargo_test(
20482049
cargo: impl Into<Command>,
20492050
libtest_args: &[&str],
20502051
crates: &[Interned<String>],
2052+
primary_crate: &str,
20512053
compiler: Compiler,
20522054
target: TargetSelection,
20532055
builder: &Builder<'_>,
20542056
) -> bool {
2055-
let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, compiler, target, builder);
2057+
let mut cargo =
2058+
prepare_cargo_test(cargo, libtest_args, crates, primary_crate, compiler, target, builder);
20562059
let _time = util::timeit(&builder);
20572060
add_flags_and_try_run_tests(builder, &mut cargo)
20582061
}
@@ -2062,6 +2065,7 @@ fn prepare_cargo_test(
20622065
cargo: impl Into<Command>,
20632066
libtest_args: &[&str],
20642067
crates: &[Interned<String>],
2068+
primary_crate: &str,
20652069
compiler: Compiler,
20662070
target: TargetSelection,
20672071
builder: &Builder<'_>,
@@ -2079,7 +2083,14 @@ fn prepare_cargo_test(
20792083
cargo.arg("--doc");
20802084
}
20812085
DocTests::No => {
2082-
cargo.args(&["--lib", "--bins", "--examples", "--tests", "--benches"]);
2086+
let krate = &builder
2087+
.crates
2088+
.get(&INTERNER.intern_str(primary_crate))
2089+
.unwrap_or_else(|| panic!("missing crate {primary_crate}"));
2090+
if krate.has_lib {
2091+
cargo.arg("--lib");
2092+
}
2093+
cargo.args(&["--bins", "--examples", "--tests", "--benches"]);
20832094
}
20842095
DocTests::Yes => {}
20852096
}
@@ -2191,7 +2202,7 @@ impl Step for Crate {
21912202
compiler.host,
21922203
target,
21932204
);
2194-
run_cargo_test(cargo, &[], &self.crates, compiler, target, builder);
2205+
run_cargo_test(cargo, &[], &self.crates, &self.crates[0], compiler, target, builder);
21952206
}
21962207
}
21972208

@@ -2284,6 +2295,7 @@ impl Step for CrateRustdoc {
22842295
cargo,
22852296
&[],
22862297
&[INTERNER.intern_str("rustdoc:0.0.0")],
2298+
"rustdoc",
22872299
compiler,
22882300
target,
22892301
builder,
@@ -2345,6 +2357,7 @@ impl Step for CrateRustdocJsonTypes {
23452357
cargo,
23462358
libtest_args,
23472359
&[INTERNER.intern_str("rustdoc-json-types")],
2360+
"rustdoc-json-types",
23482361
compiler,
23492362
target,
23502363
builder,
@@ -2504,7 +2517,7 @@ impl Step for Bootstrap {
25042517
}
25052518
// rustbuild tests are racy on directory creation so just run them one at a time.
25062519
// Since there's not many this shouldn't be a problem.
2507-
run_cargo_test(cmd, &["--test-threads=1"], &[], compiler, host, builder);
2520+
run_cargo_test(cmd, &["--test-threads=1"], &[], "bootstrap", compiler, host, builder);
25082521
}
25092522

25102523
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
@@ -2617,7 +2630,7 @@ impl Step for RustInstaller {
26172630
SourceType::InTree,
26182631
&[],
26192632
);
2620-
try_run(builder, &mut cargo.into());
2633+
run_cargo_test(cargo, &[], &[], "installer", compiler, bootstrap_host, builder);
26212634

26222635
// We currently don't support running the test.sh script outside linux(?) environments.
26232636
// Eventually this should likely migrate to #[test]s in rust-installer proper rather than a

0 commit comments

Comments
 (0)