Skip to content

Text output corrupts file content: literal "\n" escape sequences in source are rewritten as real newlines (0.25.0 & 0.26.0 tested) #270

Backlog

Description

@BENETNATH

Summary

yek in its default (text) output mode silently rewrites parts of file
content. Occurrences of the literal two-character sequence \n (backslash +
n) that appear inside source strings are converted into real newline
bytes (0x0A) in the output. The output therefore does not round-trip:
files reconstructed from a yek dump are not byte-identical to the inputs,
and Python files can become syntactically invalid ("unterminated string
literal").

The --json output mode is unaffected (byte-exact).

Environment

  • yek version: 0.26.0 (also observed on the version installed by
    curl -fsSL https://proofline.sh/yek/install.sh | bash on Linux x86_64)
  • OS: Linux (reproduced independently on two different machines)
  • Invocation: yek . --output-dir out and yek . (stdout), both affected

Reproducer

Create this file:

repro/escape.py

def escape(text):
    return (
        text.replace("\\", "\\\\")
        .replace(";", "\\;")
        .replace(",", "\\,")
        .replace("\r\n", "\\n")
        .replace("\r", "\\n")
        .replace("\n", "\\n")
    )

Run:

yek repro/ --output-dir out
cat -A out/*.txt

Expected

The chunk content is byte-identical to the input file; the .replace(...)
lines each stay on a single physical line, exactly as in the source.

Actual

The three lines whose replacement argument is the two-character sequence
\n are split across two lines: the sequence is emitted as a real newline
byte inside the string literal:

        .replace("\r\n", "$
")$
        .replace("\r", "$
")$
        .replace("\n", "$
")$

(cat -A output; $ marks a real line ending — note the string literal
now contains a raw 0x0A, so the emitted file is invalid Python.)

Other affected shapes seen in a real repository dump:

value = r"\nas"          # → value = r"<real LF>as"
"    text = '\n'.join(data_lines)\n"   # mid-string \n becomes a real LF

while other escape sequences next to them (\;, \,, \t, \r, \\) are
passed through untouched — the rewrite is specific to \n.

Impact

In our case (a Flask codebase of ~2100 files), 7 files were corrupted in a
35 MB dump, including the module that escapes CR/LF in user-controlled
text before writing iCalendar feeds — i.e. the security control itself was
mangled, and the corruption is easy to miss because the dump looks fine
in a text editor with soft wrap.

Workaround

yek --json produces byte-exact content (verified with a full
diff against the original files), presumably because the content is
JSON-escaped instead of being written through the text template.

Suggested fix

Write chunk content as raw bytes without any escape processing in the
default text template path (>>>> {path}\n{content}), or escape backslashes
when templating. The invariant worth a test: yek output must round-trip
every input file byte-for-byte (--json already does; text mode should
too).

Activity

  1. changed the title [-]Text output corrupts file content: literal "\n" escape sequences in source are rewritten as real newlines (0.26.0)[/-] [+]Text output corrupts file content: literal "\n" escape sequences in source are rewritten as real newlines (0.25.0 & 0.26.0 tested)[/+] on Nov 25, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions