"prettier --write" replaces LF by CRLF on Windows
Asked Answered
T

1

13

Some members of my team use Mac, some use Linux and others use Windows.

We started to use prettier --write before pushing changes to ensure uniformity in code style. The problem occurs on Windows, where all project files are changed. Prettier replaces all LF end of lines with CRLF.

We have "endOfLine": "lf" in our .prettierrc, but it seems to be ignored. Even adding --end-of-line lf in the command line is ignored.

This is not happening to Linux and Mac users.

How can we run Prettier to ensure line endings are preserved?

Tait answered 10/6, 2022 at 18:23 Comment(0)
L
4

According to https://prettier.io/docs/en/options.html#end-of-line the 'lf' value is already the default in current versions of prettier.

Other tools might be changing the end of lines too. How are you validating the where the change happens?

While investigating my own question about line endings, I saw a claim that many recent code editors support either format. In the past I'd use Notepad++ to both see what format a file is in, and not worry which format since it could adapt itself. There is a status area at the bottom that'll say which type of line ending the file has, and the editor can translate or match whichever type you need (might need a plugin, it's been a while since I needed that).

As another likely source I'd investigate your git configurations, as it can work some magic there. I believe the default is to use Linux/Mac format for text files in the repo, but will translate those to the local expected format on checkout (and reverse that for commits from a Windows box).

https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings

That page mentions .gitattributes and how to tell git what file types to alter and which to keep the same. I'll copy their example since it explains what they're doing and why. There is a lot more explanation on that page though.

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
Lapierre answered 1/11, 2022 at 2:24 Comment(3)
Thanks, I could solve the issue by putting: * text eol=lfCropland
Not really a solution to the underlying issue, though. What the OP reported is correct, run npx prettier . --write from the commandline will change each line-ending to CRLF. You can see this in a hex editor, or any tool that visually shows line endings, or the fact that Git shows many changes that only change in line ending.Finery
Were having the exact same issue. Did you find any actual solution?Fraxinella

© 2022 - 2024 — McMap. All rights reserved.