Skip to content

Commit a8a10dc

Browse files
Fix maven.config user property parsing (#535, #550) (#1754)
* Start fix for #535, #550 Assisted-by: Claude Code:claude-opus-5-5 * Read maven.config properties the way Maven does Vendoring a Maven reactor reads user properties from .mvn/maven.config so an override there beats a pom <properties> value. The reader split the whole file on whitespace and kept only -Dk=v tokens, so it: - used properties from commented-out "#" lines, which Maven 3.9+ ignores, and - missed --define=k=v and "-D k=v" (or -D and k=v on separate lines), which Maven applies. Either way vendor could rewrite a ${prop} dependency to the patched base, silently downgrading the build (and vex then attested it), or refuse a patch that should apply. Comment lines are now skipped, every -D/--define spelling is read, and -Dk sets k=true as in Maven. Fixes #535, #550. Assisted-by: Claude Code:claude-opus-5-5 * Read maven.config one argument per line Maven 3.9+ reads each maven.config line as one argument. Follow that: the value of -Dk=v is the rest of the line (so "-Dv=1.0 # note" is not 1.0), a wholly quoted argument is unquoted, trimming matches Java's String.trim, and a bare -D never swallows the next option. Assisted-by: Claude Code:claude-opus-5-5 * Keep pre-3.9 multi-argument maven.config lines Maven before 3.9 splits .mvn/maven.config on whitespace, so a line like `-B -Dct.version=1.11.0` or `-Dfoo=1 -Dct.version=1.11.0` defines ct.version there. The old reader caught those tokens; the per-line reader dropped them, which let the planner rewrite a pom to the patched base while a Maven 3.6/3.8 build applies the override. Add any `-Dk=v` token from a whitespace split of the non-comment lines that the per-line reading did not define. Recognising an override that Maven 3.9 would not apply only makes the planner refuse to pin, so this stays fail-closed. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeRoPDBr4nQsvdEk3uV --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent a4ebd05 commit a8a10dc

1 file changed

Lines changed: 150 additions & 7 deletions

File tree

‎crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs‎

Lines changed: 150 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,7 @@ struct Reactor {
13991399
scope: Vec<String>,
14001400
/// pom → its local parent pom.
14011401
local_parent: BTreeMap<String, String>,
1402-
/// `-Dk=v` user properties from `.mvn/maven.config`.
1402+
/// User properties from `.mvn/maven.config` ([`cli_properties`]).
14031403
cli_props: BTreeMap<String, String>,
14041404
}
14051405

@@ -1956,13 +1956,66 @@ fn has_fallback_repository(doc: &Doc) -> bool {
19561956
})
19571957
}
19581958

1959-
/// `-Dk=v` tokens of a `maven.config`.
1959+
/// The user properties a `maven.config` defines, as Maven 3.9+ reads it:
1960+
/// one argument per line, `#` lines are comments (older Maven rejects
1961+
/// them), and a wholly quoted argument is unquoted. A property is
1962+
/// `-Dk=v`, `-D k=v`, `--define=k=v` or `--define k=v`, or a bare `-D` /
1963+
/// `--define` whose `k=v` is the next line. The value is the rest of the
1964+
/// argument, so `-Dv=1.0 # note` defines `1.0 # note`, as in Maven.
1965+
/// `-Dk` sets `k=true`; a later definition wins. Maven before 3.9 splits
1966+
/// the file on whitespace instead, so a `-Dk=v` token that only that reading
1967+
/// finds (`-B -Dk=v` on one line) is kept too, unless a line defines `k`.
19601968
fn cli_properties(config: &str) -> BTreeMap<String, String> {
1961-
config
1962-
.split_whitespace()
1963-
.filter_map(|t| t.strip_prefix("-D")?.split_once('='))
1964-
.map(|(k, v)| (k.to_string(), v.to_string()))
1965-
.collect()
1969+
// Java's `String.trim`: every char up to U+0020, nothing beyond.
1970+
fn trim(s: &str) -> &str {
1971+
s.trim_matches(|c: char| c <= ' ')
1972+
}
1973+
fn unquote(s: &str) -> &str {
1974+
s.strip_prefix('"')
1975+
.and_then(|s| s.strip_suffix('"'))
1976+
.map_or(s, trim)
1977+
}
1978+
let mut args = config
1979+
.lines()
1980+
.map(|line| unquote(trim(line)))
1981+
.filter(|arg| !arg.is_empty() && !arg.starts_with('#'))
1982+
.peekable();
1983+
let mut props = BTreeMap::new();
1984+
while let Some(arg) = args.next() {
1985+
let def = match arg.strip_prefix("--define") {
1986+
Some(rest) => rest
1987+
.strip_prefix('=')
1988+
.or_else(|| (rest.is_empty() || rest.starts_with(' ')).then_some(rest)),
1989+
None => arg.strip_prefix("-D"),
1990+
};
1991+
let Some(def) = def.map(trim) else {
1992+
continue;
1993+
};
1994+
let def = match def {
1995+
"" => match args.next_if(|next| !next.starts_with('-')) {
1996+
Some(next) => next,
1997+
None => continue,
1998+
},
1999+
def => def,
2000+
};
2001+
let (k, v) = def.split_once('=').unwrap_or((def, "true"));
2002+
if !k.is_empty() {
2003+
props.insert(k.to_string(), v.to_string());
2004+
}
2005+
}
2006+
let tokens = config
2007+
.lines()
2008+
.filter(|line| !trim(line).starts_with('#'))
2009+
.flat_map(str::split_whitespace)
2010+
.filter_map(|t| t.strip_prefix("-D")?.split_once('='));
2011+
let mut legacy = BTreeMap::new();
2012+
for (k, v) in tokens {
2013+
if !k.is_empty() && !props.contains_key(k) {
2014+
legacy.insert(k.to_string(), v.to_string());
2015+
}
2016+
}
2017+
props.extend(legacy);
2018+
props
19662019
}
19672020

