Skip to content

rtk read -n (target of the cat -n rewrite) drops carriage returns and adds a final newline #4816

Open

Description

@alponme

Summary

The hook rewrites cat -n FILE to rtk read -n FILE. Apart from its own gutter, the output differs from cat -n in content: the CR of every CRLF line is removed, and a trailing LF is added to a file that does not end with one. Plain rtk read FILE keeps both, so only the numbered path is affected.

Environment

rtk 1.1.0 full edition, x86_64-unknown-linux-musl (same on 1.0.1); Ubuntu 24.04.5 LTS; GNU coreutils 9.4.

Steps to reproduce

mkdir -p /tmp/repro && cd /tmp/repro
printf 'a\r\nb\r\nc' > crlf.txt
rtk rewrite 'cat -n crlf.txt'        # rtk read -n crlf.txt
cat -n crlf.txt | od -c
rtk read -n crlf.txt | od -c
rtk read crlf.txt | od -c            # unnumbered: byte-identical to the file

Expected

cat -n (rc 0), 28 bytes: 1\ta\r\n 2\tb\r\n 3\tc, with CRs kept and no final newline.

Actual

rtk read -n (rc 0), 24 bytes: 1 │ a\n2 │ b\n3 │ c\n

  • every CR removed
  • a trailing \n added
  • gutter: number, space, U+2502 (3 bytes), space, where cat -n uses a right-aligned number and a TAB

Why it matters

An agent that uses numbered output to edit a CRLF file, or to check whether the last line ends with a newline, sees line endings that are not on disk. Parsers that split cat -n output on the TAB fail. CONTRIBUTING: "RTK's output must be a valid, useful subset of the original tool's output".

Notes

— Alfredo Pons Menargues

Activity

  1. added
    bug
    help wanted
    on Oct 28, 2026
  2. rtk-wshm-sync-bot commented on Oct 28, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 90%

    Summary

    rtk read -n (the cat -n rewrite target) normalizes CRLF to LF and appends a trailing newline not present in the source file, diverging from cat -n output; fix the numbering path in read.rs to preserve original line terminators and EOF state.

    📁 Relevant files
    • src/cmds/system/read.rs

    Suggested Actions

    1. Review src/cmds/system/read.rs numbering path using format!("{:>width$} │ {}\n", ...) — it normalizes line terminators
    2. Coordinate with open PR fix(read): separate line numbers with a tab #4647 (gutter tab change) since it doesn't address CR handling or trailing newline
    3. Extend the byte-faithful fix from fix(read): make head/tail rewrites faithful to the native commands #3941 (currently only for numbering off) to cover the numbered path too
    4. Add regression test with CRLF file lacking trailing newline, comparing rtk read -n output to cat -n byte-for-byte

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  3. KuSh commented on Oct 31, 2026

    @KuSh
    Collaborator

    There are 2 open pull requests for this issue: #4828, #4872 (oldest first).

    If you were about to start on it, reviewing or testing one of these helps more than another pull request: a note on which approach fits, or a test result on your setup, is welcome on the pull request itself.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibughelp wantedpriority:medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions