Repository navigation
refactor(discover): parse golangci-lint through arg_tokenizer - #3998
Merged
Merged
Conversation
📊 Automated PR Analysis
SummaryThis 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 ( Review Checklist
Analyzed automatically by wshm · This is an automated analysis, not a human review. |
KuSh
force-pushed
the
refactor/golangci-discover-tokenizer
branch
from
September 17, 2026 22:38
8b5e55b to
55a6a37
Compare
KuSh
force-pushed
the
refactor/golangci-discover-tokenizer
branch
from
September 28, 2026 01:05
55a6a37 to
68bfea8
Compare
KuSh
force-pushed
the
refactor/golangci-discover-tokenizer
branch
3 times, most recently
from
October 11, 2026 23:22
4935949 to
10a93b6
Compare
`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
force-pushed
the
refactor/golangci-discover-tokenizer
branch
from
November 17, 2026 08:28
10a93b6 to
ee565d6
Compare
4 tasks done
Merged
3 of 4 tasks
3 tasks done
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
discover::registry::parse_golangci_run_partswalked the words of a raw shell command by hand to find therunsubcommand:starts_with('-'), a local--flag=valuesplit (split_golangci_flag_name), a local "does this flag take a separate value" rule (golangci_flag_takes_separate_value), andGOLANGCI_GLOBAL_OPT_WITH_VALUE— a second copy of the flag namescmds::go::golangci_cmd::global_takes_valuealready tracks. All four collapse into onetokenize_grammarcall.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..]) andToken::source_indexis an args-slice index, not a byte offset. Two facts undo that:split_token_spanswas a one-line wrapper overdiscover::lexer, which already returnsVec<(&str, usize)>— word text plus byte offset. (With it gone,words_and_spansalso takes overdiscover::without_rtk_disabled, the only other place that wanted the same pairing, andcoalesce_wordsdrops to private.)Token::source_indexindexes the words handed in, sospans[token.source_index]is the byte offset.So the layering is: shell string → lexer (words + spans) →
arg_tokenizer(grammar) →source_indexback down to a byte offset.lexer::words_and_spansnow names that pairing so it doesn't get rediscovered, andsrc/core/README.md's tokenizer section states it (including thediscover::lexer::TokenKind/arg_tokenizer::TokenKindname collision).The lexer's words are not an argv, and the docs now say so.
coalesce_wordsreturns 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\ bstays escaped.parse_golangci_run_partsis unaffected — it never reads a value, only compares againstrunand re-slicescmdby offset — but a caller who reads a--configpath out ofToken::value()would get a path with literal quotes and silently look up the wrong file. The README paragraph and thewords_and_spansrustdoc now name the words unresolved and point atlexer::resolve_word_text(madepub(crate), so the advice is followable);shell_splitresolves but discards the offsets.test_words_and_spans_keeps_words_unresolvedpins 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, soparse_golangci_run_partsreturnsNoneand 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 bytest_classify_golangci_lint_quoted_subcommand_is_not_rewritten.Deduping the flag list
golangci_cmd::global_takes_valuebecomespub(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 — everyOption<ValueSpec>grammar in the tree lives with its command — andglobal_takes_valueis defined againstrun_takes_valuein the same file, which delegates to it as a strict superset. Sodiscoverimports it fromcrate::cmds::go::golangci_cmd, and the import carries a short note saying why.Behaviour
Bit-identical. The new
test_golangci_run_parts_grammartable 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.ymlcluster,--trace-path/--mem-profile-path/--cpu-profile-path, the bare-, the--early return,golangcias 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--configswallow 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: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_indexhas the same--config --gap on the argv side — it returnsNonethere 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 -- --checkandcargo clippy --all-targets -- -D warningsare clean.cargo test --all --no-fail-fastpasses in full.Behaviour was also checked against the binary built from
develop, which still has the scan this replaces:rtk rewriteover 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 arertk hook claudewith allow/deny rules andrtk discover --allover a planted session.