Skip to content

fix(sed): avoid replacing the final split sentinel - #1291

Open
sergioperezcheco wants to merge 2 commits into
shelljs:mainfrom
sergioperezcheco:fix/sed-final-line-sentinel-20261019
Open

sergioperezcheco wants to merge 2 commits into
shelljs:mainfrom
sergioperezcheco:fix/sed-final-line-sentinel-20261019

Conversation

@sergioperezcheco

Copy link
Copy Markdown
Contributor

Problem

sed() passes the final empty element from split('\n') to String.replace() even when it is only the sentinel after a terminating newline. Anchored or empty-matching replacements can append text after the final newline, and callbacks receive a nonexistent input line. For example, applying /^/ with '> ' to one\n currently produces > one\n> instead of > one\n.

Change

Remove only the split sentinel before replacing real lines, then restore the original terminating newline. Preserve real blank lines, CRLF characters, unterminated input, and existing empty-file behavior. Preserve standard sticky RegExp state across newline-terminated files and calls without executing an extra replacement or callback; unterminated-input state is unchanged.

Regression tests cover file and piped inputs, in-place replacement, callbacks, blank lines and sticky state. This is separate from the multi-file separator fix in #1261 and does not claim to fix #952 or reopen an older issue.

Validation

macOS / Node 22.22.3:

  • Previously established regressions against unchanged production: 5 failures; final focused suite: 28 passed.
  • Final npm test, including posttest ESLint: 662 passed, 6 existing skips.
  • npm run test-with-coverage: 662 passed, 6 skipped; src/sed.js has 100% statement/branch/function/line coverage.
  • Separate lint, Node-support check, generated-documentation consistency and git diff --check passed.
  • 140 public-API differential cases retain byte-identical output, including reused sticky regexes; 8 additional assertions pass.
  • Actual offline npm pack and extracted-package public API: 22 assertions pass. The same public probe with unchanged upstream sed source fails at the first anchored EOF case.

Other operating systems and Node versions have not been run locally. Hosted CI and human review are not claimed.

AI-assisted implementation, testing and automated review; no human review is claimed.

Assisted-by: AI tooling for implementation, testing and automated review
Signed-off-by: sergioperezcheco <checo520@outlook.com>
@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.20%. Comparing base (beb2b1f) to head (691bcf0).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1291      +/-   ##
==========================================
+ Coverage   97.17%   97.20%   +0.02%     
==========================================
  Files          36       36              
  Lines        1523     1536      +13     
==========================================
+ Hits         1480     1493      +13     
  Misses         43       43              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread test/sed.js Outdated

test('line anchors do not replace the split sentinel after a final newline', t => {
const file = `${t.context.tmp}/anchors.txt`;
const cases = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please unroll the loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrolled in 691bcf0 — each case is now its own write/assert block, same five inputs. npx ava test/sed.js passes (28 tests) and eslint is clean.

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.

sed command omitting characters at the end of the file

2 participants