fix(filesystem): prevent off-by-one line count in tailFile with trailing newlines - #4643
Open
teddiesloco wants to merge 1 commit into
Open
Conversation
…railing newline When a file ends with a trailing newline (`\n` or `\r\n`), splitting the last chunk on newline produces an empty trailing element. `tailFile` was treating this empty element as a valid line, returning N-1 actual lines instead of N. This fix strips the trailing newline only from the very first chunk read (the end of the file) before splitting, matching the behavior of Unix `tail`. Also handles the edge case where the very first line of the file was previously dropped if the file was read to the beginning with `remainingText` left. Adds 7 real-fs regression tests covering trailing/no-trailing newline, CRLF, single lines, and multi-chunk files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When reading the tail of a file that ends with a trailing newline (
\nor\r\n),tailFilewas returning N-1 lines instead of N lines.This happens because
split('\n')on content ending in\nproduces a trailing empty string"". The loop intailFilecounts this empty element towardnumLines, resulting in one fewer real line of content being returned. Since most text files on POSIX systems end with a newline, this affects standard usage.Reproduction
Root Cause
normalizeLineEndings(chunkText).split('\n')results in['...', '', '']or['...', ''].for (let i = chunkLines.length - 1; ...)consumes the empty trailing element as a counted line.position === 0), any remaining text from an incomplete line at the start of the file was discarded instead of being prepended to the result.Fix
isLastChunk), strip the trailing newline after normalizing line endings. Subsequent chunks preserve all newlines.position === 0and there is leftoverremainingTextand the requested line count hasn't been met, unshift it into the result array.tail -n N.Verification
Added 7 regression tests against real filesystem operations covering:
\r\n) line endings with trailing newlineAll 159 tests pass across the filesystem server suite (152 existing + 7 new).