Skip to content

fix(cargo): filter toolchain and global-prefix invocations - #5056

Merged
KuSh merged 47 commits into
rtk-ai:developfrom
milyin:fix/cargo-toolchain-filtering
Nov 17, 2026
Merged

KuSh merged 47 commits into
rtk-ai:developfrom
milyin:fix/cargo-toolchain-filtering

Conversation

@milyin

@milyin milyin commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Cargo invocations such as cargo +stable check bypassed 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.

  • Share prefix discovery between execution and rewrite classification using the existing tokenize_grammar API.
  • Forward the original OS arguments, including toolchain selectors, global options, separators, empty values, and non-UTF-8 values. Preserve the original shell text when rewriting and include all arguments in verbose output and tracking.
  • Leave root actions, unknown prefixes, and configurable aliases to Cargo. Keep the existing rewrite command set; nextest receives no new rewrite rule.
  • Honor Cargo command exclusions across toolchain/global prefixes, preserving raw argument quoting and spacing for anchored patterns. This fixes the Cargo prefix case of exclude_commands is matched on the raw command, so git -C <path> escapes it #3838.
  • Require a shell word boundary after the cargo executable name when rewriting.

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

  • Integration tests assert child argv, filtering decisions, stdout/stderr, exit status, verbatim rewriting, and tracking. They cover every prefix decision requested in review, the -pshell regression, 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 in cargo_toolchain_test, help_passthrough_fuzz_test, and rewrite_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.
  • Earlier manual validation: nine comparisons against Cargo 1.97.1 in a temporary crate, covering toolchain/global prefixes, attached package values, and help.

These checks passed at a28a7b652ac829a481301546f3e5d359d16afdc1, which includes the existing merge of develop 8bc263a9, 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.

milyin added 30 commits November 6, 2026 19:17
@CLAassistant

CLAassistant commented Nov 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@milyin milyin changed the title Autotitative single cargo arguments parser. Fix/cargo toolchain filtering Autoritative single cargo arguments parser. Fix/cargo toolchain filtering Nov 10, 2026

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

  1. 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.toml can redefine them, and cargo honours it. With t = "metadata --no-deps --format-version 1" and c = "tree", the hook now rewrites cargo t to rtk cargo t, which prints nothing with exit 0 (1199 bytes lost). cargo c prints cargo 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 for cargo nextest list (458 bytes) or cargo 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.
  2. src/cmds/rust/cargo_cmd.rs:240 — an h inside 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 -pshell into flags and its h sets help:
    • rtk cargo build -pshell and rtk cargo test -pshell now print cargo's raw output (389 and 1001 bytes); develop printed the summary (230 and 374).
    • Any crate or feature name containing an h loses filtering.
    • Needed for merge: those two forms filter as on develop, while cargo build -h and cargo build --help still pass through.
  3. 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:
    • +toolchain is 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, so cargo --config check metadata is passthrough, not check.
    • An undeclared option before the subcommand means passthrough with the argv unchanged.
    • --version, --list or --explain combined with a subcommand means passthrough (P04/P05 survive today).
    • -- before the subcommand means passthrough.
    • The rewrite emits the original command verbatim.
    • --message-format=json still selects the JSON filter for build, check and clippy (Q03/Q11/Q13 survive today). #4010's tests/cargo_double_dash_message_format_test.rs pins the clippy half against a replayed cargo stream.

Filed as follow-ups

  • #3838 exclude_commands is matched on the raw command: the new cargo spellings escape it the way git -C does. With exclude_commands = ["cargo test"], cargo +nightly test and cargo --locked test are rewritten. Absorbed by the existing issue, comment added.
  • #5063 rtk cargo test -- --nocapture keeps only test result: and drops the panic message. Pre-existing since v1.2.0; filed.
  • #5064 rtk cargo build/check --message-format=short reports 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 never and nightly -Zunstable-options -C dir route to the same filtered output as the canonical command, and rtk gain records them.
  • rtk cargo --help, bare rtk cargo, and rtk cargo test -- --nocapture (the -- survives clap's flat trailing args).
  • Not regressions: rtk err/rtk test wrapping cargo help forms print the same as on develop.
  • Gates: cargo fmt --check, cargo clippy --all-targets and cargo test --all pass 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 -- --list prints nothing on develop;
  • so does rtk cargo nextest list;
  • install --message-format=json drops the compiler warning on develop.

Suggested split:

  1. This PR, narrowed (above).
  2. The resumable per-region cursor and the three Clap Dialect axes, stacked on #4401's Grammar tables. A region reads under a Grammar and returns the typed boundary (Positional/DashDash/Stopped). Meaning stays queries over tokens (token.is(&FLAG)) rather than ParameterParser callbacks, and the two #[allow(dead_code)] helpers wait for a caller.
  3. 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 -q passes through: today -q overwrites the format and all libtest JSON is dropped (790 bytes become a recall hint). The -q --format json order behaves the same way.
    • cargo clippy -- -V prints clippy's version; develop and this head print cargo clippy: No issues found.
    • cargo test -- --list, cargo nextest list and install --message-format=json keep 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_usage and the runners' guards repeat it today, and the asks_for_usage cargo 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).

Comment thread src/cmds/rust/cargo_cmd.rs Resolved
Comment thread src/discover/rules.rs Resolved
Comment thread src/cmds/rust/cargo_cmd.rs Resolved
@milyin
milyin marked this pull request as draft November 10, 2026 16:10
@milyin milyin changed the title Autoritative single cargo arguments parser. Fix/cargo toolchain filtering fix(cargo): filter toolchain and global-prefix invocations Nov 10, 2026

@pszymkowiak pszymkowiak left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @milyin, the narrowed version works well. I retested it through the hook on real projects with stable and nightly. Approving.

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

  1. Newly routed spellings reaching the wrong filter: rtk rewrite exits 1 for cargo t, cargo +stable t, cargo --locked c, cargo --color never b, cargo nextest run, cargo nextest list and cargo nextest show-config test-groups, as on develop. The PR's tests pin it in ambiguous_prefixes_and_unsupported_commands_are_not_rewritten and unknown_or_action_prefixes_and_aliases_pass_through_unchanged.
  2. -pshell: rtk cargo build -pshell and test -pshell print the summary again (230 and 373 bytes, the same as develop), with or without +stable --locked in front. build -h, +stable build --help and +stable test -- --help still pass through byte for byte. Pinned by attached_short_values_do_not_disable_filtering_but_help_still_does.
  3. 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 (and junit) 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 usual cargo +nightly test -- -Zunstable-options --format json there, 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 test is rewritten to rtk 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, -C or -Z on stable), rtk prints cargo's error and keeps its exit code.
  • +toolchain, --locked, --config k=v, -q, -v and --color route to the same filtered output as the canonical command. --message-format=json after 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).

@KuSh
KuSh merged commit 6c94fc5 into rtk-ai:develop Nov 17, 2026
17 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Nov 17, 2026
Shotsuke added a commit to Shotsuke/rtk that referenced this pull request Nov 17, 2026
Keep the Cargo prefix rewrite from rtk-ai#5056 alongside the Python module rewrite. Both dispatch paths retain their existing behavior.
yungSNAT added a commit to yungSNAT/PascalsLetter that referenced this pull request Nov 23, 2026
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cargo +toolchain commands are not rewritten or filtered

4 participants