From 2b75b79ea090c6fcb7ed97e5779418f09e287054 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 13:07:13 +0800 Subject: [PATCH] fix(run): import a Maven module by its artifactId before a same-named folder (#4696) IDEA names a Maven module after its artifactId. The JetBrains run-configuration importer accepted a folder name or an artifactId in one breadth-first search, so a project folder named like a child module's artifactId resolved to the root module. Search artifactIds first, then folder names, then the Gradle source-set fallback. --- crates/ide-run/src/import/jetbrains.rs | 8 ++++-- crates/ide-run/tests/import_jetbrains.rs | 32 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) 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