diff --git a/crates/ide-build/src/jbuild/tests.rs b/crates/ide-build/src/jbuild/tests.rs index 9a4b3a638..baeb37a3c 100644 --- a/crates/ide-build/src/jbuild/tests.rs +++ b/crates/ide-build/src/jbuild/tests.rs @@ -416,6 +416,47 @@ fn builds_maven_modules_with_javac_and_only_what_changed() { assert!(f.out("t.app/test-classes/a/AggTest.class").is_file()); } +/// #7743: a dependency's own dependencies come before its next sibling on the classpath, as in +/// `mvn compile`, so of two artifacts with the same classes (`dom4j:dom4j` 1.x and +/// `org.dom4j:dom4j` 2.x) javac sees the one Maven does. +#[test] +fn maven_classpath_lists_dependencies_in_maven_order() { + let module = |name: &str, deps: &[&str]| { + let deps: String = deps + .iter() + .map(|d| { + format!("t{d}1") + }) + .collect(); + format!( + "4.0.0troot\ + 1{name}{deps}" + ) + }; + let root = ROOT_POM.replace( + "lib", + "newdomolddomxlib", + ); + let (newdom, olddom, x, lib) = + (module("newdom", &[]), module("olddom", &[]), module("x", &["newdom"]), module("lib", &["x", "olddom"])); + let Some(f) = Fixture::new(&[ + ("pom.xml", &root), + ("newdom/pom.xml", &newdom), + ("olddom/pom.xml", &olddom), + ("x/pom.xml", &x), + ("lib/pom.xml", &lib), + ("newdom/src/main/java/d/Element.java", "package d;\npublic class Element { public void fresh() {} }\n"), + ("olddom/src/main/java/d/Element.java", "package d;\npublic class Element {}\n"), + ("app/src/main/java/a/A.java", "package a;\nclass A { void f() { new d.Element().fresh(); } }\n"), + ]) else { + return; + }; + let built = f.launch(false); + assert_eq!(built.failure, None, "{:?}", built.messages); + let position = |name: &str| built.class_paths.iter().position(|p| *p == f.out(&format!("t.{name}/classes"))); + assert!(position("newdom") < position("olddom"), "{:?}", built.class_paths); +} + #[test] fn maps_resources_to_the_output_paths_the_build_tool_writes() { let tmp = tempfile::tempdir().unwrap(); diff --git a/java/java-build/maven/ai/openidea/javac/maven/BuildModel.java b/java/java-build/maven/ai/openidea/javac/maven/BuildModel.java index abe3ee604..1a3356925 100644 --- a/java/java-build/maven/ai/openidea/javac/maven/BuildModel.java +++ b/java/java-build/maven/ai/openidea/javac/maven/BuildModel.java @@ -323,15 +323,19 @@ public final class BuildModel extends AbstractMavenLifecycleParticipant { } } - /** Pre-order, as Maven lists `project.getArtifacts()` for classpaths. */ + /** + * Pre-order, as Maven lists `project.getArtifacts()` for classpaths: a dependency's own + * dependencies come before its next sibling, so jars with the same classes but different + * coordinates (`dom4j:dom4j` and `org.dom4j:dom4j`) resolve as they do in `mvn compile`. + */ private static void collect(DependencyNode node, List out, Set seen) { for (DependencyNode child : node.getChildren()) { Artifact a = child.getArtifact(); String key = a == null ? String.valueOf(System.identityHashCode(child)) : a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getExtension() + ":" + a.getClassifier(); if (seen.add(key)) out.add(child); + collect(child, out, seen); } - for (DependencyNode child : node.getChildren()) collect(child, out, seen); } /** diff --git a/java/java-build/openidea-java-build.jar b/java/java-build/openidea-java-build.jar index f262620be..c58d978b4 100644 Binary files a/java/java-build/openidea-java-build.jar and b/java/java-build/openidea-java-build.jar differ