rtk-ai/rtk

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

Open

#1053 aperta il 6 apr 2026

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)Rust (2914 fork)batch import
area:clibugeffort-mediumhelp wantedpriority:medium

Metriche repository

Star
 (48.085 star)
Metriche merge PR
 (Merge medio 11g 1h) (45 PR mergiate in 30 g)

Descrizione

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.

Guida contributor