Skip to content

refactor(discover): parse golangci-lint through arg_tokenizer - #3998

Merged
KuSh merged 6 commits into
rtk-ai:developfrom
KuSh:refactor/golangci-discover-tokenizer
Nov 18, 2026
Merged

KuSh merged 6 commits into
rtk-ai:developfrom
KuSh:refactor/golangci-discover-tokenizer

Conversation

@KuSh

@KuSh KuSh commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

What

discover::registry::parse_golangci_run_parts walked the words of a raw shell command by hand to find the run subcommand: starts_with('-'), a local --flag=value split (split_golangci_flag_name), a local "does this flag take a separate value" rule (golangci_flag_takes_separate_value), and GOLANGCI_GLOBAL_OPT_WITH_VALUE — a second copy of the flag names cmds::go::golangci_cmd::global_takes_value already tracks. All four collapse into one tokenize_grammar call.

Why the shell string was not an obstacle

The belief was that the tokenizer could not serve this caller, because it slices back into the original string (cmd[tokens[i].1..]) and Token::source_index is an args-slice index, not a byte offset. Two facts undo that:

  • split_token_spans was a one-line wrapper over discover::lexer, which already returns Vec<(&str, usize)> — word text plus byte offset. (With it gone, words_and_spans also takes over discover::without_rtk_disabled, the only other place that wanted the same pairing, and coalesce_words drops to private.)
  • Token::source_index indexes the words handed in, so spans[token.source_index] is the byte offset.

So the layering is: shell string → lexer (words + spans) → arg_tokenizer (grammar) → source_index back down to a byte offset. lexer::words_and_spans now names that pairing so it doesn't get rediscovered, and src/core/README.md's tokenizer section states it (including the discover::lexer::TokenKind / arg_tokenizer::TokenKind name collision).

The lexer's words are not an argv, and the docs now say so. coalesce_words returns slices of the original command string, so a word keeps its quote characters and backslash escapes: --config "a path/x.yml" yields the word "a path/x.yml" with the quotes in it, and --config=a\ b stays escaped. parse_golangci_run_parts is unaffected — it never reads a value, only compares against run and re-slices cmd by offset — but a caller who reads a --config path out of Token::value() would get a path with literal quotes and silently look up the wrong file. The README paragraph and the words_and_spans rustdoc now name the words unresolved and point at lexer::resolve_word_text (made pub(crate), so the advice is followable); shell_split resolves but discards the offsets. test_words_and_spans_keeps_words_unresolved pins the shape.

Keyword comparison has the same hazard in a milder form, and the docs draw the line: golangci-lint "run" ./... yields the word "run", which equals no keyword, so parse_golangci_run_parts returns None and the command falls through to passthrough — unfiltered but correct, which is what makes it tolerable here. Pre-existing (the deleted scan compared raw words too), now pinned by test_classify_golangci_lint_quoted_subcommand_is_not_rewritten.

Deduping the flag list

golangci_cmd::global_takes_value becomes pub(crate) (the only change in that file — no behaviour change) and is now the single source for which golangci-lint global flags take a value. The registry wraps it to add one thing the shell-string caller needs, documented at the wrapper.

The table stays with its command rather than moving into core/: core/ holds no per-tool flag table — every Option<ValueSpec> grammar in the tree lives with its command — and global_takes_value is defined against run_takes_value in the same file, which delegates to it as a strict superset. So discover imports it from crate::cmds::go::golangci_cmd, and the import carries a short note saying why.

Behaviour

Bit-identical. The new test_golangci_run_parts_grammar table was first run as a differential probe against the code it replaces — old and new implementations side by side over all 26 cases: --config=a.yml, -c a.yml, --config "a path/x.yml", --config *.yml, -cfoo.yml, the -vc a.yml cluster, --trace-path/--mem-profile-path/--cpu-profile-path, the bare -, the -- early return, golangci as the binary name, and non-matches (go build ./..., golangci-lint version, golangci-lint fmt). The probe is not in the diff; its cases are, as asserted expectations.

One input nearly diverged, and the tokenizer's default was the less correct one: golangci-lint --config -- run ./.... The old scan let --config swallow the -- as its value; ValueSpec::value() refuses a not-yet-seen --, which would have made this a non-match. Checked against the real binary rather than assumed:

$ LC_ALL=C docker run --rm golangci/golangci-lint:v2.13.1 \
    golangci-lint --config -- run ./...
Error: can't load config: can't read viper config: open --: no such file or directory

pflag reads -- as the config path, so the old behaviour was right and the grammar claims it (ValueSpec::claiming_dash_dash). Test case pinned.

Follow-up (not in this PR)

golangci_cmd::find_subcommand_index has the same --config -- gap on the argv side — it returns None there and falls through to passthrough, so the user still gets correct (just unfiltered) output. Fixing it is a behaviour change in a file this PR is scoped out of.

Out of scope by design: the other string scans in registry.rs (extract_base_command, rewrite_segment_inner, wrapper_inner_command, search_uses_pattern_file).

Verification

cargo fmt --all -- --check and cargo clippy --all-targets -- -D warnings are clean. cargo test --all --no-fail-fast passes in full.

Behaviour was also checked against the binary built from develop, which still has the scan this replaces: rtk rewrite over the 26 grammar cases and over the same commands with trailing redirects (2>&1, >/dev/null, &>/dev/null, ${PKG}2>/dev/null, a pipeline, an env prefix) is byte-identical between the two binaries, as are rtk hook claude with allow/deny rules and rtk discover --all over a planted session.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added documentation Improvements or additions to documentation refactor tests discover labels Sep 12, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

