Skip to content

fix(mvn): read goals through arg_tokenizer, not a dash scan - #4006

Merged
KuSh merged 1 commit into
rtk-ai:developfrom
KuSh:feat/mvn-arg-tokenizer
Oct 6, 2026
Merged

KuSh merged 1 commit into
rtk-ai:developfrom
KuSh:feat/mvn-arg-tokenizer

Conversation

@KuSh

@KuSh KuSh commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

The bug

detect_phase picked the Maven goal as the last argument not starting with -:

args.iter().filter(|a| !a.starts_with('-')).map(|s| s.as_str()).next_back()

It has no notion of an option consuming the next token, so any option written with a separate value after the goal hid the goal behind its own value:

invocation goal read as route before route after
mvn test -f pom.xml pom.xml Passthrough Test
mvn test -pl core core Passthrough Test
mvn verify -P prod prod Passthrough Package
mvn clean test -T 1C 1C Passthrough Test
mvn install -l build.log build.log Passthrough Package
mvn -D test test Test Passthrough

Verified against Maven 3.9.16, not from memory:

docker run --rm -e LC_ALL=C -v "$PWD:/w" -w /w maven:3-eclipse-temurin-21 mvn -B test -f pom.xml

runs Surefire and emits the standard T E S T S / BUILD SUCCESS shape, so -f's value really is a separate token and the goal really is test. End to end through a shim, rtk mvn -B -o test -f pom.xml went from forwarding all 1845 raw bytes to ~370.

Each of -pl, -rf, -P, -s, -T and -l was checked the same way against the real binary: given a bogus value, Maven reports it as that option's value (Could not find the selected project in the reactor: nosuchmodule, The specified user settings file does not exist, …) and never as an unknown lifecycle phase.

The fix

Goal detection is rebuilt on tokenize_grammar + Token::is_free_positional, behind an mvn_takes_value(kind, name) -> Option<ValueSpec> predicate transcribed from mvn --help (3.9.16) — every option the table marks <arg>, in both its short and long spelling.

