Skip to content

Fix the formatter's line endings on Windows and in CRLF files - #9042

Merged
cknitt merged 3 commits into
masterfrom
windows-crlf-format
Oct 26, 2026
Merged

cknitt merged 3 commits into
masterfrom
windows-crlf-format

Conversation

@cknitt

@cknitt cknitt commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

The formatter now keeps each file's line endings. This fixes two long-standing problems with CRLF line endings, and lets Windows CI run the syntax roundtrip test and the format checks.

Problems

1. On Windows, LF files were rewritten to CRLF. bsc -format wrote its output to stdout in text mode, which on Windows turns every \n into \r\n. As a result:

  • rescript format rewrote LF files with CRLF line endings.
  • rescript format --check reported every LF file as unformatted.

Both the Rust and the OCaml rescript use bsc's output as it is, so both behaved this way.

2. Formatting a CRLF file was not stable, on any platform. The scanner keeps the \r characters inside multi-line comments, template literals and %raw as part of their content. Those lines came out as \r\n, next to the printer's plain \n. Text-mode output, or an editor converting \n to the document's CRLF, then made them \r\r\n. Each run added another blank line.

This second problem has been reported several times since 2021. Each time it was closed with the workaround "switch your editor to LF":

Changes

  • New Res_line_endings module. Formatting reads the source with LF line endings. If the source's first line break is CRLF, the formatted result gets CRLF line endings back, so CRLF files stay CRLF.
  • bsc -format writes stdout in binary mode. Compiler_request_output.write_stdout_binary writes the bytes unchanged in both cases:
    • when bsc runs as its own process;
    • when bsc runs inside another process. The OCaml rescript formats this way, and there the captured output is otherwise converted the way a Windows text-mode stdout would.
      Text written before or after the binary output still gets the usual conversion.
  • The editor format command in rescript-editor-analysis keeps a CRLF document's line endings instead of returning mixed ones.
  • res_parser, which the syntax tests use, reads sources with LF line endings and writes its -print res output in binary mode.

CI

  • Windows CI now runs the syntax roundtrip test, which previously failed for 15 files there.
  • format_check.sh now runs the OCaml and ReScript format checks on every platform, instead of skipping them on Windows.
  • format_check.sh and format.sh pass their file lists through xargs, so each command stays under the Windows command-line length limit.

Tests

  • Syntax test (all platforms): formats an LF and a CRLF file containing a multi-line comment, a template literal and %raw. It checks that each keeps its line endings and that a second run changes nothing.
  • OUnit test: binary output while bsc's output is captured in memory, including when that output is later moved to a temporary file.
  • Rewatch test (format/05-format-line-endings.sh): runs format --check and format on LF and CRLF files. With text-mode output, it fails on the LF file.

🤖 Generated with Claude Code

On Windows, bsc -format wrote its output through text-mode stdout, so every
LF became CRLF: rescript format rewrote LF files to CRLF and format --check
reported every LF file. Formatting a CRLF file was not stable either, on any
platform: the scanner keeps the CRs inside comments, template literals and
raw JS, so those lines came out as CR LF beside the printer's plain LF, and
text-mode output or an editor turned them into CR CR LF, adding a blank line
on each run.

Formatting now reads the source with LF line endings and gives the result
CRLF ones when the source's first line break is CRLF. bsc -format and
res_parser -print res write stdout in binary mode, also inside a captured
compiler request, and the editor format command keeps a CRLF document's line
endings. With that, Windows CI runs the syntax roundtrip tests and the OCaml
and ReScript format checks, whose file lists now go through xargs to stay
under the Windows command-line length limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Make the formatter keep each file's line endings. On Windows it no longer rewrites LF files with CRLF or reports them in rescript format --check, and formatting a CRLF file no longer adds a carriage return inside multi-line comments, template literals and raw JS on every run.

🏠 Internal

  • Run the syntax roundtrip tests and the OCaml and ReScript format checks on Windows CI.

Semantic Diff

3 commits · Full page

Keep each file's line endings when formatting

1cbe977 · cknitt + Claude · Oct 25, 2026, 16:09 UTC

