From 566277337fb2870874ceb2d8ae67494f1317495f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 03:04:36 +0000 Subject: [PATCH 1/6] Start maven-hard-range-vex Refs #2378. Assisted-by: Claude Code From 9fdf4af1a403deb40988d26b0945048cb429e485 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 03:49:58 +0000 Subject: [PATCH 2/6] Read a module's own version before a Maven pin Maven reads the version a module declares for a dependency before any , so a pin does not reach a module that declares the patched artifact with a version of its own. The reactor planner counted such a module as wired by the pin all the same. For a module that requires the base version as the hard range [1.0], vendor reported the patch applied, vendor --check verified it, and vex attested not_affected while the build kept the upstream jar. A range added under a pinned root after vendoring was attested the same way, in vendored and in hosted mode. One rule now, in the three places that asked: - covers: the version a module declares, or inherits, decides whether the patch is wired there; the nearest management decides only when no entry carries a version. vex then asks the selection what such a module resolves, as for a module no pin reaches. - A hard requirement on the base version is recorded as a declaration that may still build the base, as an unresolved property is: vex withholds the statement and vendor --check names the declaration. - under: a module that requires the base version itself has no say in where a pin goes. With nothing else to wire the plan names the suffixed version nowhere, and vendor refuses it (nothing_wired) as it already refuses the same range written in . Hosted mode leaves such a reactor alone, as it does a single pom. A pin an earlier run wrote stays where it is. Refs #2378. Assisted-by: Claude Code --- .../src/vendor/jvm/maven_reactor.rs | 278 +++++++++++++++--- .../vendor/jvm/maven_reactor/hosted/tests.rs | 67 +++++ .../src/vex/discover/maven.rs | 29 ++ docs/design/maven-vendoring.md | 9 +- 4 files changed, 348 insertions(+), 35 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs index f1f23a8d40..3e0350d5e5 100644 --- a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs +++ b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs @@ -12,6 +12,7 @@ use std::ops::Range; use serde_json::{json, Value}; use crate::crawlers::maven_selection::{MavenSelection, Verdict}; +use crate::formats::maven::version::VersionRange; use crate::formats::maven::{ classpath_copy, declared_maven, interpolate_keeping, interpolate_strict, is_executable_classifier, lookup_property, maven_config_properties as cli_properties, @@ -919,12 +920,13 @@ pub fn pin_would_move(plan: &JvmPlan) -> Option<&str> { /// [`wired_checked`], and no declaration of `c`'s g:a is left at a version /// that may still read the base: the base itself, as a module or -/// dependency added since vendoring declares it (#584), or a `${...}` the -/// checkout does not define or that an inheriting pom or a profile moves -/// (#513, #459), a version-less one whose local root is left unpinned, or -/// an executable classifier or type copy of the GA (`tests`, `test-jar`, a -/// native build) the pin does not retarget. Such a module builds the -/// unpatched jar, so `vex` must not attest. +/// dependency added since vendoring declares it (#584), a hard requirement +/// on it (`[1.0]`, #2378), a `${...}` the checkout does not define or that +/// an inheriting pom or a profile moves (#513, #459), a version-less one +/// whose local root is left unpinned, or an executable classifier or type +/// copy of the GA (`tests`, `test-jar`, a native build) the pin does not +/// retarget. Such a module builds the unpatched jar, so `vex` must not +/// attest. pub fn attestable_checked( read: ReadFn<'_>, list: ListFn<'_>, @@ -973,9 +975,10 @@ fn unattestable_in(reactor: &Reactor, c: &Coords<'_>) -> Option { (!at.is_empty()).then(|| { format!( "{} may still build {ga}:{} rather than {sv}: a base version declared since \ - vendoring, a version a property, profile or unpinned root may move, or an \ - executable classifier or type copy the pin does not retarget", + vendoring or required as `[{}]`, a version a property, profile or unpinned root \ + may move, or an executable classifier or type copy the pin does not retarget", at.join(", "), + c.version, c.version ) }) @@ -1539,7 +1542,9 @@ impl Reactor { /// What the modules at and below each pom select for `patch`: every /// module is asked once, and its answer marked on each pom of its /// chain. A module `skip` names has no say (hosted mode leaves out the - /// modules a declaration already wires: a pin changes nothing there). + /// modules a declaration already wires: a pin changes nothing there), + /// and neither has one that requires the base version itself + /// ([`Self::requires_base`]): no pin reaches it. fn under( &self, selection: &MavenSelection, @@ -1549,6 +1554,9 @@ impl Reactor { let (g, a, v) = (patch.group_id, patch.artifact_id, patch.version); let mut out: BTreeMap = BTreeMap::new(); for module in self.reactor.iter().filter(|module| !skip(module)) { + if self.requires_base(module, g, a, v) { + continue; + } let (own, version) = match selection.verdict(module, g, a, v) { Verdict::Selected => (Own::Selected, None), Verdict::Other(version) => (Own::Other, Some(version)), @@ -1640,34 +1648,54 @@ impl Reactor { } /// Whether the patch `c` is wired for `module`: Maven reads its - /// suffixed version there from the nearest management on the module's - /// chain (a pin, or the project's own entry), or from a declaration - /// the module makes or inherits. + /// suffixed version there from the version the module declares itself + /// or inherits ([`Self::own_versions`]), else from the nearest + /// management on the module's chain (a pin, or the project's own + /// entry). fn covers(&self, module: &str, c: &Coords<'_>) -> bool { let sv = c.suffixed_version(); - if self - .managed_version(module, c.group_id, c.artifact_id) - .as_deref() - == Some(sv.as_str()) - { - return true; - } - self.chain(module).any(|p| { - let doc = &self.poms[p].doc; - self.keyed_declarations(p, c.group_id, c.artifact_id) + let own = self.own_versions(module, c.group_id, c.artifact_id); + if own.is_empty() { + return self + .managed_version(module, c.group_id, c.artifact_id) + .as_deref() + == Some(sv.as_str()); + } + own.iter().all(|v| v.as_deref() == Some(sv.as_str())) + } + + /// The versions `module` declares for `g:a`'s main jar itself: the + /// `` of each `` entry in the nearest pom on + /// its chain that has one, as `module` interpolates it (`None`: it + /// cannot). Maven reads a declared version before any + /// ``, so no pin reaches such a module. Empty + /// when no entry carries a version: the management decides. + fn own_versions(&self, module: &str, g: &str, a: &str) -> Vec> { + self.chain(module) + .map(|p| { + let doc = &self.poms[p].doc; + self.keyed_declarations(p, g, a) + .iter() + .filter(|(dep, _)| { + doc.section_of(*dep) == "dependencies" && doc.is_main_jar(*dep) + }) + .filter_map(|(dep, _)| doc.child(*dep, "version")) + .map(|version| self.interpolate(module, &doc.text_of(version))) + .collect::>() + }) + .find(|versions| !versions.is_empty()) + .unwrap_or_default() + } + + /// Whether `module` declares `g:a`'s main jar itself as a hard + /// requirement on `base` and nothing else (`[1.0]`): it builds that + /// version whatever a pin says. + fn requires_base(&self, module: &str, g: &str, a: &str, base: &str) -> bool { + let own = self.own_versions(module, g, a); + !own.is_empty() + && own .iter() - .any(|(dep, _)| { - let section = doc.section_of(*dep); - !section.ends_with("dependencyManagement") - && !section.starts_with("profile:") - && doc.is_main_jar(*dep) - && doc - .child(*dep, "version") - .and_then(|version| self.interpolate(module, &doc.text_of(version))) - .as_deref() - == Some(sv.as_str()) - }) - }) + .all(|v| v.as_deref().is_some_and(|v| requires_exactly(v, base))) } } @@ -2953,6 +2981,13 @@ impl Reactor { "range", format!("{at}: {ga} version {declared} is left as is, probably unpatched"), )); + // A hard requirement on the base version is that version + // whatever the repositories list, and a declared version + // beats any pin. (What another range selects is asked of + // the selection: `unwired_in`.) + if !managed_section && requires_exactly(&resolved, patch.version) { + uncertain.insert(at); + } } else { // Another patch's wiring of the same artifact (a module // that selects another version, patched too) is no @@ -3320,6 +3355,12 @@ fn is_range(version: &str) -> bool { || version == "RELEASE" } +/// Whether `version` is the hard requirement `[base]`: Maven resolves that +/// one version without listing what the repositories hold. +fn requires_exactly(version: &str, base: &str) -> bool { + VersionRange::parse(version).is_some_and(|range| range.exact() == Some(base)) +} + /// Whether a pom configures the enforcer rules that forbid POM /// repositories (by rule element or by implementation class name). fn bans_repositories(masked: &str) -> bool { @@ -6221,6 +6262,115 @@ mod tests { ); } + /// `inheriting(name)` with `deps` as its ``. + fn declaring(name: &str, deps: &str) -> Vec { + inheriting(name) + .replace( + "", + &format!(" {deps}\n"), + ) + .into_bytes() + } + + /// #2378: Maven reads the version a module declares before any + /// management, so a pin in the root does not wire a module that + /// declares a range of its own (added since the pin was written). + /// What that module resolves decides, as for a module no pin reaches. + #[test] + fn a_pin_does_not_wire_a_module_that_declares_its_own_version() { + let (_, rooted) = vendor(&two_modules()); + assert!(text(&rooted, "pom.xml").contains(PIN_TAG)); + let unwired = |b: &str, selection: &MavenSelection| { + let mut files = rooted.clone(); + files.insert("b/pom.xml".to_string(), declaring("b", b)); + let read = |p: &str| files.get(p).cloned(); + let why = unwired_modules(&read, &lister(&files), &patch().coords(), selection); + why.unwrap() + }; + let base = selecting(Some(Ok("1.10.0")), Some(Ok("1.10.0"))); + for declared in ["[1.10.0]", "[1.9,2.0)", "LATEST"] { + let why = unwired(&dep(declared), &base).expect(declared); + assert!(why.contains("b/pom.xml resolves it unpatched"), "{why}"); + let unread = selecting(Some(Ok("1.10.0")), Some(Err("unread"))); + let why = unwired(&dep(declared), &unread).expect(declared); + assert!( + why.contains("b/pom.xml may resolve it unpatched (unread)"), + "{why}" + ); + // The range selects another version: nothing of this patch. + let other = selecting(Some(Ok("1.10.0")), Some(Ok("1.11.0"))); + assert_eq!(unwired(&dep(declared), &other), None, "{declared}"); + } + // The pin is what a version-less declaration reads, and a module + // that declares the suffixed version reads it there. + let versionless = dep("").replace("", ""); + assert_eq!(unwired(&versionless, &base), None); + assert_eq!(unwired(&dep(SV), &base), None); + // A module that inherits the declaration reads it as its parent + // does. + let mut files = rooted.clone(); + let parent = text(&files, "pom.xml").replace( + "", + &format!( + " {}\n", + dep("[1.10.0]") + ), + ); + files.insert("pom.xml".to_string(), parent.into_bytes()); + let read = |p: &str| files.get(p).cloned(); + let why = unwired_modules(&read, &lister(&files), &patch().coords(), &base) + .unwrap() + .expect("inherited"); + assert!( + why.contains("a/pom.xml resolves it unpatched, and 1 more"), + "{why}" + ); + } + + /// #2378: no pin reaches a module that requires the base version + /// itself, so it has no say in where one goes. With no other module + /// to wire, the plan names the suffixed version nowhere and is + /// refused (#513) instead of counting the patch as applied over a pin + /// that nothing reads. + #[test] + fn a_module_that_requires_the_base_version_takes_no_pin() { + let mut files = two_modules(); + files.insert("a/pom.xml".to_string(), declaring("a", &dep("[1.10.0]"))); + let alone = answering(&files, &[("a/pom.xml", Ok("1.10.0"))]); + let plan = run_selecting(&files, &alone); + assert_eq!(reasons(&plan), ["range"]); + let after = applied(&files, &plan); + assert!(!text(&after, "pom.xml").contains(SV)); + assert_eq!(after["a/pom.xml"], files["a/pom.xml"]); + let refusal = left_unwired(&files, &plan).expect("refused"); + assert_eq!(refusal_reason(&refusal), "nothing_wired"); + assert!( + refusal.detail.contains("(reason: range: a/pom.xml:"), + "{}", + refusal.detail + ); + // A sibling that reaches the artifact through other artifacts + // reads the pin: the root takes it, and the module beside it + // keeps the base version. + let both = answering( + &files, + &[("a/pom.xml", Ok("1.10.0")), ("b/pom.xml", Ok("1.10.0"))], + ); + let plan = run_selecting(&files, &both); + let after = applied(&files, &plan); + assert!(text(&after, "pom.xml").contains(PIN_TAG)); + assert_eq!(after["a/pom.xml"], files["a/pom.xml"]); + assert!(left_unwired(&files, &plan).is_none()); + let read = |p: &str| after.get(p).cloned(); + let c = patch().coords(); + assert!(!attestable_checked(&read, &lister(&after), &c).unwrap()); + // A pin written before stays where it is. + let (_, pinned) = vendor(&files); + assert!(text(&pinned, "pom.xml").contains(PIN_TAG)); + let again = run_selecting(&pinned, &alone); + assert!(again.writes.is_empty(), "{:?}", pom_rels(&again)); + } + /// A declaration that names the base version is rewritten whatever /// the selection says of the rest: that replaces a version in place. #[test] @@ -9320,6 +9470,66 @@ mod tests { assert_eq!(attestable(&after), (true, true)); } + /// #2378: a hard requirement on the base version (`[1.10.0]`) is that + /// version whatever the repositories list, and Maven reads a declared + /// version before any pin. The module keeps the unpatched jar beside a + /// rewritten sibling: not attestable. + #[test] + fn a_hard_requirement_on_the_base_version_is_not_attestable() { + let defining = ROOT.replace( + " ", + " [1.10.0]\n ", + ); + for (root, declared) in [ + (ROOT, "[1.10.0]"), + (ROOT, "[ 1.10.0 ]"), + (defining.as_str(), "${ct.version}"), + ] { + let a = module("a", &dep(declared)); + let files = fs(&[ + ("pom.xml", root), + ("a/pom.xml", &a), + ("b/pom.xml", &module("b", &dep("1.10.0"))), + ]); + let (plan, after) = vendor(&files); + assert_eq!(reasons(&plan), ["range"], "{declared}"); + assert_eq!(text(&after, "a/pom.xml"), a, "{declared}"); + assert_eq!(text(&after, "b/pom.xml"), module("b", &dep(SV))); + assert_eq!(attestable(&after), (true, false), "{declared}"); + let read = |p: &str| after.get(p).cloned(); + let why = unattestable_checked(&read, &lister(&after), &patch().coords()) + .unwrap() + .expect("why"); + assert!(why.starts_with("a/pom.xml:10 may still build "), "{why}"); + } + // A hard requirement on another version builds that version, and + // what an open range selects is not read off the poms: neither is + // a declaration left at the base (`unwired_modules` asks the + // selection). A managed entry alone puts nothing on a classpath. + let managing = ROOT.replace( + " ", + &format!( + " {}\ + \n ", + dep("[1.10.0]") + ), + ); + for (root, declared) in [ + (ROOT, "[1.11.0]"), + (ROOT, "[1.9,2.0)"), + (ROOT, "[1.10.0],[1.12.0]"), + (managing.as_str(), "[1.11.0]"), + ] { + let files = fs(&[ + ("pom.xml", root), + ("a/pom.xml", &module("a", &dep(declared))), + ("b/pom.xml", &module("b", &dep("1.10.0"))), + ]); + let (_, after) = vendor(&files); + assert_eq!(attestable(&after), (true, true), "{declared}"); + } + } + /// A `` copy of the GA is its own coordinate: the /// classifier-less pin does not retarget it and the vendored tree does /// not serve it, so Maven fetches the public release. An executable diff --git a/crates/socket-patch-core/src/vendor/jvm/maven_reactor/hosted/tests.rs b/crates/socket-patch-core/src/vendor/jvm/maven_reactor/hosted/tests.rs index 63a0dde72c..4bd1fcd1d6 100644 --- a/crates/socket-patch-core/src/vendor/jvm/maven_reactor/hosted/tests.rs +++ b/crates/socket-patch-core/src/vendor/jvm/maven_reactor/hosted/tests.rs @@ -526,6 +526,73 @@ fn an_undetermined_module_is_not_pinned_and_its_sibling_is() { assert_eq!(reasons(&unread), ["selection_undetermined"]); } +/// #2378: Maven reads the version a module declares before any pin, so no +/// pin reaches a module that requires the base version itself +/// (`[1.10.0]`). It has no say in where a pin goes: its sibling is wired +/// as it would be without it, and the patch is not attested while the +/// module builds the base version. Alone, it leaves nothing to wire. +#[test] +fn a_module_that_requires_the_base_version_takes_no_pin_and_is_not_attested() { + let hard = dep(A, Some("[1.10.0]")); + let through = dep("lib-y", Some("1.0")); + let both = [("a/pom.xml", Ok(BASE)), ("b/pom.xml", Ok(BASE))]; + for (b, edits) in [ + // `b` names the base version: rewritten where it stands, and no + // module is left that a pin would wire. + (dep(A, Some(BASE)), [("b/pom.xml", HostedEditKind::Version)]), + // `b` reaches it through another artifact: the root's pin is what + // it reads. + (through.clone(), [("pom.xml", HostedEditKind::Pin)]), + ] { + let files = fs(&[ + ("pom.xml", &root("")), + ("a/pom.xml", &module("a", &hard)), + ("b/pom.xml", &module("b", &b)), + ]); + let selection = selecting(&files, &both); + let plan = planned(&files, Some(&selection)); + assert_eq!(kinds(&plan), edits); + assert_eq!(reasons(&plan), ["range"]); + assert!(plan.wired); + let after = written(&files, &plan); + assert_eq!(after["a/pom.xml"], files["a/pom.xml"]); + assert_invariant(&after); + assert_unwires_to(&after, &files); + let why = reactor(&after) + .unattestable(&coords(), &selection) + .expect("not attested"); + assert!(why.starts_with("a/pom.xml:10 may still build "), "{why}"); + // The next scan writes nothing more. + let again = planned(&after, Some(&selection)); + assert!(again.poms.is_empty(), "{:?}", again.edits); + } + + let files = fs(&[ + ("pom.xml", &root("")), + ("a/pom.xml", &module("a", &hard)), + ("b/pom.xml", &module("b", &through)), + ]); + let alone = selecting(&files, &[("a/pom.xml", Ok(BASE))]); + let plan = planned(&files, Some(&alone)); + assert!(plan.poms.is_empty() && !plan.wired, "{:?}", plan.edits); + assert_eq!(reasons(&plan), ["range"]); + + // A range of its own that selects the base version is not what a pin + // in the root wires either: what the module resolves decides. + let files = fs(&[ + ("pom.xml", &root("")), + ("a/pom.xml", &module("a", &dep(A, Some("[1.9,2.0)")))), + ("b/pom.xml", &module("b", &through)), + ]); + let selection = selecting(&files, &both); + let after = written(&files, &planned(&files, Some(&selection))); + assert!(after["pom.xml"].contains(TAG), "{}", after["pom.xml"]); + let why = reactor(&after) + .unattestable(&coords(), &selection) + .expect("not attested"); + assert!(why.contains("a/pom.xml resolves it unpatched"), "{why}"); +} + /// A scan over a cold local repository wires what it can tell; the next /// one, once Maven has resolved the project, pins the module the first /// could not, and moves nothing the first wrote. Told nothing new, it diff --git a/crates/socket-patch-core/src/vex/discover/maven.rs b/crates/socket-patch-core/src/vex/discover/maven.rs index 3afbb9f21b..806eb66a91 100644 --- a/crates/socket-patch-core/src/vex/discover/maven.rs +++ b/crates/socket-patch-core/src/vex/discover/maven.rs @@ -2498,6 +2498,35 @@ mod tests { let u = &out.unattested[0]; assert_eq!(u.kind, UnattestedKind::MavenReactorRoot); assert!(u.detail.contains("b/pom.xml"), "{}", u.detail); + + // Neither is one that requires the base version as a hard range + // by a pin of its own or of its parent (#2378): Maven reads the + // declared version first. + for pinned in ["pom.xml", "b/pom.xml"] { + let requiring = format!( + "\n{}\n", + dep("org.slf4j", "slf4j-api", Some("[1.7.36]")) + ); + let (root, b) = match pinned { + "pom.xml" => (reactor_root(&pin), reactor_module("b", &requiring)), + _ => ( + reactor_root(""), + reactor_module("b", &format!("{requiring}{pin}")), + ), + }; + p.write("pom.xml", root); + p.write("a/pom.xml", reactor_module("a", "")); + p.write("b/pom.xml", b); + let out = run(&p).await; + assert_eq!(out.unattested.len(), 1, "{pinned}: {:?}", out.unattested); + let u = &out.unattested[0]; + assert_eq!(u.kind, UnattestedKind::MavenReactorRoot, "{pinned}"); + assert!( + u.detail.contains("b/pom.xml:") && u.detail.contains("may still build"), + "{pinned}: {}", + u.detail + ); + } } /// A `${property}` version wired through the property is a pin of the diff --git a/docs/design/maven-vendoring.md b/docs/design/maven-vendoring.md index ab7c58fbca..347bb62c87 100644 --- a/docs/design/maven-vendoring.md +++ b/docs/design/maven-vendoring.md @@ -160,7 +160,14 @@ neither does a root a conflicting declaration rules out; the poms below them are still placed. A parent that resolves the base version itself above a module that resolves another takes no pin either (`pin_would_move_module`): Maven's management always reaches the children, so there is no pin that -patches the parent and leaves the child alone. `vex` attests the patch only +patches the parent and leaves the child alone. Maven reads the version a +module declares itself before any management, so no pin reaches a module that +declares the artifact with a version of its own. One that requires the base +version as a hard range (`[1.0]`) builds that version whatever the +repositories list: it has no say in where a pin goes, a plan with nothing +else to wire is refused (`nothing_wired`, with the `range` reason), and beside +modules that are wired `vendor --check` names the declaration +(`vendor_jvm_unattested`). `vex` attests the patch only when every module that resolves the base version is reached by its wiring. When no module of the build selects the base version the patch is skipped before anything is downloaded (`vendor_maven_version_not_selected`, exit 0, naming From 08b9c91898fffbd96370471e57fb393e325b471f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 03:49:59 +0000 Subject: [PATCH 3/6] Cover the Maven hard range through the CLI Five projects through vendor, vendor --check and vex: a module that requires the base version as [1.0] alone and beside a rewritten sibling, and a module added after vendoring that declares [1.0], the open range [1.0,2.0) or another version. Against main's binary the first case fails: vendor exits 0 where it must refuse. Refs #2378. Assisted-by: Claude Code --- .../socket-patch-cli/tests/vendor_jvm_cli.rs | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/crates/socket-patch-cli/tests/vendor_jvm_cli.rs b/crates/socket-patch-cli/tests/vendor_jvm_cli.rs index 1cd6b794eb..f09d34643d 100644 --- a/crates/socket-patch-cli/tests/vendor_jvm_cli.rs +++ b/crates/socket-patch-cli/tests/vendor_jvm_cli.rs @@ -1416,6 +1416,140 @@ fn maven_reactor_uncertain_property_version_withholds_vex() { } } +/// #2378: Maven reads the version a module declares before any pin, so a +/// module that requires the base version as the hard range `[1.0]`, or +/// selects it from a range of its own, keeps the unpatched jar. `vex` does +/// not attest it, `vendor --check` says so for the hard range (what an +/// open range selects depends on the local repository, which the check +/// does not read), and with nothing else to wire `vendor` refuses rather +/// than counting the patch as applied over a pin that nothing reads. +#[test] +fn maven_reactor_declared_range_on_the_base_withholds_vex() { + // (what `a` declares, a sibling at the literal base, `b` added after + // vendoring and what it declares) + let cases: [(&str, bool, Option<&str>); 5] = [ + ("[1.0]", false, None), + ("[1.0]", true, None), + ("1.0", false, Some("[1.0]")), + ("1.0", false, Some("[1.0,2.0)")), + ("1.0", false, Some("[2.0]")), + ]; + for (declared, sibling, late) in cases { + let case = format!("a: {declared}, sibling: {sibling}, late: {late:?}"); + let tmp = tempfile::tempdir().unwrap(); + let root = tmp.path(); + fixture(root, Shape::Reactor, &[("foo", FOO_UUID)]); + // What Maven lists when it resolves a range of `foo`. + write( + root, + "m2/org/example/foo/maven-metadata-local.xml", + b"org.examplefoo\ + 1.0\n", + ); + let pristine = std::fs::read_to_string(root.join("proj/a/pom.xml")).unwrap(); + let declaring = |name: &str, version: &str| { + pristine + .replace( + "a", + &format!("{name}"), + ) + .replace( + "1.0", + &format!("{version}"), + ) + }; + let add_b = |version: &str| { + let agg = std::fs::read_to_string(root.join("proj/pom.xml")).unwrap(); + let agg = agg.replace( + "a", + "a\n b", + ); + write(root, "proj/pom.xml", agg.as_bytes()); + write(root, "proj/b/pom.xml", declaring("b", version).as_bytes()); + }; + let a = declaring("a", declared); + write(root, "proj/a/pom.xml", a.as_bytes()); + if sibling { + add_b("1.0"); + } + let vex = root.join("vex.json"); + let vex_args = [ + "vex", + "-O", + vex.to_str().unwrap(), + "--product", + "pkg:generic/x@1", + ]; + if declared != "1.0" && !sibling { + assert_refused(root, "nothing_wired", "reason: range: a/pom.xml:6: "); + let (_, env) = socket(root, &vex_args); + assert!(!env.to_string().contains("not_affected"), "{case}: {env}"); + assert!(!vex.exists(), "{case}: {env}"); + continue; + } + let env = ok(root, &["vendor"]); + assert_eq!( + env.to_string().contains("reason: range: a/pom.xml:6: "), + declared != "1.0", + "{case}: {env}" + ); + assert_eq!( + std::fs::read_to_string(root.join("proj/a/pom.xml")).unwrap() == a, + declared != "1.0", + "{case}" + ); + assert!(wiring_text(root).contains("-socket."), "{case}: wired"); + if let Some(version) = late { + add_b(version); + } + + // The entry is intact and wired either way: the check passes, and + // names the declaration it can tell keeps the base version. + let env = ok(root, &["vendor", "--check"]); + let unattested: Vec<&str> = env["warnings"] + .as_array() + .into_iter() + .flatten() + .filter(|w| w["code"] == "vendor_jvm_unattested") + .filter_map(|w| w["detail"].as_str()) + .collect(); + let hard = match (declared, late) { + ("[1.0]", _) => Some("a/pom.xml:6"), + (_, Some("[1.0]")) => Some("b/pom.xml:6"), + _ => None, + }; + match hard { + Some(at) => assert!( + unattested.len() == 1 + && unattested[0].contains(&format!("{at} may still build org.example:foo:1.0")), + "{case}: {env}" + ), + None => assert!(unattested.is_empty(), "{case}: {env}"), + } + + let (code, env) = socket(root, &vex_args); + if late == Some("[2.0]") { + // `b` requires another version: nothing of this patch. + assert_eq!(code, Some(0), "{case}: {env}"); + let doc: serde_json::Value = + serde_json::from_slice(&std::fs::read(&vex).unwrap()).unwrap(); + assert_eq!(doc["statements"][0]["status"], "not_affected", "{case}"); + continue; + } + let withheld = match hard { + Some(_) => "vendor_unwired", + None => "vendor_jvm_shape_unsupported", + }; + assert_eq!( + events(&env), + [(purl("foo"), "skipped".to_string(), withheld.to_string())], + "{case}: {env}" + ); + assert!(!env.to_string().contains("not_affected"), "{case}: {env}"); + assert!(!vex.exists(), "{case}: {env}"); + } +} + /// #584: a module added after vendoring that declares the base literally /// builds the unpatched jar. `vendor --check` flags the drift and `vex` /// withholds attestation until a re-vendor rewrites the module. From 627ac67eb8a2ec395a8f3a59beaf3e5c57222a44 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 04:13:38 +0000 Subject: [PATCH 4/6] Let a version-less entry read the pin Review of the last two commits, checked against a real Maven 3.9.11: a module that declares the artifact with no version replaces the entry it would inherit, and reads the management. The helper walked past that entry to the parent's versioned one, so such a module was counted as unwired under a pin it does read, and as having no say in where a pin goes. It now stops at the nearest pom that declares the artifact at all. The same review found a pin nothing reads one step further: a module whose own requires the base version as [1.0] overrides any pin above it, yet the root was pinned for it and vendor reported the patch applied. Such a module has no say either; with nothing else to wire the plan is refused, as it is when the root's own management carries that range. Refs #2378. Assisted-by: Claude Code --- .../src/vendor/jvm/maven_reactor.rs | 103 ++++++++++++++---- 1 file changed, 82 insertions(+), 21 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs index 3e0350d5e5..8dfe9694be 100644 --- a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs +++ b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs @@ -1666,36 +1666,45 @@ impl Reactor { /// The versions `module` declares for `g:a`'s main jar itself: the /// `` of each `` entry in the nearest pom on - /// its chain that has one, as `module` interpolates it (`None`: it - /// cannot). Maven reads a declared version before any - /// ``, so no pin reaches such a module. Empty - /// when no entry carries a version: the management decides. + /// its chain that declares it (a module's entry replaces the one it + /// would inherit), as `module` interpolates it (`None`: it cannot). + /// Maven reads a declared version before any ``, + /// so no pin reaches such a module. Empty when those entries carry no + /// version, or no pom declares it: the management decides. fn own_versions(&self, module: &str, g: &str, a: &str) -> Vec> { self.chain(module) - .map(|p| { + .find_map(|p| { let doc = &self.poms[p].doc; - self.keyed_declarations(p, g, a) - .iter() - .filter(|(dep, _)| { - doc.section_of(*dep) == "dependencies" && doc.is_main_jar(*dep) - }) - .filter_map(|(dep, _)| doc.child(*dep, "version")) - .map(|version| self.interpolate(module, &doc.text_of(version))) - .collect::>() + let entries: Vec = self + .keyed_declarations(p, g, a) + .into_iter() + .map(|(dep, _)| dep) + .filter(|&dep| doc.section_of(dep) == "dependencies" && doc.is_main_jar(dep)) + .collect(); + (!entries.is_empty()).then(|| { + entries + .into_iter() + .filter_map(|dep| doc.child(dep, "version")) + .map(|version| self.interpolate(module, &doc.text_of(version))) + .collect() + }) }) - .find(|versions| !versions.is_empty()) .unwrap_or_default() } - /// Whether `module` declares `g:a`'s main jar itself as a hard - /// requirement on `base` and nothing else (`[1.0]`): it builds that - /// version whatever a pin says. + /// Whether `module` builds `base` whatever a pin says: every version + /// it declares for `g:a`'s main jar itself is a hard requirement on + /// `base` (`[1.0]`), or it declares none and the nearest management + /// on its chain, which a pin above it does not override, is one. fn requires_base(&self, module: &str, g: &str, a: &str, base: &str) -> bool { let own = self.own_versions(module, g, a); - !own.is_empty() - && own - .iter() - .all(|v| v.as_deref().is_some_and(|v| requires_exactly(v, base))) + if own.is_empty() { + return self + .managed_version(module, g, a) + .is_some_and(|v| requires_exactly(&v, base)); + } + own.iter() + .all(|v| v.as_deref().is_some_and(|v| requires_exactly(v, base))) } } @@ -6325,6 +6334,14 @@ mod tests { why.contains("a/pom.xml resolves it unpatched, and 1 more"), "{why}" ); + // A version-less entry of its own replaces the one a module would + // inherit, and reads the pin. + files.insert("b/pom.xml".to_string(), declaring("b", &versionless)); + let read = |p: &str| files.get(p).cloned(); + let why = unwired_modules(&read, &lister(&files), &patch().coords(), &base) + .unwrap() + .expect("a inherits"); + assert!(why.ends_with("a/pom.xml resolves it unpatched"), "{why}"); } /// #2378: no pin reaches a module that requires the base version @@ -6369,6 +6386,50 @@ mod tests { assert!(text(&pinned, "pom.xml").contains(PIN_TAG)); let again = run_selecting(&pinned, &alone); assert!(again.writes.is_empty(), "{:?}", pom_rels(&again)); + + // The same requirement in the module's own management, nearer + // than any pin above it: nothing to wire either. + let managing = inheriting("a").replace( + "", + &format!( + " {}\ + \n", + dep("[1.10.0]") + ), + ); + files.insert("a/pom.xml".to_string(), managing.into_bytes()); + let plan = run_selecting(&files, &alone); + assert!(!text(&applied(&files, &plan), "pom.xml").contains(SV)); + let refusal = left_unwired(&files, &plan).expect("refused"); + assert!( + refusal.detail.contains("(reason: range: a/pom.xml:"), + "{}", + refusal.detail + ); + + // A module that declares the artifact with no version replaces + // the requirement it would inherit: the pin is what it reads, so + // it has its say, and the sibling that inherits has none. + let requiring = PARENT_OF_TWO.replace( + "", + &format!( + " {}\n", + dep("[1.10.0]") + ), + ); + let versionless = dep("").replace("", ""); + let redeclaring = String::from_utf8(declaring("a", &versionless)).unwrap(); + let files = fs(&[ + ("pom.xml", &requiring), + ("a/pom.xml", &redeclaring), + ("b/pom.xml", &inheriting("b")), + ]); + let inherits = answering(&files, &[("b/pom.xml", Ok("1.10.0"))]); + let plan = run_selecting(&files, &inherits); + assert!(!text(&applied(&files, &plan), "pom.xml").contains(SV)); + let plan = run_selecting(&files, &both); + assert!(text(&applied(&files, &plan), "pom.xml").contains(PIN_TAG)); + assert!(left_unwired(&files, &plan).is_none()); } /// A declaration that names the base version is rewritten whatever From a190e96051d92be51ea5f13de67d2ba26629f5d4 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 04:34:42 +0000 Subject: [PATCH 5/6] Keep a plain other version under a pin wired A second review found that the new reading of a module's own version asked the selection too much: a module that declares another plain version under an existing pin (1.11.0 beside a patch of 1.10.0) builds that version, and vex attested the patch before. With the reading of the last commits it asked what the module resolves, and withheld the statement wherever that cannot be read: no Maven local repository, a Maven 4 build, a project that is not on disk. Such a module is wired as the management says again, as on main. Only a version that may be the base is asked of the selection: a range, the base version itself, or one that cannot be read. Refs #2378. Assisted-by: Claude Code --- .../src/vendor/jvm/maven_reactor.rs | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs index 8dfe9694be..12ce8ef24a 100644 --- a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs +++ b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs @@ -1648,20 +1648,29 @@ impl Reactor { } /// Whether the patch `c` is wired for `module`: Maven reads its - /// suffixed version there from the version the module declares itself - /// or inherits ([`Self::own_versions`]), else from the nearest - /// management on the module's chain (a pin, or the project's own - /// entry). + /// suffixed version there from the nearest management on the module's + /// chain (a pin, or the project's own entry), unless the module + /// declares a version itself or inherits one + /// ([`Self::own_versions`]), which it reads first. A declared + /// suffixed version wires it; another plain version is the one it + /// builds, and leaves the management to wire what else it reaches; a + /// range, the base version or a version that cannot be read may be + /// the base, whatever the management says. fn covers(&self, module: &str, c: &Coords<'_>) -> bool { let sv = c.suffixed_version(); + let managed = self + .managed_version(module, c.group_id, c.artifact_id) + .as_deref() + == Some(sv.as_str()); let own = self.own_versions(module, c.group_id, c.artifact_id); if own.is_empty() { - return self - .managed_version(module, c.group_id, c.artifact_id) - .as_deref() - == Some(sv.as_str()); + return managed; } - own.iter().all(|v| v.as_deref() == Some(sv.as_str())) + own.iter().all(|own| match own.as_deref() { + Some(own) if own == sv => true, + Some(own) => managed && !is_range(own) && !is_base_like(own, c.version), + None => false, + }) } /// The versions `module` declares for `g:a`'s main jar itself: the @@ -6310,6 +6319,15 @@ mod tests { let other = selecting(Some(Ok("1.10.0")), Some(Ok("1.11.0"))); assert_eq!(unwired(&dep(declared), &other), None, "{declared}"); } + // Another plain version is what the module builds, whatever can + // be told of the rest: nothing of this patch, as before. + let untold = MavenSelection::unavailable("nothing was read"); + assert_eq!(unwired(&dep("1.11.0"), &untold), None); + let why = unwired(&dep("[1.10.0]"), &untold).expect("a range"); + assert!( + why.contains("b/pom.xml may resolve it unpatched (nothing was read)"), + "{why}" + ); // The pin is what a version-less declaration reads, and a module // that declares the suffixed version reads it there. let versionless = dep("").replace("", ""); From 24df7b551f6b61de301d10501200d02aa7b7214c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 04:49:06 +0000 Subject: [PATCH 6/6] Count a required suffixed version as wired A third review pass found no defect in the change, and two loose ends. A module that declares the suffixed version as a hard range by hand, [1.10.0-socket.], builds the vendored jar; the new reading took it for a range that may be the base, where main counted the module wired by its pin. It is wired by its own declaration. The design note said vendor --check names "the declaration"; it names a entry. A requirement in a module's own takes no pin, and vex withholds it by what the module resolves. Refs #2378. Assisted-by: Claude Code --- crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs | 3 ++- docs/design/maven-vendoring.md | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs index 12ce8ef24a..fa0abb8c05 100644 --- a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs +++ b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs @@ -1667,7 +1667,7 @@ impl Reactor { return managed; } own.iter().all(|own| match own.as_deref() { - Some(own) if own == sv => true, + Some(own) if own == sv || requires_exactly(own, &sv) => true, Some(own) => managed && !is_range(own) && !is_base_like(own, c.version), None => false, }) @@ -6333,6 +6333,7 @@ mod tests { let versionless = dep("").replace("", ""); assert_eq!(unwired(&versionless, &base), None); assert_eq!(unwired(&dep(SV), &base), None); + assert_eq!(unwired(&dep(&format!("[{SV}]")), &base), None); // A module that inherits the declaration reads it as its parent // does. let mut files = rooted.clone(); diff --git a/docs/design/maven-vendoring.md b/docs/design/maven-vendoring.md index 347bb62c87..86fe9bcfc5 100644 --- a/docs/design/maven-vendoring.md +++ b/docs/design/maven-vendoring.md @@ -166,8 +166,10 @@ declares the artifact with a version of its own. One that requires the base version as a hard range (`[1.0]`) builds that version whatever the repositories list: it has no say in where a pin goes, a plan with nothing else to wire is refused (`nothing_wired`, with the `range` reason), and beside -modules that are wired `vendor --check` names the declaration -(`vendor_jvm_unattested`). `vex` attests the patch only +modules that are wired `vendor --check` names its `` entry +(`vendor_jvm_unattested`). A module whose own `` +carries that requirement has no say either; `vex` withholds its patch by +what the module resolves. `vex` attests the patch only when every module that resolves the base version is reached by its wiring. When no module of the build selects the base version the patch is skipped before anything is downloaded (`vendor_maven_version_not_selected`, exit 0, naming