From 40b95650cf2a12b372d53ff2ea7c3c2e635f0d4a Mon Sep 17 00:00:00 2001 From: Mollo-i <76565859+Greenker@users.noreply.github.com> Date: Fri, 20 Nov 2026 10:08:09 +0800 Subject: [PATCH] fix(maven): custom project.* properties and bare built-in shorthands resolve to their definition (#4651) Every project.* name was treated as a Maven built-in, so custom properties such as project.port never navigated, and the bare built-in shorthand (@artifactId@) did not either. Non-coordinate project.* names now fall through to the lookup along the parent chain, and the bare shorthand resolves to the pom's own coordinate unless a custom property of that name exists. Verified with cargo test -p ide-build (maven tests pass; the one failing Go workspace test is unrelated to this change and fails from the local go toolchain) and clippy. Co-authored-by: Greenker <76565859+Greenker@users.noreply.github.com> --- crates/ide-build/src/maven.rs | 99 +++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/crates/ide-build/src/maven.rs b/crates/ide-build/src/maven.rs index 580f40813..e4817e371 100644 --- a/crates/ide-build/src/maven.rs +++ b/crates/ide-build/src/maven.rs @@ -2020,11 +2020,25 @@ fn property_target(text: &str, path: &Path, name: &str, local_repo: Option` lookup below resolves. + let built_in = match name.strip_prefix("project.") { + Some(key) if matches!(key, "groupId" | "artifactId" | "version" | "packaging" | "name") => Some(key), + // `project.basedir`, `project.build.directory`… have no element of their own. + Some(_) => None, + None if matches!(name.as_str(), "groupId" | "artifactId" | "version" | "packaging" | "name") => { + // The bare shorthand of a built-in; a custom property of that name wins. + if let Some(found) = properties_target(&chain, &name, &resolver) { + return Some(found); + } + Some(name.as_str()) } + None => None, + }; + if let Some(key) = built_in { for (p, text) in &chain { let Some(root) = xml::parse(text).filter(|e| e.name == "project") else { continue }; let Some(e) = root.child(key).filter(|e| !e.text.trim().is_empty()) else { continue }; @@ -2037,30 +2051,34 @@ fn property_target(text: &str, path: &Path, name: &str, local_repo: Option`: the first POM of the chain (child-first) that - // declares it, at project level or in an active profile. - for (p, text) in &chain { + properties_target(&chain, &name, &resolver) +} + +/// A property of ``: the first POM of the chain (child-first) that +/// declares it, at project level or in an active profile. +fn properties_target(chain: &[(PathBuf, String)], name: &str, resolver: &Resolver) -> Option { + for (p, text) in chain { let Some(root) = xml::parse(text).filter(|e| e.name == "project") else { continue }; let active: Vec = parse_pom(text) .map(|pom| resolver.active(&pom.profiles).map(|pr| pr.id.clone()).collect()) .unwrap_or_default(); // Project level first, then the first active profile declaring the property. - let e = root.child("properties").and_then(|props| props.child(&name)).or_else(|| { + let e = root.child("properties").and_then(|props| props.child(name)).or_else(|| { root.list("profiles", "profile") .into_iter() .find(|profile| { active.contains(&profile.child("id").map(|id| id.text.trim().to_owned()).unwrap_or_default()) - && profile.child("properties").and_then(|props| props.child(&name)).is_some() + && profile.child("properties").and_then(|props| props.child(name)).is_some() })? .child("properties")? - .child(&name) + .child(name) }); if let Some(e) = e { return Some(PomGoto { path: p.clone(), range: range_of(text, e), kind: PomGotoKind::Property, - label: name.clone(), + label: name.to_owned(), }); } } @@ -3086,6 +3104,65 @@ mod tests { assert_eq!(goto.range, Range::new(pos_at(&text, from), pos_at(&text, to))); } + #[test] + fn a_custom_project_property_is_found_like_a_plain_one() { + let text = format!( + r#"{CHILD_HEADER} + 8001 + + + + com.example + lib + ${{project.port}} + + +"# + ); + let path = Path::new("/tmp/pom.xml"); + let goto = goto_declaration(&text, path, at(&text, "${project.port}"), None).unwrap(); + assert_eq!(goto.kind, PomGotoKind::Property); + assert_eq!(goto.label, "project.port"); + assert_eq!(goto.path, path); + let from = text.find("").unwrap(); + let to = text.find("").unwrap() + "".len(); + assert_eq!(goto.range, Range::new(pos_at(&text, from), pos_at(&text, to))); + // `${project.basedir}` has no element and no custom property: still nothing. + let basedir = text.replace("${project.port}", "${project.basedir}"); + assert_eq!(goto_declaration(&basedir, path, at(&basedir, "${project.basedir}"), None), None); + } + + #[test] + fn the_bare_builtin_shorthand_resolves_to_the_coordinate_and_a_custom_property_wins() { + let text = format!( + r#"{CHILD_HEADER}app + + + com.example + lib + + + + custom + + ${{artifactId}}-${{groupId}} docs +"# + ); + let path = Path::new("/tmp/pom.xml"); + // `@artifactId@` in a filtered resource resolves to the own coordinate. + let goto = goto_declaration(&text, path, at(&text, "${artifactId}"), None).unwrap(); + assert_eq!(goto.label, "artifactId"); + let from = text.find("app").unwrap(); + assert_eq!(goto.range.start, pos_at(&text, from)); + // A custom property of the bare name wins over the shorthand. The click + // sits inside the reference: the second one's `$` is behind the first's + // closing `}`, which `property_ref_at` does not take for the `$` itself. + let goto = goto_declaration(&text, path, at(&text, "groupId} docs"), None).unwrap(); + assert_eq!(goto.label, "groupId"); + let from = text.find("custom").unwrap(); + assert_eq!(goto.range.start, pos_at(&text, from)); + } + #[test] fn an_inherited_property_reference_finds_its_definition_in_the_parent_pom() { let tmp = tempfile::tempdir().unwrap();