rtk-ai/rtk

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

Open

#1,053 创建于 2026年4月6日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)Rust (2,914 fork)batch import
area:clibugeffort-mediumhelp wantedpriority:medium

仓库指标

Star
 (48,085 star)
PR 合并指标
 (平均合并 11天 1小时) (30 天内合并 45 个 PR)

描述

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.

贡献者指南