From d6b7a5bee8cc76aec22aadd6bef840b12493d0bd Mon Sep 17 00:00:00 2001 From: FredApps Date: Fri, 20 Nov 2026 03:34:46 +0100 Subject: [PATCH] Follow nested modules beside explicit registry paths --- crates/ui-macros/src/registry.rs | 30 +++++++++++-------- .../tests/fixtures/registry/child.rs | 5 ++++ .../tests/fixtures/registry/nested.rs | 2 ++ .../tests/fixtures/registry/other.rs | 2 ++ 4 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 crates/ui-macros/tests/fixtures/registry/nested.rs diff --git a/crates/ui-macros/src/registry.rs b/crates/ui-macros/src/registry.rs index 5dc543577..a265949e2 100644 --- a/crates/ui-macros/src/registry.rs +++ b/crates/ui-macros/src/registry.rs @@ -88,7 +88,7 @@ fn contains_suite_item(items: &[Item]) -> bool { }) } -fn module_file(dir: &Path, path_dir: &Path, module: &ItemMod) -> Option { +fn explicit_path(path_dir: &Path, module: &ItemMod) -> Option { for attr in &module.attrs { if attr.path().is_ident("path") && let Meta::NameValue(value) = &attr.meta @@ -98,11 +98,15 @@ fn module_file(dir: &Path, path_dir: &Path, module: &ItemMod) -> Option return Some(path_dir.join(path.value())); } } - let plain = dir.join(format!("{}.rs", module.ident)); + None +} + +fn module_file(module_dir: &Path) -> Option { + let plain = module_dir.with_extension("rs"); if plain.is_file() { Some(plain) } else { - let nested = dir.join(module.ident.to_string()).join("mod.rs"); + let nested = module_dir.join("mod.rs"); nested.is_file().then_some(nested) } } @@ -129,21 +133,21 @@ fn walk( cfg.extend(attrs_cfg(&module.attrs)); let mut child_path = path.to_vec(); child_path.push(module.ident.clone()); + let module_dir = dir.join(module.ident.to_string()); + let explicit = explicit_path(path_dir, module); if let Some((_, content)) = &module.content { - let inline_dir = dir.join(module.ident.to_string()); - walk(content, file, &inline_dir, &inline_dir, &child_path, &cfg, calls)?; - } else if let Some(file) = module_file(dir, path_dir, module) { + let inline_dir = explicit.as_deref().unwrap_or(&module_dir); + walk(content, file, inline_dir, inline_dir, &child_path, &cfg, calls)?; + } else if let Some(file) = explicit.clone().or_else(|| module_file(&module_dir)) { let text = std::fs::read_to_string(&file) .map_err(|e| syn::Error::new_spanned(module, format!("cannot read {}: {e}", file.display())))?; let parsed = syn::parse_file(&text).map_err(|e| syn::Error::new_spanned(module, format!("cannot parse {}: {e}", file.display())))?; cfg.extend(attrs_cfg(&parsed.attrs)); - let child_dir = if file.file_name().is_some_and(|n| n == "mod.rs" || n == "main.rs" || n == "lib.rs") { - file.parent().unwrap_or(dir).to_path_buf() - } else { - file.parent().unwrap_or(dir).join(file.file_stem().unwrap_or_default()) - }; - walk(&parsed.items, &file, &child_dir, file.parent().unwrap_or(dir), &child_path, &cfg, calls)?; + let file_dir = file.parent().unwrap_or(dir); + // Rust looks beside a file selected by #[path] for that file's own modules. + let child_dir = if explicit.is_some() { file_dir } else { &module_dir }; + walk(&parsed.items, &file, child_dir, file_dir, &child_path, &cfg, calls)?; } } Item::Struct(item) @@ -257,6 +261,8 @@ mod tests { assert!(generated.contains("__registry_story_view")); assert!(generated.contains("__registry_trial_changes")); assert!(generated.contains("__registry_trial_path_trial")); + assert!(generated.contains("crate :: child :: other :: nested :: __registry_trial_nested_path_trial")); + assert!(generated.contains("crate :: child :: inline_alias :: nested :: __registry_trial_nested_path_trial")); assert!(generated.contains("cfg (not (target_arch = \"wasm32\"))")); assert!(!generated.contains("# !")); } diff --git a/crates/ui-macros/tests/fixtures/registry/child.rs b/crates/ui-macros/tests/fixtures/registry/child.rs index 832cf8638..2e2525059 100644 --- a/crates/ui-macros/tests/fixtures/registry/child.rs +++ b/crates/ui-macros/tests/fixtures/registry/child.rs @@ -7,3 +7,8 @@ fn view() {} fn changes() {} #[path = "other.rs"] mod other; + +#[path = "."] +mod inline_alias { + mod nested; +} diff --git a/crates/ui-macros/tests/fixtures/registry/nested.rs b/crates/ui-macros/tests/fixtures/registry/nested.rs new file mode 100644 index 000000000..5b4dd1385 --- /dev/null +++ b/crates/ui-macros/tests/fixtures/registry/nested.rs @@ -0,0 +1,2 @@ +#[trial] +fn nested_path_trial() {} diff --git a/crates/ui-macros/tests/fixtures/registry/other.rs b/crates/ui-macros/tests/fixtures/registry/other.rs index 1887f4901..5d05daba5 100644 --- a/crates/ui-macros/tests/fixtures/registry/other.rs +++ b/crates/ui-macros/tests/fixtures/registry/other.rs @@ -1,2 +1,4 @@ #[trial] fn path_trial() {} + +mod nested;