Repository navigation
fix(cargo): filter toolchain and global-prefix invocations - #5056
Conversation
KuSh
left a comment
There was a problem hiding this comment.
Round 1 — CHANGES REQUESTED
Claim: rtk cargo and the rewrite hook read a rustup +toolchain selector, cargo's global options and the built-in aliases before the subcommand through one cargo parser, built on a new resumable arg_tokenizer API with a Clap dialect, so those invocations get the same filters as the canonical forms (#3387).
Scope: accept if narrowed to "cargo +toolchain … and cargo's global options before a canonical subcommand name are rewritten and routed to the existing filters", with the prefix read on the existing tokenize_grammar and no new public API in arg_tokenizer. The rest is good work that should land as a stack (see Next) rather than in this PR. Maintainer PRs: #4401 declares tool grammars as Grammar/Flag tables in arg_tokenizer, which is the base the per-region cursor and the Clap axes should build on, so that part lands after it. #4010 reads --message-format from cargo's own region; it converges with this PR, and its two integration tests pass on this parser.
Ran: real cargo 1.98.1 + nightly 1.101 and cargo-nextest 0.9.143 on four crates (compile error; warnings with failing tests; a user [alias] config; a crate named shell), 53 command rows, develop vs head, each through rtk rewrite first; 1843 generated prefix argv diffed against real cargo's own parse; rtk err/rtk test help forms; install from a local path. Invariants: under the hook; split streams; every decision. Savings vs the 20% floor (CONTRIBUTING.md): +stable check 31%, +stable test 62%, +stable clippy 83%, +stable nextest run 54%, +stable build --release 37%.
Pins: 91 mutations over the decisions the diff adds; 17 survive the suite (P04 P05 P26 P32 P37 P44 P46 P47 P49; Q01 Q02 Q03 Q08 Q11 Q12 Q13 Q18), 3 more are equivalent.
Matrix: executed this round; pin check: executed this round
CI @e51bcf58: all 15 build and test jobs green (Linux, macOS, Windows msvc and gnu).
Blocks merge (3)
src/cmds/rust/cargo_cmd.rs:47,src/discover/rules.rs:95— spellings the hook newly routes reach a filter not built for their output (2 sites).b/c/t: a project's.cargo/config.tomlcan redefine them, and cargo honours it. Witht = "metadata --no-deps --format-version 1"andc = "tree", the hook now rewritescargo ttortk cargo t, which prints nothing with exit 0 (1199 bytes lost).cargo cprintscargo check (0 crates compiled)instead of the tree. Develop does not rewrite either.nextest: the new rule routes every operation, and develop's nextest filter prints nothing forcargo nextest list(458 bytes) orcargo nextest show-config test-groups(309). This head avoids that only through the operation model, which the narrowed PR does not keep.- Needed for merge: the built-in aliases are not routed, and the rule only adds what the narrowed PR's filters read correctly.
rtk rewrite "cargo t","cargo nextest list"and"cargo nextest show-config test-groups"must exit 1.
src/cmds/rust/cargo_cmd.rs:240— anhinside a value-taking short option's attached value is read as-h(regression vs develop). The command region declares no arity for-p/-F/-j…, so the Clap scan splits-pshellinto flags and itshsets help:rtk cargo build -pshellandrtk cargo test -pshellnow print cargo's raw output (389 and 1001 bytes); develop printed the summary (230 and 374).- Any crate or feature name containing an
hloses filtering. - Needed for merge: those two forms filter as on develop, while
cargo build -handcargo build --helpstill pass through.
- Behaviour the narrowed PR keeps has to be pinned by tests. In this head, 17 mutations pass the whole suite. In the narrowed PR, every decision below must fail some test when mutated, and the test must assert the child argv or the route taken, not the parser's return value:
+toolchainis recognised only as the first argument and stays in the child argv.- Each value-taking global (
--config,--color,-C,-Z) consumes its value in separate,=and attached spellings, socargo --config check metadatais passthrough, not check. - An undeclared option before the subcommand means passthrough with the argv unchanged.
--version,--listor--explaincombined with a subcommand means passthrough (P04/P05 survive today).--before the subcommand means passthrough.- The rewrite emits the original command verbatim.
--message-format=jsonstill selects the JSON filter for build, check and clippy (Q03/Q11/Q13 survive today). #4010'stests/cargo_double_dash_message_format_test.rspins the clippy half against a replayed cargo stream.
Filed as follow-ups
- #3838
exclude_commandsis matched on the raw command: the new cargo spellings escape it the waygit -Cdoes. Withexclude_commands = ["cargo test"],cargo +nightly testandcargo --locked testare rewritten. Absorbed by the existing issue, comment added. - #5063
rtk cargo test -- --nocapturekeeps onlytest result:and drops the panic message. Pre-existing since v1.2.0; filed. - #5064
rtk cargo build/check --message-format=shortreports a clean build and drops the warnings. Pre-existing since v1.2.0; filed.
Checked and correct — no need to re-verify
- Prefix parsing against real cargo: across 1843 generated argv (toolchain × 34 global-option forms × 14 commands, plus pairs), rtk never picked a different subcommand than cargo ran. Where cargo rejects the argv, rtk keeps cargo's error text and exit code.
+toolchain,--locked,--config k=v,-q,-v,--color neverand nightly-Zunstable-options -C dirroute to the same filtered output as the canonical command, andrtk gainrecords them.rtk cargo --help, barertk cargo, andrtk cargo test -- --nocapture(the--survives clap's flat trailing args).- Not regressions:
rtk err/rtk testwrapping cargo help forms print the same as on develop. - Gates:
cargo fmt --check,cargo clippy --all-targetsandcargo test --allpass on the head (5565 tests) and on the head merged with current develop (5567).
For the stacked PRs
The parts the narrowed claim cuts fix real develop bugs:
rtk cargo test -- --listprints nothing on develop;- so does
rtk cargo nextest list; install --message-format=jsondrops the compiler warning on develop.
Suggested split:
- This PR, narrowed (above).
- The resumable per-region cursor and the three Clap
Dialectaxes, stacked on #4401'sGrammartables. A region reads under aGrammarand returns the typed boundary (Positional/DashDash/Stopped). Meaning stays queries over tokens (token.is(&FLAG)) rather thanParameterParsercallbacks, and the two#[allow(dead_code)]helpers wait for a caller. - Cargo's harness, nextest and passthrough model on top of 2. Rows it has to pass, measured on this head:
cargo +nightly test -- -Zunstable-options --format json -qpasses through: today-qoverwrites the format and all libtest JSON is dropped (790 bytes become a recall hint). The-q --format jsonorder behaves the same way.cargo clippy -- -Vprints clippy's version; develop and this head printcargo clippy: No issues found.cargo test -- --list,cargo nextest listandinstall --message-format=jsonkeep this head's passthrough.- Each decision is pinned: P26, P32, P37, P44, P46, P47 and Q01, Q02, Q08, Q12, Q18 survive today.
- Help/version is decided in one place:
requests_usageand the runners' guards repeat it today, and theasks_for_usagecargo arm is unreachable, since every runner passes.unguarded_help().
Your questions
- None asked.
Next
Narrow to the toolchain and global-option routing: drop the b/c/t aliases and the nextest rule addition (or limit the rule to what the kept filters read), read the prefix with the existing tokenize_grammar, keep develop's command-region handling (blocker 2), and pin the decisions in blocker 3. The cursor, the Clap axes and the cargo model then go up as the stack described above.
Threads: 3 open (blockers 1 and 2, waiting on the narrowed push).
There was a problem hiding this comment.
Thanks @milyin, the narrowed version works well. I retested it through the hook on real projects with stable and nightly. Approving.
KuSh
left a comment
There was a problem hiding this comment.
Round 2 — APPROVED, follow-ups filed
Claim: cargo +toolchain … and cargo's global options before a canonical subcommand name are rewritten and routed to the existing filters, with the original argv forwarded unchanged (#3387).
Scope: accept if narrowed to the prefix routing (round 1, frozen). This head is that narrowing: it reads the prefix with the existing tokenize_grammar, adds no tokenizer API, does not route b/c/t, and adds no nextest rule. Maintainer PRs: checked against #4010, #5095, #5099, #5100, #5102 and #5103. Each merged with this head on current develop, both test sets pass.
Ran: real cargo 1.98.1 + nightly and cargo-nextest 0.9.143 on four crates (compile error; warnings with a failing test; a user [alias] config; a crate named shell), 69 command rows, develop vs head, each through rtk rewrite first. 1843 generated prefix argv diffed against real cargo's own parse. Exclusions under uv run and bash -c. Invariants: under the hook; split streams; every decision. Savings vs the 20% floor (CONTRIBUTING.md): +stable check 31%, +stable test 62%, +stable clippy 83%, +stable build --release 37%, +stable install 38%.
Pins: 23 decisions, 35 mutations. 30 are killed by the PR's tests. Of the 5 that survive, one is equivalent, one is covered by a second guard that is pinned, and three only change argv that cargo itself rejects.
Matrix: executed this round; pin check: executed this round
CI @a28a7b65: all 15 build and test jobs green (Linux, macOS, Windows msvc and gnu).
Round-1 blockers — all closed
- Newly routed spellings reaching the wrong filter:
rtk rewriteexits 1 forcargo t,cargo +stable t,cargo --locked c,cargo --color never b,cargo nextest run,cargo nextest listandcargo nextest show-config test-groups, as on develop. The PR's tests pin it inambiguous_prefixes_and_unsupported_commands_are_not_rewrittenandunknown_or_action_prefixes_and_aliases_pass_through_unchanged. -pshell:rtk cargo build -pshellandtest -pshellprint the summary again (230 and 373 bytes, the same as develop), with or without+stable --lockedin front.build -h,+stable build --helpand+stable test -- --helpstill pass through byte for byte. Pinned byattached_short_values_do_not_disable_filtering_but_help_still_does.- Pins: every decision listed in round 1 now fails a test when mutated, through the child argv or the route taken (mutations on the toolchain position, each value-taking global and its spellings, unknown options,
--version/--list/--explain/--help,--, the verbatim rewrite, the JSON branch for build, check and clippy, the leading--kept by clap).
Follow-ups
- #5141
rtk cargo test -- --format json(andjunit) drops the libtest report and the failing test's name. Pre-existing since v1.2.0 for the canonical spelling. This PR now also routes the usualcargo +nightly test -- -Zunstable-options --format jsonthere, so it belongs to the harness work in the stack from round 1; filed. - #3838: the wrapper exclusion checks still match the raw text, so with
exclude_commands = ["cargo test"],uv run cargo +stable testis rewritten tortk uv run …. Same result on develop; comment added.
Checked and correct — no need to re-verify
- Prefix parsing against real cargo: across 1843 generated argv, rtk never filtered as a different subcommand than cargo ran. Where cargo rejects the argv (
--offline=true,-qv,-Cor-Zon stable), rtk prints cargo's error and keeps its exit code. +toolchain,--locked,--config k=v,-q,-vand--colorroute to the same filtered output as the canonical command.--message-format=jsonafter a prefix reaches the JSON filter (262 bytes, the same as the canonical form).- Commands that stay unrewritten, as on develop:
--version,--list,metadata,run,nextest. - Gates on the head merged with current develop:
cargo fmt --check,cargo clippy --all-targets,cargo test --all.
Your questions
- None asked.
Next
Nothing — approving. The tokenizer cursor, Clap dialect and cargo harness model can go up as the stack described in round 1, and the libtest-format issue above is one of the rows the harness PR should pass.
Threads: 0 open, 3 resolved this round (blockers 1 and 2, fixed in 17f5fdd).
Keep the Cargo prefix rewrite from rtk-ai#5056 alongside the Python module rewrite. Both dispatch paths retain their existing behavior.
* fix(cargo): filter toolchain and global-prefix cargo invocations Ports upstream rtk-ai#5056 (v1.2.1): `cargo +nightly test`, `cargo --locked check` and other prefixed invocations now route to the cargo filters, and exclusions match the canonical subcommand. The fork keeps its two-reading resolve_word_text; the cargo classifier uses the plain reading upstream uses. * style: format the registry import list * fix(cargo): read the subcommand word as bash passes it when classifying * fix(cargo): keep upstream's plain word reading in the cargo classifier The ANSI-C quoted subcommand case passes with either reading, so the classifier stays as upstream wrote it; the case stays as coverage. * fix(hook): keep a prefixed excluded command excluded behind a routable wrapper uv run cargo +stable test with cargo test excluded was rewritten through the uv filter: the wrapper re-check tested only the raw inner text. It now also tests the inner command's tool form. The exclusion test checks each excluded command is rewritten without the exclusion, and the ANSI-C quoted case moves to its own assertion.
Cargo invocations such as
cargo +stable checkbypassed rewriting and compact output filtering. This fixes #3387 by locating the canonical command after rustup's selector and Cargo's global prefix, then routing it through the existing filters.tokenize_grammarAPI.This is the prefix-routing portion of the review split. Resumable tokenizer regions, a Clap dialect, and the full Cargo semantic model are deferred to separate work. Command-region message-format changes remain in #4010.
Validation
-pshellregression, and JSON stream filtering for build/check/clippy.cargo test --locked --all --no-fail-fast: 5,534 passed, 8 ignored across the full workspace, including all 24 tests incargo_toolchain_test,help_passthrough_fuzz_test, andrewrite_exit_code_test.cargo clippy --locked --all-targets: passed.cargo clippy --locked --workspace --all-targets: passed.cargo fmt --all -- --check: passed.bash scripts/check-test-presence.sh origin/develop: passed.These checks passed at
a28a7b652ac829a481301546f3e5d359d16afdc1, which includes the existing merge of develop8bc263a9, on Linux with Rust 1.93.0. The full suite used Git 2.43.7 and jq 1.8.1 from a temporary tools directory on PATH: the system Git 2.34.1 lacks options exercised by existing Git log tests, and jq was initially absent. No Cargo debug-info, incremental-compilation, or codegen-unit overrides were needed.GitHub CI for this head remains
action_required, pending maintainer approval.