On Windows, bsc -format wrote its output through text-mode stdout, so every LF became CRLF: rescript format rewrote LF files to CRLF and format --check reported every LF file. Formatting a CRLF file was not stable either, on any platform: the scanner keeps the CRs inside comments, template literals and raw JS, so those lines came out as CR LF beside the printer's plain LF, and text-mode output or an editor turned them into CR CR LF, adding a blank line on each run.

Formatting now reads the source with LF line endings and gives the result CRLF ones when the source's first line break is CRLF. bsc -format and res_parser -print res write stdout in binary mode, also inside a captured compiler request, and the editor format command keeps a CRLF document's line endings. With that, Windows CI runs the syntax roundtrip tests and the OCaml and ReScript format checks, whose file lists now go through xargs to stay under the Windows command-line length limit.

Expect formatted files to keep their line endings in rewatch tests

88297dc · cknitt + Claude · Oct 25, 2026, 17:45 UTC

The rewatch format unit tests expected formatted output to have CRLF line endings on Windows, which was the text-mode output bug. They now expect each file to keep its own line endings, and check that a formatted CRLF file passes format --check and is left unchanged by format, through the in-process formatter.

✨ Read syntax test file lists before running tests in the background

25243f2 · cknitt + Claude · Oct 25, 2026, 17:56 UTC · new

The syntax test script read each file list in a while loop from a file and started a background job per file. Under MSYS on Windows that occasionally corrupted the names the loop read, so a test file was skipped, or, in the roundtrip tests now running on Windows, reported as a failure. The script now reads each list into an array before it starts any job.

@cknitt cknitt changed the title Keep each file's line endings when formatting Fix the formatter's line endings on Windows and in CRLF files Oct 25, 2026
@cknitt

cknitt commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

@codex review

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.50000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.33%. Comparing base (7593903) to head (25243f2).

Files with missing lines Patch % Lines
analysis/src/commands.ml 0.00% 4 Missing ⚠️
compiler/syntax/src/res_line_endings.ml 94.44% 1 Missing ⚠️
tests/ounit_tests/ounit_compiler_request_tests.ml 93.75% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9042      +/-   ##
==========================================
+ Coverage   81.32%   81.33%   +0.01%     
==========================================
  Files         456      457       +1     
  Lines       64036    64106      +70     
==========================================
+ Hits        52077    52141      +64     
- Misses      11959    11965       +6     
Files with missing lines Coverage Δ
compiler/bsc/rescript_compiler_driver.ml 81.22% <100.00%> (ø)
compiler/ext/compiler_request_output.ml 81.81% <100.00%> (+5.27%) ⬆️
compiler/syntax/cli/res_cli.ml 75.00% <100.00%> (+1.66%) ⬆️
compiler/syntax/src/res_driver.ml 88.46% <100.00%> (ø)
compiler/syntax/src/res_multi_printer.ml 76.92% <100.00%> (+1.92%) ⬆️
compiler/syntax/src/res_line_endings.ml 94.44% <94.44%> (ø)
tests/ounit_tests/ounit_compiler_request_tests.ml 97.10% <93.75%> (-0.06%) ⬇️
analysis/src/commands.ml 62.50% <0.00%> (-1.33%) ⬇️
🚀 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.

@cknitt
cknitt requested a review from cristianoc October 25, 2026 16:31
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 1cbe9772cd

ℹ️ 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".

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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 🔄 Running since 2026-10-25T17:25:15.203958Z 25243f2 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.

@pkg-pr-new

pkg-pr-new Bot commented Oct 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 25243f2

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

@codex review

@cknitt
cknitt enabled auto-merge (squash) October 25, 2026 17:35
cknitt and others added 2 commits October 25, 2026 18:45
The rewatch format unit tests expected formatted output to have CRLF line
endings on Windows, which was the text-mode output bug. They now expect each
file to keep its own line endings, and check that a formatted CRLF file
passes format --check and is left unchanged by format, through the
in-process formatter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
The syntax test script read each file list in a while loop from a file and
started a background job per file. Under MSYS on Windows that occasionally
corrupted the names the loop read, so a test file was skipped, or, in the
roundtrip tests now running on Windows, reported as a failure. The script
now reads each list into an array before it starts any job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt requested a review from fhammerschmidt October 26, 2026 09:13
@cknitt
cknitt merged commit 533bff8 into master Oct 26, 2026
26 checks passed
@cknitt
cknitt deleted the windows-crlf-format branch October 26, 2026 12:55
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.

2 participants