Prettier で Delete ␍ の改行コードエラーが消せない
Windows で checkout した CRLF ファイルに対して Prettier 既定の LF と差分が出るのが原因。
endOfLine: auto と .gitattributes の併用で恒久的に解決する。
公開: 更新:
要約
「Delete ␍ がファイル全行に出る」「Prettier を実行すると全行が変更扱いになる」現象は、CRLF と LF の改行コード差。
Prettier の endOfLine を "auto" にし、.gitattributes で LF に統一すれば再発しなくなる。
実行例
core.autocrlf true のまま取り出し直すと作業ツリーが CRLF になり、ESLint が全行に Delete ␍ を出して prettier --check も警告で終わる。.prettierrc に "endOfLine": "auto" を置くと ESLint は通り、.gitattributes で LF を指定して取り出し直すとファイル自体が LF に戻って警告も出なくなる。
$ git config core.autocrlf true$ rm src/index.js$ git checkout -- src/index.js$ file src/index.js
src/index.js: ASCII text, with CRLF line terminators$ npx eslint src/index.js
/tmp/tmp.jyaMyv0PuZ/src/index.js
1:13 error Delete `␍` prettier/prettier
2:13 error Delete `␍` prettier/prettier
3:20 error Delete `␍` prettier/prettier
✖ 3 problems (3 errors, 0 warnings)
3 errors and 0 warnings potentially fixable with the `--fix` option.
$ echo $?
1$ npx prettier --check src/index.js
Checking formatting...
[warn] src/index.js
[warn] Code style issues found in the above file. Run Prettier with --write to fix.
$ echo $?
1$ cat .prettierrc
{
"endOfLine": "auto"
}$ npx eslint src/index.js
$ echo $?
0$ cat .gitattributes
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf$ git add .gitattributes$ git add --renormalize .$ git status --short
A .gitattributes$ rm src/index.js$ git checkout -- src/index.js$ file src/index.js
src/index.js: ASCII text$ npx eslint src/index.js
$ echo $?
0— 2026-10-01 時点の出力
検証環境
- 検証日
- 実行環境
node:20Debian GNU/Linux 12 (bookworm)- バージョン
- Node.js 20.20.2
- npm 10.8.2
- Python 3.11.2
- Git 2.39.5
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
よくある原因
- Windows で
git config --global core.autocrlf trueになっており、checkout 時に CRLF へ変換される - Prettier の既定
endOfLineがlfのため、CRLF ファイルを「LF に直せ」と差分指示してくる .gitattributesがリポジトリに無いので OS ごとに改行コードが揺れる- VS Code の「Files: Eol」設定が
\r\nになっている
解決策
1. Prettier の設定を "auto" に
{
"endOfLine": "auto"
}"auto" はファイル先頭の改行コードを尊重するので、CRLF も LF も「現状維持」になる。
リポジトリ全体を LF で揃えたい場合は "lf" を指定し、次の .gitattributes と組み合わせる。
各値の意味は Prettier 公式ドキュメント(新しいタブで開く) を参照。
2. .gitattributes で改行コードを正規化
リポジトリルートに以下を置く:
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlftext=auto で Git が「テキストファイルらしいもの」を自動判定し、eol=lf で working tree に LF で展開する。.bat / .cmd だけは Windows の制約で CRLF を強制する。
3. 既存ファイルを再正規化
.gitattributes 追加直後は、既存コミット済みファイルの改行コードはそのまま。
以下で一括正規化する:
git add --renormalize .
git status # 改行コードだけが変わったファイルが大量に表示される
git commit -m "Normalize line endings to LF"4. エディタ側も合わせる
VS Code なら settings.json に:
{
"files.eol": "\n"
}ESLint と Prettier を併用している場合、ESLint 側の linebreak-style ルールも同じ向きに揃えないと、片方が CRLF を要求して堂々巡りになる。