Repository navigation
fix(gh): tokenize gh args instead of walking them by hand - #4000
Conversation
📊 Automated PR Analysis
SummaryReplaces a hand-rolled, incomplete flag-walking heuristic in gh_cmd.rs with a proper tokenizer-based approach that extracts the PR/issue/run identifier from gh subcommand args. Fixes cases where value-taking flags (e.g. -i/--interval, -j/--job, --add-label) had their values misidentified as the PR/issue number, and restores '--' handling that clap had stripped before gh_cmd saw the args. Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
36e1bb4 to
86ac6e3
Compare
aeppling
left a comment
There was a problem hiding this comment.
Sorry, missed this on first check.
tests/gh_double_dash_test.rs spawns the binary through CARGO_BIN_EXE_rtk directly. This branch forked before the tests_that_spawn_rtk_isolate_their_data scan landed on develop (e0d99ab), so CI is green here — but the suite fails once merged, same as what #4005 hit (repaired in #4460).
Repro:
git checkout develop # df39e33d
git merge <this branch>
cargo test tests_that_spawn_rtk_isolate_their_data
panicked at src/core/test_isolation/mod.rs:302:
tests/gh_double_dash_test.rs: CARGO_BIN_EXE_rtk
Fix, same shape as #4460's glab change: mod common; + common::rtk_command() in gh_argv, and drop the HOME / XDG_CONFIG_HOME / RTK_DB_PATH envs (rtk_command() pins those plus the tee/recall paths and telemetry).
Verified locally: with that change, this branch merged onto develop + #4460 passes cargo test --all.
86ac6e3 to
ed1c416
Compare
|
@aeppling fixed |
`extract_identifier_and_extra_args` picked the PR/issue/run identifier with a
hand-rolled walker: a hardcoded eight-entry list of value-taking flags, a
`skip_next` bool, and `arg.starts_with('-')`. Anything the list missed had its
value read as the identifier, and `pr_action` did not consult the list at all.
Verified against gh 2.46.0 and 2.100.0:
rtk gh pr checks --interval 30 -> gh pr checks 30 --interval
rtk gh run view -j 12345 -> gh run view 12345 -j
rtk gh pr edit --add-label bug -> "ok edited #bug"
Replace it with `arg_tokenizer::tokenize_grammar` plus one `takes_value`
predicate per gh subcommand, transcribed from each subcommand's own `--help`,
and take the first free positional. `pr_action` now uses the same tokenization,
and `run()` restores the `--` that clap's `trailing_var_arg` consumes so gh
receives the boundary the user typed.
Co-Authored-By: Claude Opus 5 <[email protected]>
Co-Authored-By: Claude Opus 5 <[email protected]>
`trailing_var_arg` only ever lets clap strip one `--`, the one at the head of the region it hands to `gh` — before the subcommand, or right after it. That one is rtk's option terminator and was never part of the gh command line, so restoring it fabricated an argument: `gh -- pr view 42` is `unknown command "pr" for "gh"` and `gh api -- repos/o/r --jq .name` is `accepts 1 arg(s), received 3` on gh 2.46.0 and 2.100.0 alike. Every later `--` clap passes through untouched and still reaches gh verbatim. Rebuilding the region before the drop keeps the subcommand from being reinjected, so `gh -- pr view 42` runs `gh pr view 42`, not `gh pr pr view 42`. Co-Authored-By: Claude Opus 5 <[email protected]>
`split_identifier` reads positionals past `--` because gh does too (`gh pr view -- 42` views PR 42), but the callers re-emit the identifier ahead of the boundary. When the escaped region held more than the identifier, that promoted a flag the user had escaped back into flag position: `rtk gh pr checks -- --watch 42` ran gh with `--watch` live, blocking on a watch the user had asked gh to treat as a positional. gh takes at most one positional for these subcommands, so an escaped region holding more than one token is something gh rejects whatever rtk sends. Look past the boundary only while it escapes a single token, and hand the rest to gh untouched. Also isolate HOME and RTK_DB_PATH in the argv test, which otherwise read the developer's own config and wrote to their real tracking database. Co-Authored-By: Claude Opus 5 <[email protected]>
Refusing to look past `--` only when two or more tokens trailed it still unescaped the single-token case, which is the one a user is most likely to type: `rtk gh pr checks -- --watch` hoisted `--watch` into flag position and ran the blocking watch the user had escaped, where develop passed the command through untouched. `gh issue view -- --web` and `gh run view -- --log` went the same way. What makes hoisting safe is not how much the boundary escapes but whether unescaping it is a no-op: reach past `--` only for one token that is still read as a positional without it, so `gh pr view -- 42` keeps gaining `--json`. gh reads everything past the boundary as a positional -- `gh pr view -R o/r -- --web` resolves `--web` as the PR selector -- so a flag left there is a flag gh never acts on. Co-Authored-By: Claude Opus 5 <[email protected]>
`dirs::config_dir()` prefers `XDG_CONFIG_HOME` over `HOME`, so on a machine that sets it the argv assertions still ran against the developer's real `~/.config/rtk`. Co-Authored-By: Claude Opus 5 <[email protected]>
The `--` tests started rtk from CARGO_BIN_EXE_rtk and pointed HOME, XDG_CONFIG_HOME and RTK_DB_PATH at a tempdir by hand. A child spawned that way is not covered by the isolation guard, which fails the suite for any test file that names the binary directly, because it also writes the tee spool and recall database under the developer's real data directory. `common::rtk_command()` pins all of those, plus telemetry, so the manual settings and the comment justifying them go. The PATH that puts the argv-recording `gh` stub first is not covered by it and stays. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
ed1c416 to
5033633
Compare
The bug
extract_identifier_and_extra_argsinsrc/cmds/git/gh_cmd.rspicked the PR/issue/runidentifier out of the args with a hand-rolled walker: a hardcoded eight-entry
flags_with_valuearray (-R --repo -q --jq -t --template --job --attempt), askip_nextbool, and
arg.starts_with('-').Three defects fell out of that:
read as the identifier.
gh pr checks's-i/--intervalwas missing entirely, and only thelong spellings of
--job/--attemptwere listed, not-j/-a.pr_action(gh pr comment/gh pr edit) did not consult the list at all — a bare.find(|a| !a.starts_with('-'))— so the first flag value became the PR number in theconfirmation line.
--awareness anywhere, and clap'strailing_var_arghad already eaten theseparator before gh_cmd saw the args, so it never reached gh either.
Reproducer
Verified against
gh2.46.0 (Debian) and 2.100.0 (container),LC_ALL=C, with aghshim onPATHthat echoes its argv:After the change all of these keep the flag and its value together, and the identifier stays
empty so gh resolves the current branch's PR:
What changed
flags_with_valueare gone.split_identifiercallsarg_tokenizer::tokenize_grammarand takes the firstToken::is_free_positional, thenrebuilds
extraby excluding that token'ssource_index— detect and act with one rule.takes_valuepredicate per gh subcommand, each transcribed from that subcommand'sown
--help:view_takes_value(gh pr view/gh issue view, whose flag sets arebyte-identical),
pr_checks_takes_value,run_view_takes_value,pr_comment_takes_value,pr_edit_takes_value. The two that build onview_takes_valueare verified strict supersetsof it;
pr commentandpr editget their own tables. Each table is the union over gh 2.46(still distro-shipped) and 2.100, since gh keeps adding flags here —
gh pr checksgained-q/--jq,--jsonand-t/--templatebetween the two, andgh pr comment/gh pr editgained
--attach.ValueSpecisvalue().claiming_dash_dash(): verified that gh swallows a following literal--as a flag value (gh run view --job -- 1queries job"--"), and that a value-takingshorthand is legal inside a cluster (
gh run view -vj 1works, as does-j1).pr_actiontakes the same predicate from its call site, sogh pr commentandgh pr editeach get their own grammar rather than sharing one.
run()reassembles[subcommand] + argsbefore callingargs_utils::restore_double_dash:clap carves the
subcommandpositional out of the same trailing region, so restoring overargsalone lands the--one token off and duplicates the subcommand (rtk gh -- pr view 42ran
gh pr pr view 42). The restored--is then dropped, becausetrailing_var_argonlyever lets clap strip one: the one at the head of that region, before the subcommand or right
after it. That one is rtk's own option terminator, not something the user handed gh. Every
later
--clap passes through untouched and still reaches gh verbatim, andghkeeps readingpositionals past the boundary (
gh pr view -- 42views PR 42), so the identifier searchdeliberately is not scoped to
before_dashdash.Behaviour change
No working invocation changes against
develop. The child argv is byte-identical todevelopon every--spellingmeasured with a
ghshim, includingrtk gh -- pr view 42,rtk gh api -- repos/o/r,rtk gh api -- repos/o/r --jq .name,rtk gh pr view -- 42,rtk gh pr list -- --state open,and the two-
--forms. What changed is only the identifier extraction listed above.Dropping the terminator rather than forwarding it is what real gh requires. On gh 2.46.0 and
2.100.0 alike,
gh -- pr view 42isunknown command "pr" for "gh"whilegh pr view 42works, and
gh api -- repos/cli/cli --jq .nameisaccepts 1 arg(s), received 3whilegh api repos/cli/cli --jq .nameprintscli— gh reads everything past--as a positional.The identifier search reaches past a
--the user typed, because gh does too (gh pr view -- 42views PR 42), but only where pulling the identifier forward is a no-op: the boundary escapes
exactly one token, and that token is still read as a positional once unescaped. Otherwise hoisting
the identifier in front of the boundary would promote whatever else trailed it back into flag
position —
rtk gh pr checks -- --watchran gh with--watchlive and blocked on a watch the userhad escaped, and
gh issue view -- --web/gh run view -- --logwent the same way. gh readseverything past the boundary as a positional (
gh pr view -R o/r -- --webresolves--webas thePR selector) and accepts at most one of them here, so an escaped region rtk leaves alone is one gh
rejects on arity whatever rtk sends. The one argv that differs from
developas a result is thecrowded case,
rtk gh pr checks -- --watch 42(developreorders it topr checks 42 -- --watch);real gh rejects both spellings with
accepts at most 1 arg(s), received 2.Pre-existing and untouched:
rtk gh pr view -- 42still forwards a dangling trailing--afterrtk's injected
--json, byte-identical todevelop. Also pre-existing and byte-identical todevelop:rtk gh repo view -- o/rappends rtk's--jsonpast the boundary (run_repoappendsrather than splicing at
injection_point), andrtk gh pr -- listruns the filtered list wherereal gh would say
unknown command "list" for "gh pr"— clap consumes that--as rtk's ownterminator and rtk cannot tell it apart from one aimed at gh.
Tests
Nine new cases in the module's
#[cfg(test)] mod tests, plus the existing identifier testsmigrated to the new entry point.
tests/gh_double_dash_test.rsasserts the child argv for nine--positions: dropped before the subcommand, dropped after it (bare and before a followingflag), preserved interior on both a passthrough (
gh api) and a filtered (gh pr list)subcommand, an escaped flag kept behind the boundary whether it is alone there or
not, on both the
pr checksandrun viewgrammars, and a lone escaped identifier still hoistedso rtk can inject
--json. The six drop/escape cases were confirmed red before their fix andgreen after; the preservation and hoisting cases stay green throughout.
Five of the identifier cases were confirmed red against the old walker and green after:
pr checks -i/--interval,run view -j/-a, the-vjcluster,pr edit --add-label, andpr comment -b.