19682021
// ── pom edits ───────────────────────────────────────────────────
@@ -3865,6 +3918,96 @@ mod tests {
38653918
assert_eq!(text(&after, "a/pom.xml"), a.replace("${ct.version}", SV));
38663919
}
38673920

3921+
/// A module at `${ct.version}` whose pom says 1.10.0 (the patch base),
3922+
/// vendored with `config` as `.mvn/maven.config`.
3923+
fn vendor_with_maven_config(config: &str) -> (JvmPlan, Fs, String) {
3924+
let root = ROOT.replace(
3925+
" <modules>",
3926+
" <properties>\n <ct.version>1.10.0</ct.version>\n </properties>\n <modules>",
3927+
);
3928+
let a = module("a", &dep("${ct.version}"));
3929+
let files = fs(&[
3930+
("pom.xml", &root),
3931+
("a/pom.xml", &a),
3932+
("b/pom.xml", &module("b", "")),
3933+
(".mvn/maven.config", config),
3934+
]);
3935+
let (plan, after) = vendor(&files);
3936+
(plan, after, a)
3937+
}
3938+
3939+
// #535: Maven applies `--define=k=v`, `-D k=v` and `-D` / `k=v` on
3940+
// separate lines, so the override must beat the pom's 1.10.0.
3941+
#[test]
3942+
fn maven_config_long_and_split_define_spellings_win() {
3943+
for config in [
3944+
"--define=ct.version=1.11.0\n",
3945+
"-D ct.version=1.11.0\n",
3946+
"-D\nct.version=1.11.0\n",
3947+
"--define\nct.version=1.11.0\n",
3948+
"-B\r\n--define=ct.version=1.11.0\r\n",
3949+
"\"-Dct.version=1.11.0\"\n",
3950+
// Maven 3.9+ reads the whole line as the value.
3951+
"-Dct.version=1.10.0 # TODO bump\n",
3952+
// Maven before 3.9 splits a line into several arguments.
3953+
"-B -Dct.version=1.11.0\n",
3954+
"-Dfoo=1 -Dct.version=1.11.0\n",
3955+
] {
3956+
let (plan, after, a) = vendor_with_maven_config(config);
3957+
assert_eq!(
3958+
reasons(&plan),
3959+
["conflicting_literal_version"],
3960+
"{config:?}"
3961+
);
3962+
assert_eq!(text(&after, "a/pom.xml"), a, "{config:?}");
3963+
}
3964+
}
3965+
3966+
// #550: since Maven 3.9 a `#` line is a comment and defines nothing.
3967+
#[test]
3968+
fn maven_config_comment_lines_define_nothing() {
3969+
for config in [
3970+
"# -Dct.version=1.11.0\n",
3971+
"# -Dct.version=1.11.0 old pin\n",
3972+
" # --define=ct.version=1.11.0\n",
3973+
"# just a note\n-B\n",
3974+
] {
3975+
let (plan, after, a) = vendor_with_maven_config(config);
3976+
assert!(plan.warnings.is_empty(), "{config:?}: {:?}", plan.warnings);
3977+
assert_eq!(
3978+
text(&after, "a/pom.xml"),
3979+
a.replace("${ct.version}", SV),
3980+
"{config:?}"
3981+
);
3982+
}
3983+
}
3984+
3985+
#[test]
3986+
fn maven_config_user_properties_parse_like_maven() {
3987+
let props = cli_properties(
3988+
"# -Dgone=1\n-Da=1\n--define=b=2\n-D c=3\n-D\nd=4\n--define e=5\n-Dflag\n -Da=6 \n\
3989+
\"-Dq=7\"\n-Dv=1.0 # note\n-D\n-Dw=8\n--defined=x\n-B\n-T 1C -Dy=9 -Dw=0\n",
3990+
);
3991+
let want = [
3992+
("a", "6"),
3993+
("b", "2"),
3994+
("c", "3"),
3995+
("d", "4"),
3996+
("e", "5"),
3997+
("flag", "true"),
3998+
("q", "7"),
3999+
("v", "1.0 # note"),
4000+
("w", "8"),
4001+
("y", "9"),
4002+
];
4003+
assert_eq!(
4004+
props,
4005+
want.iter()
4006+
.map(|(k, v)| (k.to_string(), v.to_string()))
4007+
.collect::<BTreeMap<_, _>>()
4008+
);
4009+
}
4010+
38684011
#[test]
38694012
fn project_version_expression_resolves() {
38704013
let root = ROOT.replace("<version>1-SNAPSHOT</version>", "<version>1.10.0</version>");

0 commit comments

Comments
 (0)