Skip to content

22 rewrite rules end in a bare subcommand alternation with no word boundary (git branchless → rtk git branchless) #4009

Closed

Description

@KuSh

Summary

22 rules in src/discover/rules.rs end in a bare subcommand alternation with no trailing word boundary, so any command whose second word merely starts with a listed subcommand is rewritten. git branchless status becomes rtk git branchless status, and git-branchless is a real, widely used tool.

This was found while reviewing #3676, which fixes exactly this bug for the pnpm rule by appending (\s|$). 23 rules had the defect on develop; that PR fixes one, leaving 22.

Reproduction

Against the develop binary (79b96a4):

$ rtk rewrite "git branchless"      → rtk git branchless
$ rtk rewrite "git statuses"        → rtk git statuses
$ rtk rewrite "git logger"          → rtk git logger
$ rtk rewrite "git showcase"        → rtk git showcase
$ rtk rewrite "cargo installer"     → rtk cargo installer
$ rtk rewrite "cargo builder"       → rtk cargo builder
$ rtk rewrite "gh prs"              → rtk gh prs
$ rtk rewrite "gh issues"           → rtk gh issues
$ rtk rewrite "glab mrs"            → rtk glab mrs
$ rtk rewrite "docker psql"         → rtk docker psql
$ rtk rewrite "kubectl getall"      → rtk kubectl getall
$ rtk rewrite "oc status-check"     → rtk oc status-check
$ rtk rewrite "ruff checker"        → rtk ruff checker
$ rtk rewrite "go vetting"          → rtk go vetting
$ rtk rewrite "terraform planner"   → rtk terraform planner
$ rtk rewrite "pio runner"          → rtk pio runner

Every one of these should be left untouched.

git branchless is the one with real-world bite today: it is an installed third-party subcommand, and the hook silently redirects it into rtk git, which has no idea what branchless is.

Affected rules

All 22 end in an alternation group with nothing anchoring its right edge:

git|yadm, gh, glab, cargo, prettier, next build, playwright, prisma, docker, kubectl, oc, ruff, sqlfluff lint, pip|uv pip, go, sbt, rake|rails test, pio run, quarto render, shopify theme, terraform plan, trunk build

(pnpm was the 23rd and is fixed by #3676.)

Rules that already do this correctly — ^ls(\s|$), ^tree(\s|$), ^wc(\s|$), ^mypy(\s|$), ^pytest(\s|$), the gradlew rule, and the several \b-terminated ones — show the intended shape.

Suggested fix

Append (\s|$) (or \b) to each affected pattern, then close the class with a table-wide test asserting every rule's pattern is boundary-terminated, so a new rule can't reintroduce it:

#[test]
fn every_rule_pattern_is_boundary_terminated() {
    for (i, rule) in RULES.iter().enumerate() { /* … */ }
}

A test that simply appends a junk suffix to each rule's own sample command and asserts no rewrite would catch all 22 at once.

Note on blast radius

pnpm 12 adds clean and purge built-ins that delete node_modules. None are matched by a rule today, but the combination of destructive built-ins and unanchored alternations is worth closing before it matters.

Related

Activity

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

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 96%

    Summary

    22 rules in src/discover/rules.rs lack a trailing word-boundary on their subcommand alternation, causing false-positive rewrites (e.g. git branchless → rtk git branchless); fix by appending (\s|$) to each pattern and add a table-wide boundary test.

    📁 Relevant files
    • src/discover/rules.rs

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

  3. tsugumi42 commented on Sep 18, 2026

    @tsugumi42

    Two instances of the pip|uv pip rule, from a 0.45.0 install on Windows/MSYS:

    $ rtk rewrite "pip3 list"
    rtk pip list      # `pip3` collapsed to `pip`
    
    $ rtk rewrite "uv pip list"
    rtk pip list      # `uv` qualifier dropped

    uv pip list looks worth more than a cosmetic fix. Here pip list and
    uv pip list resolve to different interpreters (uv's managed environment vs
    the active venv), so the rewrite doesn't return a shorter version of what was
    asked — it answers a different question, silently. By the Transparency rule in
    CONTRIBUTING, rtk pip list is not "a valid, useful subset" of uv pip list.

    One note on workarounds, since it affects how these get fixed:
    [hooks] exclude_commands only matches the outermost command. Adding uv there
    does suppress uv pip list / uv sync / uv add / uv lock / uv venv /
    uvx ... (verified), but uv run pytest still becomes uv run rtk pytest.
    That specific case is already tracked in #1205 (v0.43.0 regression, silently
    collects 0 tests), so I'm not re-filing it — the point is just that the
    config-level opt-out can't reach wrapped commands.

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:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions