Skip to content

fix(hook): rewrite commands without rewriting the text between them - #4132

Merged
KuSh merged 1 commit into
developfrom
lex/04-span-emitter
Oct 31, 2026
Merged

KuSh merged 1 commit into
developfrom
lex/04-span-emitter

Conversation

@KuSh

@KuSh KuSh commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

rewrite_compound split a compound command on each operator, rewrote the
segments, and then rebuilt the line by emitting " {op} " around every
operator it had split on. The operators came back, but the spacing was the
emitter's rather than the author's, and anything it did not model was
invented: ;; came back as ; ; and ;& as ; &, which is how a case
statement became a syntax error and a fall-through became a background job.
Those two had to be fixed in the lexer because the emitter could not tell
them apart from two separate operators.

The emitter now copies the gaps instead of rebuilding them. A segment is
emitted as its own leading whitespace, its rewrite, and its own trailing
whitespace; the operator between two segments is copied byte for byte from
the source. The multi-line path keeps a line's trailing whitespace as well as
its indent.

Wrapper prefixes had it too: strip_word_prefix trims the remainder so the
inner command can be matched, and the three sites that rejoined it used a
single space, so uv run ls and timeout 5 cargo build came back
respaced. prefix_gap hands the original back instead.

The pipeline rewriters had the same habit one level down, joining producer,
| and consumer with format!("{} {}", ..) over trimmed text, so
ls -la | head -3 came back single-spaced around the pipe. They now carry
the gaps either side of the | through as well. That was not a regression —
it predates this commit — but it is the same defect, and leaving it would
have made this commit's claim false for every pipeline.

What changes, all of it the author's formatting coming back intact:

ls -la  &&  ls     ->  rtk ls -la  &&  rtk ls      (was ` && `)
ls -la  |  head -3 ->  rtk ls -la  |  head -3       (was ` | `)
uv run  ls -la     ->  uv run  rtk ls -la           (was `uv run `)
ls ; ls            ->  rtk ls ; rtk ls             (was `; `)
ls;ls              ->  rtk ls;rtk ls               (was `; `)
ls &&\nls          ->  rtk ls &&\nrtk ls           (was ` && `)
case … 1;;esac     ->  … 1;;esac                   (was `;; `)

Five tests pinned the old normalisation and now pin the input's own spacing.
Each rewritten form was checked with bash -n.

The property is what makes this checkable rather than claimed: for a
generated corpus, deleting every inserted rtk from the output must give
back the input. It fails on a named input if the emitter is put back the way
it was. It also caught a case no example covered — a line's trailing
whitespace was dropped before a newline — which is fixed here rather than
excused in the assertion. Its one exemption is the whole-command trim()
that happens before the emitter is reached.

Its first version asserted nothing about pipelines: none of the commands it
generated is a safe pipe consumer, so every pipe case returned None. The
corpus now crosses real producers and consumers with irregular spacing around
the |, which is what surfaced the pipeline rewriters above.

Separators are safe to reproduce verbatim: ;, & and | each end a word
on their own, so a rewrite ending in a digit cannot fuse with them. A
redirect can — --head-lines 1 beside >out reads as an fd-1 redirect — but
that is settled before the emitter by join_redirect_suffix, and both cases
already had tests.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all — 3855 passing, clippy clean, at this commit rather than only at the tip of the stack
  • bytes outside a rewritten span are preserved exactly, and the sequence of separators is unchanged — head -1 f>out does not become --head-lines 1>out

Part of stack #4139 (#4129 → #4138). Based on #4131. Each commit gates green on its own, so the stack is bisectable.

🤖 Generated with Claude Code

@KuSh
KuSh added this pull request to stack #4139 September 19, 2026 13:31
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working hook rust labels Sep 19, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

This PR fixes command-rewriting logic in the hook/discover registry so that whitespace and text between rewritten command segments (in pipelines, compound commands, and wrapped prefixes) is preserved verbatim from the source rather than being rebuilt with normalized single spaces. It introduces helper functions (emit_segment, prefix_gap) to emit original gaps around rewritten commands.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

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

