Skip to content

Keep line breaks inside literals when formatting - #9116

Merged
cknitt merged 1 commit into
masterfrom
fix-9100-format-line-endings-strings
Oct 29, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-9100-format-line-endings-strings

Conversation

@cknitt

@cknitt cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member

Since #9042, bsc -format converted the whole source to LF before parsing and the printed result back to CRLF for CRLF files. String literals went through both conversions, so formatting changed the program: a raw LF inside a string in a CRLF file became CRLF, and a raw CRLF inside a string in an LF file became LF. The same happened to template literals, character literals ('<LF>') and polymorphic variant names (#"a<LF>b"). Each conversion also removed only one CR before an LF, so a comment containing \r\r\n changed again on every run.

The source is now parsed as is. Res_doc.to_string takes the newline to write for the line breaks the printer emits itself, and Res_printer.print_implementation/print_interface pass it through; literal_line, which only string and template contents use, stays LF, and text copied from the source (literals, comments) keeps its bytes. Res_line_endings shrinks to newline source: CRLF when the source's first line break is CRLF, LF otherwise. bsc -format, res_parser -print res and the editor's format command all print with it. The one place where the printer rewrote a comment's line break itself, the first line of a reindented /* … * … */ comment, now drops that line's CRs, as the following lines' trimming already did.

res_parser -print res used to print a CRLF file with LF line endings; it now keeps them, like bsc -format.

The multi-printer test in tests/syntax_tests/res_test.ml now also checks that formatting leaves these unchanged and is stable on them: a reindented star comment in a CRLF file; raw LF and CRLF inside strings, templates, character literals and polymorphic variants, in both CRLF and LF files; comments holding \r\r\n or mixed line breaks. It also checks that a reindented comment's \r\r\n becomes the file's newline. Each of the new literal and comment cases fails on master.

This issue doesn't cover lone CRs (#9108), and this PR leaves them alone.

Fixes #9100

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Make bsc -format keep the line breaks inside string, template and character literals and polymorphic variant names as written, and format comments with \r\r\n stably.

Semantic Diff

1 commit · Full page

✏️ Write the formatter's own line breaks with the file's newline

9de0dae · cknitt + Claude · Oct 28, 2026, 15:42 UTC · edited

Why

Since #9042, formatting converted the whole source to LF and the output back to CRLF, string literals included, so bsc -format changed the program when a string held a raw line break of the other kind. Each conversion also dropped only one CR before an LF, so a comment with \r\r\n changed on every run (#9100).

The source is now parsed as is, and the printer writes the line breaks it emits with the newline of the source (CRLF when the first line break is CRLF), while literals keep their bytes. bsc -format, res_parser -print res and the editor's format command do the same. In the examples below, \r and \n stand for CR and LF, the heading holds the input, and each version is what bsc -format prints for it.

Format modified (let x = 1\r\nlet s = "a\nb"\r\n)

text

-let x = 1\r\nlet s = "a\r\nb"\r\n
+let x = 1\r\nlet s = "a\nb"\r\n

Format modified (let x = 1\nlet s = "a\r\nb"\n)

text

-let x = 1\nlet s = "a\nb"\n
+let x = 1\nlet s = "a\r\nb"\n

Format modified (let x = 1\n/* a\r\r\nb */\n)

text · Before: first run, then second run · After: first and second run

-let x = 1\n/* a\r\n b */\n
 let x = 1\n/* a\n b */\n

Review

  • Why does the comment lose its CRs? The printer reindents a multi-line /* … */ comment and writes that comment's line breaks itself, as it already did for the lines after the first.

@codecov

codecov Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.47%. Comparing base (85f31fb) to head (9de0dae).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
analysis/src/commands.ml 0.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9116   +/-   ##
=======================================
  Coverage   81.46%   81.47%           
=======================================
  Files         460      460           
  Lines       64675    64680    +5     
