diff --git a/crates/testkit/src/guards.rs b/crates/testkit/src/guards.rs index 95ec54ed75..cb90fd38ab 100644 --- a/crates/testkit/src/guards.rs +++ b/crates/testkit/src/guards.rs @@ -1517,7 +1517,7 @@ fn extern_crates<'a>(toks: &'a [Token], never: &[bool]) -> Vec<(bool, &'a str, u /// to the crate's extern prelude, so that a path starting with `::m` names muda anywhere in the crate. /// /// The crate roots follow Cargo's layout: `src/lib.rs` and `src/main.rs`, except one below another package's -/// `src` folder that a declaration names; each file that [`cargo_target`] +/// `src` folder that a declaration outside its own package folder names; each file that [`cargo_target`] /// accepts; another `build.rs`, `lib.rs` or `main.rs` when no `mod` declaration in another of the files names /// it, plainly or through a `#[path]` attribute; and each other file directly in a `bin`, `examples`, `tests` /// or `benches` folder other than `mod.rs` when no package folder holds it and no declaration without a @@ -1553,6 +1553,9 @@ fn extern_prelude_names(paths: &[String], contents: &[&str]) -> Vec> = (0..paths.len() * 2) .map(|node| { let (file, path_loaded) = (node / 2, node % 2 == 1); @@ -1564,6 +1567,13 @@ fn extern_prelude_names(paths: &[String], contents: &[&str]) -> Vec Vec !(named[file] && in_package_src(&paths[file])), + (Some("lib.rs" | "main.rs"), Some("src")) => !(named_from_outside[file] && in_package_src(&paths[file])), (Some("lib.rs" | "main.rs" | "build.rs"), _) => !named[file], // In a known package, `cargo_target` has already placed the target folders. (Some(name), Some("bin" | "examples" | "tests" | "benches")) => { diff --git a/crates/testkit/tests/it/guards.rs b/crates/testkit/tests/it/guards.rs index 03620ab8a4..0262d28bb8 100644 --- a/crates/testkit/tests/it/guards.rs +++ b/crates/testkit/tests/it/guards.rs @@ -720,6 +720,23 @@ fn predefined_quit_reads_target_folders_below_the_package_folder_only() { let lib = "extern crate other_menu as menu;\n#[path = \"nested/src/lib.rs\"]\nmod nested;\n"; let found = predefined_quit([("src/lib.rs", lib), ("src/nested/src/lib.rs", local)]); assert!(found.is_empty(), "{found:?}"); + // A nested package's library that only its own binary loads stays a crate root, so its alias reaches its modules. + let nested = "extern crate muda as menu;\nmod child;\n"; + let found = predefined_quit([ + ("src/lib.rs", ""), + ("src/nested/src/lib.rs", nested), + ("src/nested/src/child.rs", "fn f() { ::menu::PredefinedMenuItem::quit(None); }\n"), + ("src/nested/src/bin/tool.rs", "#[path = \"../lib.rs\"]\nmod lib;\n"), + ]); + assert_eq!(found.iter().map(|f| (f.path.as_str(), f.line)).collect::>(), vec![("src/nested/src/child.rs", 1)]); + // One that a module beside its `src` folder loads is a module of the outer crate. + let found = predefined_quit([ + ("src/lib.rs", "mod nested;\n"), + ("src/nested/mod.rs", "#[path = \"src/lib.rs\"]\nmod inner;\n"), + ("src/nested/src/lib.rs", nested), + ("src/nested/src/child.rs", "fn f() { ::menu::PredefinedMenuItem::quit(None); }\n"), + ]); + assert!(found.is_empty(), "{found:?}"); } #[test]