@KuSh
KuSh marked this pull request as ready for review September 19, 2026 16:22
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from dc1e62c to 9ea0fce Compare September 19, 2026 21:13
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 9ea0fce to 5108a50 Compare September 20, 2026 23:23
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 5108a50 to 6cc0197 Compare September 25, 2026 22:55
@KuSh
KuSh force-pushed the lex/04-span-emitter branch 2 times, most recently from 0721aea to 4bfc7fd Compare September 25, 2026 23:29
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 4bfc7fd to 087ddeb Compare September 26, 2026 17:56
@KuSh
KuSh force-pushed the lex/04-span-emitter branch 6 times, most recently from bd172e0 to c603a04 Compare October 5, 2026 22:36
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from c603a04 to fe931ec Compare October 7, 2026 07:16
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from fe931ec to 90198ae Compare October 9, 2026 11:07
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 90198ae to 1d1e670 Compare October 9, 2026 16:16
@KuSh
KuSh force-pushed the lex/04-span-emitter branch 2 times, most recently from 42db953 to 9d8df99 Compare October 9, 2026 20:29
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 9d8df99 to 22bae1a Compare October 15, 2026 00:24
@KuSh
KuSh force-pushed the lex/04-span-emitter branch 2 times, most recently from 3ad1f88 to beef9c1 Compare October 19, 2026 16:38
@KuSh
KuSh force-pushed the lex/04-span-emitter branch 2 times, most recently from ecd81b0 to 7bf0a40 Compare October 26, 2026 11:29
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 7bf0a40 to 7955dff Compare October 28, 2026 23:40
Base automatically changed from lex/03-one-segmenter to develop October 28, 2026 23:54
`rewrite_compound` split a compound command on each operator, rewrote the
segments, and then rebuilt the line by emitting `" {op} "` around every
operator it had split on. The operators came back, but the spacing was the
emitter's rather than the author's, and anything it did not model was
invented: `;;` came back as `; ;` and `;&` as `; &`, which is how a `case`
statement became a syntax error and a fall-through became a background job.
Those two had to be fixed in the lexer because the emitter could not tell
them apart from two separate operators.

The emitter now copies the gaps instead of rebuilding them. A segment is
emitted as its own leading whitespace, its rewrite, and its own trailing
whitespace; the operator between two segments is copied byte for byte from
the source. The multi-line path keeps a line's trailing whitespace as well as
its indent.

Wrapper prefixes had it too: `strip_word_prefix` trims the remainder so the
inner command can be matched, and the three sites that rejoined it used a
single space, so `uv run  ls` and `timeout  5  cargo build` came back
respaced. `prefix_gap` hands the original back instead.

The pipeline rewriters had the same habit one level down, joining producer,
`|` and consumer with `format!("{} {}", ..)` over trimmed text, so
`ls -la  |  head -3` came back single-spaced around the pipe. They now carry
the gaps either side of the `|` through as well. That was not a regression —
it predates this commit — but it is the same defect, and leaving it would
have made this commit's claim false for every pipeline.

What changes, all of it the author's formatting coming back intact:

    ls -la  &&  ls     ->  rtk ls -la  &&  rtk ls      (was ` && `)
    ls -la  |  head -3 ->  rtk ls -la  |  head -3       (was ` | `)
    uv run  ls -la     ->  uv run  rtk ls -la           (was `uv run `)
    ls ; ls            ->  rtk ls ; rtk ls             (was `; `)
    ls;ls              ->  rtk ls;rtk ls               (was `; `)
    ls &&\nls          ->  rtk ls &&\nrtk ls           (was ` && `)
    case … 1;;esac     ->  … 1;;esac                   (was `;; `)

Five tests pinned the old normalisation and now pin the input's own spacing.
Each rewritten form was checked with `bash -n`.

The property is what makes this checkable rather than claimed: for a
generated corpus, deleting every inserted `rtk ` from the output must give
back the input. It fails on a named input if the emitter is put back the way
it was. It also caught a case no example covered — a line's trailing
whitespace was dropped before a newline — which is fixed here rather than
excused in the assertion. Its one exemption is the whole-command `trim()`
that happens before the emitter is reached.

Its first version asserted nothing about pipelines: none of the commands it
generated is a safe pipe consumer, so every pipe case returned `None`. The
corpus now crosses real producers and consumers with irregular spacing around
the `|`, which is what surfaced the pipeline rewriters above.

Separators are safe to reproduce verbatim: `;`, `&` and `|` each end a word
on their own, so a rewrite ending in a digit cannot fuse with them. A
redirect can — `--head-lines 1` beside `>out` reads as an fd-1 redirect — but
that is settled before the emitter by `join_redirect_suffix`, and both cases
already had tests.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@KuSh
KuSh force-pushed the lex/04-span-emitter branch from 7955dff to 05e7f80 Compare October 28, 2026 23:54

@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 thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hook rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants