rtk-ai/rtk

[bug] rewrite_segment() fails to match path-prefixed commands and fixing it naively risks wrong-binary execution

Open

#1 053 ouverte le 6 avr. 2026

Voir sur GitHub
 (2 commentaires) (0 réactions) (0 assignés)Rust (2 914 forks)batch import
area:clibugeffort-mediumhelp wantedpriority:medium

Métriques du dépôt

Stars
 (48 085 stars)
Métriques de merge PR
 (Merge moyen 11j 1h) (45 PRs mergées en 30 j)

Description

RTK’s rewrite pipeline handles path-prefixed commands inconsistently.

Commands like .venv/bin/pytest -v, .venv/bin/ruff check ., and ./node_modules/.bin/vitest run are recognized as supported by classify_command(), which normalizes argv[0] via strip_absolute_path(). However, rewrite_segment() does not normalize before prefix replacement, so these commands can classify as supported and still fail rewrite.

Relevant code in src/discover/registry.rs:

  • classify_command() normalizes via strip_absolute_path() (around line 100)
  • rewrite_segment() matches against the unnormalized token (around line 555)
  • exclude_commands matching also uses the unnormalized base token (around line 512)

Current behavior is accidentally safe. Path-prefixed commands fail rewrite, fall through to raw execution, and preserve the original executable path. No filtering happens, but execution semantics do not change.

A naive fix that normalizes in rewrite_segment() and emits rtk pytest -v would change semantics from “run this exact binary” to “run whatever PATH resolves.” RTK handlers generally discard the caller’s executable path and re-resolve via resolved_command() (for example pytest_cmd.rs:16 and ruff_cmd.rs:44). When the venv is not activated, or the path was specified intentionally, this can produce wrong-binary execution or command-not-found failures.

A correct fix requires three things:

  1. Normalize path-prefixed commands in rewrite_segment() for matching only.
  2. Preserve the original executable path in the rewritten RTK invocation, for example via a --rtk-bin flag or environment variable.
  3. Teach relevant handlers to honor an explicit binary override instead of always resolving via PATH.

Without all three pieces, making path-prefixed commands rewrite successfully would turn safe passthrough into incorrect execution.

Guide contributeur