aft icon indicating copy to clipboard operation
aft copied to clipboard

bash-hints: grep nudge fires on every Windows path — readShellToken unescapes backslashes, so absolute operands resolve in-project

Open aurele-l opened this issue 3 weeks ago • 2 comments

Summary

On Windows, the grep/rg nudge fires on every rg/grep command whose path operand is written with backslashes — including paths outside the project and single files inside it, which are exactly the cases the suppression logic was written to stay quiet for. The same commands with forward slashes are silent, so it looks like "the hint is random" to anyone on win32.

Environment

  • @cortexkit/[email protected] (reproduced there; shouldSuppressGrepSearchHint and readShellToken are byte-identical in 0.64.0, and on main at packages/aft-bridge/src/bash-hints.ts)
  • Windows 11, Node 24, OpenCode host (bash tool = PowerShell on this machine)
  • project root C:\Users\dev\proj

Repro

cd C:\Users\dev\proj
rg -n foo C:\Users\dev\elsewhere\CHANGELOG.md

Output is followed by:

DO NOT search code by running grep/rg in bash — it is unindexed, unranked, and serial. Use the `grep` tool instead (indexed and ranked).

Same command with forward slashes is silent:

rg -n foo C:/Users/dev/elsewhere/CHANGELOG.md

Measured suppression

I extracted shouldSuppressGrepSearchHint and its helpers from the shipped @cortexkit/aft-bridge/dist/bash-hints.js and ran them directly with projectRoot = C:\Users\dev\proj:

command suppress
rg -n foo crates/as-run/src (directory) false — intended
rg -n foo crates/as-run/src/zone.rs (file) true
rg -n foo "C:\Users\dev\elsewhere\CHANGELOG.md" false — bug
rg -n foo C:/Users/dev/elsewhere/CHANGELOG.md true
rg -n foo C:\Users\dev\proj\app\Cargo.toml false — bug
rg -n foo C:/Users/dev/proj/app/Cargo.toml true
cd C:/Users/dev/elsewhere && rg -n foo . true
rg -n foo . false — intended

Root cause

readShellToken applies POSIX escape semantics unconditionally, and PowerShell does not use \ as an escape character:

// packages/aft-bridge/src/bash-hints.ts
} else if (ch === "\\") {
  index++;
  token += command[index] ?? "\\";   // drops the separator
}

So the operand arrives as:

collectPathOperands(...) -> ["C:UsersdevprojappCargo.toml"]

Then the chain runs exactly as written for a relative path:

  1. resolvePathOperand: path.isAbsolute("C:UsersdevprojappCargo.toml") is false on win32 (drive-relative, no separator after the colon), so it resolves against the project root.
  2. isPathInsideProject → true.
  3. sawInProjectOperand = true; shouldSuppressResolvedPath calls statSync on a path that does not exist → catch { return false }.
  4. After the loop: if (sawInProjectOperand) return false; → nudge.

The "operand outside the project" branch (if (!isPathInsideProject(...)) continue) is therefore unreachable on Windows for any backslash path.

firstPipelineStage and splitTopLevelStatements do the same index++ after \, so they inherit the same wrong model of the shell.

There is no win32 test coverage: packages/aft-bridge/src/__tests__/bash-hints.test.ts is entirely POSIX-shaped (no path.win32, no C:\).

Suggested fix

Either half alone fixes the reported behaviour; both are cheap.

  1. Do not unescape \ on win32 in readShellToken (and the same index++ in firstPipelineStage / splitTopLevelStatements), or key the quoting model off the host shell. PowerShell escapes with a backtick, not a backslash.
  2. Fail open on statSync: an in-project operand that does not exist is more likely a mis-resolved token than a real in-project search, so treat it as external instead of setting sawInProjectOperand. That is also correct for a genuinely in-project path that was deleted between the grep and the check.

Tests worth pinning (the last one is the positive control that keeps the nudge working):

  • readShellToken("C:\\a\\b c", 0) keeps its separators on win32.
  • maybeAppendGrepSearchHint(out, 'rg -n foo C:\\Users\\dev\\elsewhere\\f.md', true, 'C:\\Users\\dev\\proj') === out.
  • … 'rg -n foo C:\\Users\\dev\\proj\\app\\f.md' … === out.
  • … 'rg -n foo C:\\Users\\dev\\proj\\src' … still nudges.

Happy to send a PR for this if you'd like it in that shape.

aurele-l avatar Nov 12 '26 22:11 aurele-l

Thanks for the precise report and the measurements. Confirmed on main: readShellToken, firstPipelineStage and splitTopLevelStatements in packages/aft-bridge/src/bash-hints.ts treat \ as an escape character outside quotes on every platform, and inside double quotes before any character. On Windows a backslash path loses its separators, resolves as a relative path inside the project, fails statSync, and the nudge fires. That matches your table.

We'll fix it here, with Windows path tests that include your positive control (an in-project directory still gets the nudge), and post on this issue when the release that carries it is out.

aft-alfonso[bot] avatar Nov 12 '26 22:11 aft-alfonso[bot]

Fixed in v0.65.0, released today: On Windows, the grep hint no longer misreads backslash paths outside the project. Thanks for the report. If you still see it after updating, reply here and we'll reopen.

aft-alfonso[bot] avatar Nov 14 '26 10:11 aft-alfonso[bot]