Skip to content

fix(tools): create new files with the platform-native line ending - #351

Open
sonderhq wants to merge 1 commit into
lessweb:mainfrom
sonderhq:fix/new-file-platform-line-endings
Open

sonderhq wants to merge 1 commit into
lessweb:mainfrom
sonderhq:fix/new-file-platform-line-endings

Conversation

@sonderhq

Copy link
Copy Markdown

Problem

A newly created file was written with LF endings whenever the model's content carried no CRLF:

const lineEndings = existingMetadata?.lineEndings ?? (input.content.includes("\r\n") ? "CRLF" : "LF");

Created files have no existing EOL to preserve, so on Windows they ended up with LF while native tooling (and the files a user's editor creates) use CRLF. Existing files were unaffected — their recorded line endings already win, and reading already has detectLineEndings() for exactly that purpose.

Fix

platformLineEnding() in packages/core/src/common/file-utils.ts supplies the default for created files. It mirrors detectLineEndings() on the read side and takes the platform EOL as an injectable parameter so both branches are testable off-Windows. On LF platforms the behaviour is unchanged (no-op).

Tests

New packages/core/src/tests/write-handler-line-endings.test.ts:

  • platformLineEnding("\r\n") / platformLineEnding("\n")CRLF / LF (both branches, any host platform);
  • a created file matches the platform-native ending (Windows CI exercises the CRLF branch for real);
  • an existing CRLF file keeps its endings when rewritten with LF content (no regression to the preservation path).

Verification:

  • node src/tests/run-tests.mjs (core): 385 tests, 383 pass, 0 fail (2 skipped);
  • npm run typecheck, npm run lint, prettier --check clean; pre-commit hook passed.

A newly created file used LF whenever the model's content carried no CRLF, so
files written on Windows ended up with LF endings while native tooling uses
CRLF. Existing files were unaffected: their recorded line endings already win.

platformLineEnding() (file-utils) now supplies the default for created files,
mirroring the existing detectLineEndings() helper for reads. On LF platforms
this is a no-op.

Tests cover the CRLF/LF branches of the helper, a created file matching the
platform ending, and an existing CRLF file keeping its endings.
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.

1 participant