♻️ Type refactor
🟢 Risk low

Summary

This PR refactors golangci-lint command parsing in discover::registry to use the shared arg_tokenizer/grammar machinery instead of a hand-rolled shell-word scanner, deduplicating the golangci global-flag list by reusing golangci_cmd::global_takes_value. It also documents the shell-string-to-tokenizer layering in src/core/README.md and adds a differential test pinning behavior identical to the old implementation, including one corrected edge case (--config --) verified against the real golangc…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

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

@KuSh
KuSh force-pushed the refactor/golangci-discover-tokenizer branch from 8b5e55b to 55a6a37 Compare September 17, 2026 22:38
@KuSh
KuSh force-pushed the refactor/golangci-discover-tokenizer branch from 55a6a37 to 68bfea8 Compare September 28, 2026 01:05
@KuSh
KuSh force-pushed the refactor/golangci-discover-tokenizer branch 3 times, most recently from 4935949 to 10a93b6 Compare October 11, 2026 23:22
KuSh and others added 6 commits November 17, 2026 09:26
`parse_golangci_run_parts` walked the words of a raw shell command by hand
(`starts_with('-')`, a local `--flag=value` split, and a second copy of
golangci-lint's value-taking global flags) to find the `run` subcommand.

The shell string is not an obstacle to the shared tokenizer: the lexer already
returns each word with its byte offset, and `Token::source_index` indexes the
words handed in, so the offsets map a token back to a slice of the original
string. `lexer::words_and_spans` now names that pairing, and the flag grammar
comes from `golangci_cmd::global_takes_value` instead of a duplicate list that
could drift from it.

Behaviour is unchanged across all 26 cases in the new grammar test, which was
first run as a differential probe against the code it replaces. The one input
where the two nearly parted ways is `--config -- run`: pflag reads the `--` as
the config path (2.13.1: "can't read viper config: open --"), so the grammar
claims it rather than treating it as an end-of-options boundary.

Co-Authored-By: Claude Opus 5 <[email protected]>
`src/core/README.md` called `words_and_spans`' output "argv words". It is not:
`coalesce_words` returns slices of the original command string, so a word keeps
its quote characters and backslash escapes -- `--config "a path/x.yml"` yields
the word `"a path/x.yml"`, quotes included, and `--config=a\ b` stays escaped.

`parse_golangci_run_parts` is unaffected because it never reads a value: it
compares tokens against `run` and re-slices `cmd` by byte offset. But a caller
who follows the documented recipe and reads a `--config` path out of
`Token::value()` gets a path with literal quotes in it and silently looks up
the wrong file. The README paragraph and the `words_and_spans` rustdoc now say
the words are unresolved and point at `resolve_word_text`, which is what turns
one into argv text; it was private, so the advice was unfollowable.
`shell_split` resolves but drops the offsets, so it is not a substitute here.

The new lexer test pins the unresolved shape the docs now describe.

Co-Authored-By: Claude Opus 5 <[email protected]>
`use crate::cmds::go::golangci_cmd` makes `discover` depend on a command
filter's own flag table, which reads as an inverted dependency unless the
reason is written down.

Moving `global_takes_value` into `core/` would trade the edge for a worse one:
`core/` holds no per-tool flag table today (every `Option<ValueSpec>` grammar
in the tree lives with its command), and the table's own doc defines it against
`run_takes_value` in the same file, which delegates to it as a strict superset.
Splitting that pair across layers is the larger cost, so the table stays put.

Co-Authored-By: Claude Opus 5 <[email protected]>
…ences

The layering note said comparing a token against a keyword was safe. It is only
safe for the unquoted spelling: the words keep their quotes, so `golangci-lint
"run"` matches no keyword and `parse_golangci_run_parts` returns `None`. Here
that is passthrough -- unfiltered but correct -- which is why it is tolerable,
and the docs now draw that line instead of blessing the comparison outright.
New test pins the quoted spelling falling through rather than misrewriting.

Two test comments still named `split_token_spans`, deleted earlier in this
branch; they now name what actually does the work.

Co-Authored-By: Claude Opus 5 <[email protected]>
`words_and_spans` took over the last caller outside the lexer, so the pairing
of words with offsets is now the only shape the module hands out.

Co-Authored-By: Claude Opus 5 <[email protected]>
`spans[token.source_index]` only holds when the whole word list was handed to
the tokenizer. `parse_golangci_run_parts` tokenizes `&words[1..]`, so it reads
`spans[source_index + 1]`; a caller following the old wording while skipping
the binary would slice the original string at the wrong byte offset.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@KuSh
KuSh force-pushed the refactor/golangci-discover-tokenizer branch from 10a93b6 to ee565d6 Compare November 17, 2026 08:28

@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 merged commit 3ab08be into rtk-ai:develop Nov 18, 2026
16 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Nov 18, 2026
@KuSh
KuSh deleted the refactor/golangci-discover-tokenizer branch November 18, 2026 19:46
yungSNAT added a commit to yungSNAT/PascalsLetter that referenced this pull request Nov 24, 2026
…okenizer (#197)

refactor(discover): parse golangci-lint through arg_tokenizer

Ported from upstream rtk-ai/rtk 3ab08be. Kept the fork's two-argument
resolve_word_text (as_passed = false, as the fork's cargo normaliser
already passed) and its own lexer imports.

Co-authored-by: Nicolas Le Cam <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discover documentation Improvements or additions to documentation refactor tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants