diff --git a/crates/ide-run/src/import/jetbrains.rs b/crates/ide-run/src/import/jetbrains.rs index cedca83a7..dd60a24fc 100644 --- a/crates/ide-run/src/import/jetbrains.rs +++ b/crates/ide-run/src/import/jetbrains.rs @@ -1112,8 +1112,12 @@ fn module_count(root: &Path) -> usize { /// named after its `artifactId` (usually its folder), a Gradle one `..`. fn module_dir(root: &Path, name: &str) -> Option { let folder = |dir: &Path| dir.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); - let exact = |dir: &Path| folder(dir) == name || pom_artifact(dir).is_some_and(|a| a == name); - if let Some(dir) = find_module(root, &exact) { + // A Maven module is named after its artifactId, so that match wins over a folder of the same + // name anywhere above it (a checkout folder named like a child module's artifactId). + if let Some(dir) = find_module(root, &|dir: &Path| pom_artifact(dir).is_some_and(|a| a == name)) { + return Some(dir); + } + if let Some(dir) = find_module(root, &|dir: &Path| folder(dir) == name) { return Some(dir); } let gradle = name.strip_suffix(".main").or_else(|| name.strip_suffix(".test")).unwrap_or(name); diff --git a/crates/ide-run/tests/import_jetbrains.rs b/crates/ide-run/tests/import_jetbrains.rs index f9e424ec3..d090f4b3d 100644 --- a/crates/ide-run/tests/import_jetbrains.rs +++ b/crates/ide-run/tests/import_jetbrains.rs @@ -860,6 +860,38 @@ fn java_applications_and_spring_boot_keep_their_module() { assert_eq!(reason(&r, "NoMain"), &SkipReason::NothingToRun); } +/// A checkout folder named like a child module's artifactId: IDEA's module is the child. +#[test] +fn maven_module_artifact_wins_over_a_folder_of_the_same_name() { + let dir = tempfile::tempdir().unwrap(); + let root = dunce::canonicalize(dir.path()).unwrap().join("demo-service"); + write( + &root, + "pom.xml", + "demo-service-parentpom", + ); + write( + &root, + "app-web/pom.xml", + "demo-service-parentdemo-service", + ); + write( + &root, + ".run/Demo.run.xml", + &run_file( + "Demo", + r#" + + "#, + ), + ); + + let r = import_from(&root, &[]); + + assert_eq!(get(&r, "Demo").extras.module.as_deref(), Some("app-web")); +} + /// IDEA writes the working directory as a URL. On Windows `file://$PROJECT_DIR$` /// expands to `file://C:/project`, where the drive letter sits where the authority /// belongs; read as a path that is `C:project`, which resolves against the project