Dialect: Dialect::CommonsCli (new in #4008). Maven's CLI is Apache commons-cli — /usr/share/maven/lib/ ships commons-cli-1.11.0.jar — and the preset states that grammar directly: POSIX, except SingleDash::Atomic, because the library's short options are whole multi-character words (-pl, -gs, -amd, -ntp) that do not cluster. Case stays sensitive, so -b/-B and -t/-T stay distinct, and -- ends option parsing, measured:

mvn -B -o -- -X validate
[ERROR] Unknown lifecycle phase "-X". ...

Because the preset says all three things, this module no longer compensates for any of them: flag lookups are plain arg_tokenizer::has_flag, and goals() is one filter(is_free_positional) over the whole token vector, with nothing re-read past the -- boundary.

SingleDash::Atomic tags -pl as TokenKind::Long, not Short — Maven's own --help calls it a short option, and a predicate keyed on Short would silently stop -pl claiming its value. mvn_takes_value matches on the name alone and never reads the kind.

The preset does not model commons-cli's Java-property options: -DskipTests=true tokenizes as the flag DskipTests, so has_flag(…, "D") is false for that spelling. Nothing here reads -D's value — goal detection only needs "this is not a positional", which holds — and the separate spelling mvn -D test still matches the D entry in the table and swallows test, which is why that entry stays.

Routing moves into an MvnRoute enum, so the verbose / quiet / phase decision is one pure function of the args and every regression test asserts on the resolved route rather than on a predicate.

rtk mvnd routes through this same module and shares all of it; a test pins that, and that child argv is forwarded untouched for both binaries.

Regression tests

Each was proven red against the previous logic and green after:

  • option_value_after_the_goal_is_not_the_goal — 7 invocations (-f, -pl, -P, -T, -l, -s, -rf)
  • separate_define_value_is_not_a_goal — mvn -D test defines a property, leaving no goal
  • multi_char_short_options_do_not_cluster — -pl core test yields the single goal test
  • everything_after_double_dash_is_a_goal
  • mvnd_shares_argument_handling_and_argv

Plus attached_option_values_do_not_swallow_the_goal, verbose_and_quiet_options_pick_their_route and encrypt_options_are_not_the_errors_flag as guards.

quiet_does_not_match_unrelated_flags asserted !is_quiet(["-quiet", "test"]). That assertion was wrong: mvn -B -o -quiet validate runs quiet against a real Maven, because commons-cli accepts the single-dash spelling of a long option. It is now quiet_detects_single_dash_long_flag, with --QUIET taking its place as the negative case.

Left out

mvn_cmd does not call args_utils::restore_double_dash, and this PR does not add it. Measured with a mvn shim on PATH: clap consumes the user's -- only when it is the first token of the trailing region, so rtk mvn -- test reaches Maven as [test] and rtk mvn test -- foo as [test] [--] [foo]. For Maven the dropped leading -- is inert — it has no forwarding convention, and post--- arguments are just goals — except for rtk mvn -- -X, where dropping it turns a Maven "unknown lifecycle phase" error into a real debug run. That is a fidelity gap worth its own change, not this one; goal detection here already honours any -- that does survive.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working parser rust maven labels Sep 12, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

This PR rewrites Maven goal detection in mvn_cmd.rs to use a proper argument tokenizer (arg_tokenizer with an Msbuild dialect) instead of a naive scan for the last non-dash token, fixing cases where option values (e.g. -f pom.xml, -P prod, -D test) were mistakenly read as the goal and misrouted output filtering. Routing logic is consolidated into an MvnRoute enum, and extensive regression tests are added covering separate-token option values, -- handling, and quiet/verbose detection.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh
KuSh marked this pull request as draft September 12, 2026 22:22
@KuSh
KuSh force-pushed the feat/mvn-arg-tokenizer branch 2 times, most recently from 05b14e2 to 14db625 Compare September 18, 2026 21:09
@KuSh
KuSh marked this pull request as ready for review September 18, 2026 21:10
@KuSh
KuSh changed the base branch from develop to feat/tokenizer-dialect-axes September 18, 2026 21:15

@aeppling aeppling left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@KuSh
KuSh force-pushed the feat/mvn-arg-tokenizer branch from 14db625 to 4ec6756 Compare October 6, 2026 22:18
@CLAassistant

CLAassistant commented Oct 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@KuSh
KuSh force-pushed the feat/mvn-arg-tokenizer branch from 4ec6756 to 14db625 Compare October 6, 2026 22:23
@KuSh
KuSh force-pushed the feat/tokenizer-dialect-axes branch from a41cc10 to cd9d7a6 Compare October 6, 2026 22:35
@KuSh
KuSh force-pushed the feat/mvn-arg-tokenizer branch 2 times, most recently from 7e58c55 to 1d3c809 Compare October 6, 2026 22:49
@KuSh
KuSh changed the base branch from feat/tokenizer-dialect-axes to develop October 6, 2026 22:50
`detect_phase` took the last argument not starting with `-` as the Maven goal,
so any option whose value is a separate token hid the goal behind it: `mvn test
-f pom.xml`, `mvn test -pl core`, `mvn verify -P prod` and `mvn install -l
build.log` all resolved to Passthrough and ran unfiltered.

Goal detection now comes from `tokenize_grammar` + `is_free_positional` under a
`mvn_takes_value` predicate transcribed from `mvn --help` (3.9.16), read under
`Dialect::CommonsCli`. Maven's CLI is Apache commons-cli, whose short options
are whole multi-character words (`-pl`, `-gs`, `-amd`) that never cluster and
whose names are case-sensitive, and which ends option parsing at `--` (verified:
`mvn -- -X validate` reports `-X` as an unknown lifecycle phase), so the module
states its grammar instead of correcting one.

Routing moves into an `MvnRoute` enum so the verbose, quiet and phase decisions
are one pure function over the args, testable without running Maven. `rtk mvnd`
shares it, and is covered.

Co-Authored-By: Claude Opus 5 <[email protected]>
@KuSh
KuSh force-pushed the feat/mvn-arg-tokenizer branch from 1d3c809 to 6f7de39 Compare October 6, 2026 22:54
@KuSh
KuSh merged commit ef53d65 into rtk-ai:develop Oct 6, 2026
11 checks passed
@KuSh
KuSh deleted the feat/mvn-arg-tokenizer branch October 6, 2026 23:01
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working maven parser rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants