Skip to content

fix(output): preserve literal \n escape sequences in file content - #271

Open
Tyagiquamar wants to merge 1 commit into
prooflinedev:mainfrom
Tyagiquamar:fix/literal-newline-corruption-in-text-template
Open

Tyagiquamar wants to merge 1 commit into
prooflinedev:mainfrom
Tyagiquamar:fix/literal-newline-corruption-in-text-template

Conversation

@Tyagiquamar

Copy link
Copy Markdown

Summary

Fixes #270.

Preserves literal \n (and other escaped sequences) in file content when emitting output in text mode.

Problem

In src/lib.rs, concat_files unescaped template newlines by running .replace("\\\\\n", "\n").replace("\\\\n", "\n") on the formatted string after interpolating FILE_CONTENT.

As reported in #270, if source file content contained literal \n sequences (such as text.replace("\n", "\\n"), r"\nas", or regexes), yek in text mode silently transformed those literal escape sequences into real newline bytes (0x0A). This caused source files reconstructed from yek dumps to differ byte-for-byte from the input and broke syntax in files like Python.

Solution

Unescape template newlines in output_template before inserting file content rather than doing replacements on the concatenated output:

  1. Pre-process output_template once via config.output_template.as_ref().map(|t| t.replace("\\\\\n", "\n").replace("\\\\n", "\n")).
  2. When formatting each file (in both token counting and main output paths), replace FILE_PATH and FILE_CONTENT on the unescaped template.
  3. Because FILE_CONTENT is substituted last without subsequent string mutations, file content passes through byte-exact without escape sequence alteration.

Verification

  • Added unit regression test test_concat_files_preserves_literal_newlines_in_content in tests/lib_test.rs asserting byte-exact preservation of literal \n, \\n, and raw string escapes.
  • Ran cargo test --test lib_test -- test_concat_files: all 9 tests passed.
  • Verified code formatting with rustfmt --check.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant