Skip to content

Commit 791ded0

Browse files
committed
Merge branch 'fix/maven-discovery-property-overrides' into fix/maven-discovery-coordinate-overrides
2 parents 46005e1 + ddf4808 commit 791ded0

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎crates/socket-patch-core/src/formats/maven/mod.rs‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ struct ProfileProperties {
176176
/// classpaths, `<pluginRepositories>` / `<distributionManagement>` plugin
177177
/// and deploy repositories; `<profiles>` apply only when activated. Shared
178178
/// by the VEX reader ([`parse_pom`]) and the hosted rewriter, so the
179-
/// rewriter edits exactly the markup VEX later attests.
179+
/// rewriter edits exactly the markup VEX later attests. Only the VEX
180+
/// reader weighs profile `<properties>` ([`Self::read_profiles`]).
180181
#[derive(Debug)]
181182
pub(crate) struct PomScope {
182183
/// The pom with comments and CDATA blanked byte-for-byte.
@@ -332,7 +333,17 @@ impl PomScope {
332333
.unwrap_or_else(|| text.to_string());
333334
match self.resolve_in_build(text, pos, interpolate_strict) {
334335
(Some(build), alternatives) => (spelled, build, alternatives),
335-
(None, _) => (spelled, text.to_string(), Vec::new()),
336+
// Unresolved here (a parent pom this reader never sees may
337+
// define it): the build version stays as written, but a value
338+
// some profiles give it is still one the build may resolve.
339+
(None, alternatives) => (
340+
spelled,
341+
text.to_string(),
342+
alternatives
343+
.into_iter()
344+
.filter(|a| a.value != text)
345+
.collect(),
346+
),
336347
}
337348
}
338349

@@ -544,10 +555,15 @@ fn inside(pos: usize, set: &[Element]) -> bool {
544555
pub(crate) struct ScopedDep {
545556
pub(crate) element: Element,
546557
/// `<groupId>` / `<artifactId>` with every `${prop}` the build defines
547-
/// with no profile active (user properties, the project model, the
548-
/// root `<properties>`) interpolated
549-
/// ([`PomScope::interpolate_coordinate`]); an undefined one stays as
550-
/// written, so [`may_name`] can tell it apart.
558+
/// (user properties, the project model, the root `<properties>`)
559+
/// interpolated ([`PomScope::interpolate_coordinate`]); an undefined
560+
/// one stays as written, so [`may_name`] can tell it apart. No profile
561+
/// is active but, for a `<profiles>` declaration, its own, whose
562+
/// `<properties>` count only once [`PomScope::read_profiles`] has run:
563+
/// the VEX reader runs it, the hosted rewriter does not, so a profile
564+
/// declaration whose coordinates only its own profile's `<properties>`
565+
/// define is the pinned GA to VEX (which then withholds the pin) and
566+
/// not to the rewriter.
551567
pub(crate) group: Option<String>,
552568
pub(crate) artifact: Option<String>,
553569
/// The trimmed `<groupId>` / `<artifactId>` text as written.
@@ -735,10 +751,12 @@ fn profile_properties(
735751
.iter()
736752
.find(|p| props.start >= p.start && props.start < p.end)
737753
.copied();
754+
// Maven names a profile without an `<id>` `default`, as the
755+
// vendored reactor does.
738756
let id = holder
739757
.and_then(|p| direct_child_text(p.inner(text), "id"))
740758
.filter(|id| !id.is_empty())
741-
.unwrap_or_else(|| format!("#{}", out.len() + 1));
759+
.unwrap_or_else(|| "default".to_string());
742760
let mut map = BTreeMap::new();
743761
read_properties(props.inner(text), &mut map)?;
744762
out.push(ProfileProperties {

‎crates/socket-patch-core/src/formats/maven/properties.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ pub(crate) fn profile_combinations(
244244
}
245245

246246
/// The `name -> trimmed value` pairs of a `<properties>` body, into `map`
247-
/// (a name's first definition wins). An element's name is its tag up to
247+
/// (a name's last definition wins, as in Maven's model reader). An element's name is its tag up to
248248
/// whitespace or `/`, so `<v combine.self="override">` and `<v >` define
249249
/// `v`, and `<v/>` defines it empty; a comment-blanked or malformed child
250250
/// is an error (fail closed).

‎crates/socket-patch-core/src/vex/discover/maven.rs‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,11 +1104,19 @@ fn profile_override(
11041104
};
11051105
if !d.in_profile {
11061106
// Off the pin as built, it is no pin (or another bypass).
1107-
if build_version(d) != Some(pinned) {
1107+
// One this pom leaves unresolved (a parent pom's property)
1108+
// may be the pin.
1109+
let built = build_version(d)?;
1110+
let unresolved = built.contains("${");
1111+
if built != pinned && !unresolved {
11081112
return None;
11091113
}
1110-
// The smallest combination of profiles that moves it.
1111-
let moved = d.version_alternatives.first()?;
1114+
// The smallest combination of profiles that moves it: for
1115+
// an unresolved one, to a version this pom does give.
1116+
let moved = d
1117+
.version_alternatives
1118+
.iter()
1119+
.find(|a| a.value != pinned && !(unresolved && a.value.contains("${")))?;
11121120
let declared = d.version_text.as_deref().unwrap_or_default();
11131121
let value = if !matches!(moved.by, crate::formats::maven::MovedBy::Profiles(_)) {
11141122
"resolve a version this reader cannot work out".to_string()
@@ -1118,9 +1126,14 @@ fn profile_override(
11181126
moved.value.clone()
11191127
};
11201128
return Some(format!(
1121-
"{POM}: {section}{named} <version>{declared}</version> resolves the \
1129+
"{POM}: {section}{named} <version>{declared}</version> {} the \
11221130
patched {pinned}, but the <properties> of {} make it {value}; whenever that \
11231131
applies Maven resolves it instead of the pin",
1132+
if unresolved {
1133+
"may resolve"
1134+
} else {
1135+
"resolves"
1136+
},
11241137
moved.by
11251138
));
11261139
}
@@ -3093,7 +3106,15 @@ mod tests {
30933106
/// version of the vendored GA off the vendored version.
30943107
#[tokio::test]
30953108
async fn vendored_ref_under_profile_properties_is_not_attested() {
3096-
for (version, attested) in [("1.10.1", false), ("1.10.0", true)] {
3109+
// `root` is the pom's own `<properties>`; none leaves `${ct}` to a
3110+
// parent pom this reader never sees, which may give the vendored
3111+
// version, while the profile's value is known.
3112+
for (root, version, attested) in [
3113+
("<properties><ct>1.10.0</ct></properties>", "1.10.1", false),
3114+
("<properties><ct>1.10.0</ct></properties>", "1.10.0", true),
3115+
("", "1.10.1", false),
3116+
("", "1.10.0", true),
3117+
] {
30973118
let p = vendored_project(UUID_A);
30983119
let wired = std::fs::read_to_string(p.root().join("pom.xml"))
30993120
.expect("read pom")
@@ -3102,7 +3123,7 @@ mod tests {
31023123
p.write(
31033124
"pom.xml",
31043125
format!(
3105-
"{}<properties><ct>1.10.0</ct></properties>\n<profiles><profile><id>p</id>\
3126+
"{}{root}\n<profiles><profile><id>p</id>\
31063127
<properties><ct>{version}</ct></properties></profile></profiles>\n{}",
31073128
&wired[..at],
31083129
&wired[at..]
@@ -3113,7 +3134,7 @@ mod tests {
31133134
assert_eq!(
31143135
attests(&out, V_PURL),
31153136
attested,
3116-
"{version}: {:?}",
3137+
"{root:?} {version}: {:?}",
31173138
out.unattested
31183139
);
31193140
}

0 commit comments

Comments
 (0)