Skip to content

A case (or subshell / brace group) anywhere on a line disables rewriting of every pipeline on that line #4283

Open

Description

@KuSh

Summary

A case statement anywhere on a command line disables the rewrite of any pipeline anywhere else on that line — including a pipeline in a different segment. The same happens for a subshell or a brace group sharing a line with a pipe.

This is a conservative miss, not corruption: the command runs as written. It is filed because it survives #3343's safe-consumer rewrite, so it is what still keeps the shapes in #3722 unrewritten once a case, ( ) or { } is on the line.

Repro

rtk rewrite at the tip of the lexing stack (#4138, a4e60f84); the plain-pipe row is the control.

command result
git status | cat rtk git status | cat
case x in x) git status;; esac | cat not rewritten
git status | cat; case x in x) echo 1;; esac not rewritten
ls -la | head -3; case x in x) echo 1;; esac not rewritten
(git status) | cat not rewritten
{ git status; } | cat not rewritten

The two bold rows are the cost: the case and the pipeline are separate commands, and the pipeline would rewrite on its own.

Cause

rewrite_compound in src/discover/registry.rs bails before it segments anything:

let has_opaque_grouping = tokens.iter().any(|token| {
    token.kind == TokenKind::Shellism && matches!(token.value.as_str(), "(" | ")" | "{" | "}")
});
if has_pipe && has_opaque_grouping {
    return None;
}

The check is over the whole line, and a case pattern's closing ) counts as grouping. So one ) anywhere, plus one | anywhere, and nothing on the line is looked at.

What makes it fixable now

#4135 added CaseTracker to the lexer, which both segmenters consult so that a pattern's ( is not read as a subshell. The same tracker knows which ) closes a pattern, so has_opaque_grouping could skip those — and, more generally, the check could be made per segment rather than per line, so a pipeline in one segment is judged on its own rather than on what sits beside it.

The subshell and brace-group rows are the same check with a different bracket. Whether those should rewrite inside a pipeline is a separate question from whether a case elsewhere on the line should block a pipeline that has nothing to do with it.

Related: #3722 (the general pipe skip; this is its residue after #3343), #3197 (where the case row was noticed while re-testing the ;; fix), #4135 (CaseTracker).

Activity

  1. added
    bug
    help wanted
    on Sep 26, 2026
  2. rtk-wshm-sync-bot commented on Sep 26, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 90%

    Summary

    A case/subshell/brace-group anywhere on a line makes rewrite_compound bail on the whole line's pipelines instead of checking per segment via CaseTracker.

    📁 Relevant files
    • src/discover/registry.rs

    Suggested Actions

    1. Use CaseTracker (from fix(discover): make a subshell a command boundary everywhere #4135) in has_opaque_grouping to exclude ) that closes a case pattern
    2. Make the opaque-grouping check per-segment rather than per-line in rewrite_compound
    3. Decide separately whether subshell/brace-group pipelines ((git status) | cat, { git status; } | cat) should rewrite
    4. Cross-reference Hook rewrite (rtk hook claude) skips commands containing a pipe (|) #3722 and bug(hook): rewrite path turns ;; into ; ;, breaking every case statement (bash syntax error) #3197 when implementing the segment-level fix

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibughelp wantedpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions