diff --git a/tools/file_operations.py b/tools/file_operations.py index 1b4ffd5a38761..5792d2281ed58 100644 --- a/tools/file_operations.py +++ b/tools/file_operations.py @@ -1484,6 +1484,16 @@ class ShellFileOperations(FileOperations): total_lines = int(wc_output.strip()) except ValueError: total_lines = 0 + + # wc -l counts newline characters, not lines. A file without a trailing + # newline has one more line of content than wc -l reports. Detect this + # by checking if the last byte of the file is 0a (\n). + if file_size > 0: + tail_cmd = f"tail -c 1 {self._escape_shell_arg(path)} | od -An -tx1 | tr -d ' '" + tail_result = self._exec(tail_cmd) + last_byte = tail_result.stdout.strip() + if last_byte and last_byte != "0a": + total_lines += 1 # Check if truncated truncated = total_lines > end_line