=======================================
+ Hits        52689    52696    +7     
+ Misses      11986    11984    -2     
Files with missing lines Coverage Δ
compiler/syntax/cli/res_cli.ml 76.00% <100.00%> (+1.00%) ⬆️
compiler/syntax/src/res_doc.ml 69.75% <100.00%> (+0.14%) ⬆️
compiler/syntax/src/res_driver.ml 88.67% <100.00%> (+0.21%) ⬆️
compiler/syntax/src/res_line_endings.ml 100.00% <100.00%> (+5.55%) ⬆️
compiler/syntax/src/res_minibuffer.ml 81.81% <ø> (-4.55%) ⬇️
compiler/syntax/src/res_multi_printer.ml 78.57% <100.00%> (+1.64%) ⬆️
compiler/syntax/src/res_printer.ml 94.53% <100.00%> (+<0.01%) ⬆️
analysis/src/commands.ml 63.38% <0.00%> (+0.88%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9116

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9116

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9116

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9116

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9116

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9116

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9116

commit: 9de0dae

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T16:42:40.777484Z b32d878 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b32d878cea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Why:
    Since #9042, formatting converted the whole source to LF and the output
    back to CRLF, string literals included, so `bsc -format` changed the
    program when a string held a raw line break of the other kind. Each
    conversion also dropped only one CR before an LF, so a comment with
    `\r\r\n` changed on every run (#9100).

The source is now parsed as is, and the printer writes the line breaks it
emits with the newline of the source (CRLF when the first line break is
CRLF), while literals keep their bytes. bsc -format, res_parser -print res
and the editor's format command do the same. In the examples below, `\r`
and `\n` stand for CR and LF, the heading holds the input, and each version
is what `bsc -format` prints for it.

Format modified (let x = 1\r\nlet s = "a\nb"\r\n):
Before (text): let x = 1\r\nlet s = "a\r\nb"\r\n
After (text): let x = 1\r\nlet s = "a\nb"\r\n

Format modified (let x = 1\nlet s = "a\r\nb"\n):
Before (text): let x = 1\nlet s = "a\nb"\n
After (text): let x = 1\nlet s = "a\r\nb"\n

Format modified (let x = 1\n/* a\r\r\nb */\n):
Before (text): first run, then second run
    let x = 1\n/* a\r\n b */\n
    let x = 1\n/* a\n b */\n
After (text): first and second run
    let x = 1\n/* a\n b */\n

Review: Why does the comment lose its CRs? The printer reindents a
    multi-line `/* … */` comment and writes that comment's line breaks
    itself, as it already did for the lines after the first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <[email protected]>
@cknitt
cknitt force-pushed the fix-9100-format-line-endings-strings branch from b32d878 to 9de0dae Compare October 28, 2026 20:16
cknitt added a commit that referenced this pull request Oct 28, 2026
Why:
    Some Windows editors write a byte order mark, and bsc and bsc -format
    rejected it byte by byte. A CR without an LF ended a `//` comment but
    did not start a new line, so the next statement counted as on the same
    line. The scanner now starts after a byte order mark and ends a line
    at a CR not followed by LF, as editors and JavaScript do; code frames
    show lines the same way, and the formatter keeps the byte order mark.

Diagnostic removed (byte order mark):
Example:
    \xEF\xBB\xBFlet x = 1
Before (diagnostic):
    Syntax error!
    A.res:1:1

    Not sure what to do with this character: "\239".
    … the same for "\187" and "\191"
After (diagnostic): none

Diagnostic removed (lone CR):
Example:
    let x = 1 // c\rlet y = 2
Before (diagnostic):
    Syntax error!
    B.res:1:10-18

    Consecutive statements on a line must be separated by ';' or a newline
After (diagnostic): none

Format modified (lone CR):
Example:
    let x = 1 // c\rlet y = 2
Before (text): Syntax error
After (rescript):
    let x = 1 // c
    let y = 2

Review: Why convert a CR-only file to LF when formatting? The formatter
writes CRLF when the first line break is CRLF and LF otherwise (#9116);
CR-only files are rare enough not to get their own newline.

Fixes #9108

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP
Signed-off-by: Christoph Knittel <[email protected]>
@cknitt
cknitt requested a review from cristianoc October 28, 2026 20:25
@cknitt
cknitt marked this pull request as ready for review October 28, 2026 20:25

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the separation of generated line endings from literal contents and the LF/CRLF regression tests. No blocking issues found. CI, including multi-printer and syntax tests, passes; not rerun locally.

@cknitt
cknitt merged commit b6458ea into master Oct 29, 2026
26 checks passed
@cknitt
cknitt deleted the fix-9100-format-line-endings-strings branch October 29, 2026 05:47
cknitt added a commit that referenced this pull request Oct 29, 2026
Skip a UTF-8 byte order mark and end a line at a lone CR

Why:
    Some Windows editors write a byte order mark, and bsc and bsc -format
    rejected it byte by byte. A CR without an LF ended a `//` comment but
    did not start a new line, so the next statement counted as on the same
    line. The scanner now starts after a byte order mark and ends a line
    at a CR not followed by LF, as editors and JavaScript do; code frames
    show lines the same way, and the formatter keeps the byte order mark.

Diagnostic removed (byte order mark):
Example:
    \xEF\xBB\xBFlet x = 1
Before (diagnostic):
    Syntax error!
    A.res:1:1

    Not sure what to do with this character: "\239".
    … the same for "\187" and "\191"
After (diagnostic): none

Diagnostic removed (lone CR):
Example:
    let x = 1 // c\rlet y = 2
Before (diagnostic):
    Syntax error!
    B.res:1:10-18

    Consecutive statements on a line must be separated by ';' or a newline
After (diagnostic): none

Format modified (lone CR):
Example:
    let x = 1 // c\rlet y = 2
Before (text): Syntax error
After (rescript):
    let x = 1 // c
    let y = 2

Review: Why convert a CR-only file to LF when formatting? The formatter
writes CRLF when the first line break is CRLF and LF otherwise (#9116);
CR-only files are rare enough not to get their own newline.

Fixes #9108


Claude-Session: https://claude.ai/code/session_01Kbf3gGkPma9LAZimuQjnrP

Signed-off-by: Christoph Knittel <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

bsc -format changes string literals that contain line breaks, and is not idempotent on \r\r\n

2 participants