Skip to content

fix: tail -n counts lines correctly when input lacks trailing newline (fixes #1787)#1823

Merged
gnodet merged 1 commit into
jline:masterfrom
gnodet:ci-issue-1787
Apr 24, 2026
Merged

fix: tail -n counts lines correctly when input lacks trailing newline (fixes #1787)#1823
gnodet merged 1 commit into
jline:masterfrom
gnodet:ci-issue-1787

Conversation

@gnodet
Copy link
Copy Markdown
Member

@gnodet gnodet commented Apr 23, 2026

Summary

  • Fix off-by-one error in PosixCommands.tail where -n N returned one extra line when input doesn't end with a newline character
  • The implementation counted newline characters (\n) rather than lines, so the final unterminated line wasn't counted, causing the window to include one extra line
  • Add tests for both trailing-newline and no-trailing-newline cases

Fixes #1787

Summary by CodeRabbit

  • Bug Fixes
    • Fixed the tail command to properly handle files without trailing newlines. Previously, when displaying the last N lines of a file that doesn't end with a newline, the final line could be incorrectly excluded from the output. The command now correctly counts and displays all lines.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 20aab721-20a1-49d1-a815-7b4b9dc7a05f

📥 Commits

Reviewing files that changed from the base of the PR and between 79b0f73 and 43f2a4f.

📒 Files selected for processing (2)
  • builtins/src/main/java/org/jline/builtins/PosixCommands.java
  • builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java

📝 Walkthrough

Walkthrough

The tail() command's line-counting logic is corrected to treat a non-empty buffer ending without a newline as containing an additional line, fixing undercounting. Test cases validate correct behavior with and without trailing newlines.

Changes

Cohort / File(s) Summary
Line Counting Bug Fix
builtins/src/main/java/org/jline/builtins/PosixCommands.java
Modified tail() implementation to account for the final line when buffer doesn't end with '\n', preventing undercounting of lines in files without trailing newlines.
Test Coverage
builtins/src/test/java/org/jline/builtins/PosixCommandsTest.java
Added two test methods covering tail -n 2 behavior: one with input lacking trailing newline (testTailNoTrailingNewline) and one with trailing newline (testTailWithTrailingNewline).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A tail that counts with care,
No more lines lost in the air,
The final line, though bare,
Now gets its rightful share! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: correcting the line-counting behavior in tail when input lacks a trailing newline, with a reference to the issue number.
Linked Issues check ✅ Passed The PR correctly implements the fix for issue #1787 by adjusting the tail() line-counting logic to treat non-empty buffers without trailing newlines as containing an additional line, and adds comprehensive tests for both scenarios.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the tail command's line-counting bug: the implementation fix and two new test cases for trailing-newline scenarios.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sonarqubecloud
Copy link
Copy Markdown

@gnodet gnodet merged commit f0d6a6d into jline:master Apr 24, 2026
19 of 27 checks passed
@gnodet gnodet added the fix Bug fix label May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: PosixCommands.tail counts newlines rather than lines

1 participant