Skip to content

fix(grep): do not match the final split sentinel - #1292

Open
Sskift wants to merge 1 commit into
shelljs:mainfrom
Sskift:fix/grep-ignore-final-sentinel
Open

Sskift wants to merge 1 commit into
shelljs:mainfrom
Sskift:fix/grep-ignore-final-sentinel

Conversation

@Sskift

@Sskift Sskift commented Nov 2, 2026

Copy link
Copy Markdown

grep() treats the final empty element from splitting newline-terminated input as an actual line. As a result, ShellString('text\n').grep(/^$/) returns a blank match with exit code 0, and inverted matching can produce an extra line even when every input line matches.

Skip that final empty element when matching lines. Preserve actual blank lines, line numbering and context, and use the existing no-match error handling when no physical line matches. This leaves the separate after-context bounds change in #1268 untouched.

Regression coverage includes piped input, empty files, real blank lines, inverted matches and numbered context. The existing inverted-match count is corrected to exclude its phantom final line.

Validation on Windows with Node.js 24.22.1:

  • Before the fix: six new regression tests and the corrected existing assertion fail.
  • Focused grep and pipe tests: 54 passed.
  • ESLint and git diff --check: passed.
  • Seven public-API cases match GNU grep output and exit status.
  • Full suite: 660 passed, 6 skipped and 3 failures. Original HEAD in an isolated baseline has the same 3 failures (654 passed, 6 skipped): two Windows command-error expectations and one symlink fixture assertion.

Investigated, implemented and tested with OpenAI Codex assistance. No human review is claimed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant