apply_patch drops the BOM when line 1 is replaced, and write_file rewrites CRLF and BOM files as LF without a BOM

クローズ 初心者向け
#285 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
tooling

調査の方向性

まず src/main/tools/apply_patch.ts の applyHunks と src/main/tools/files.ts の write_file を確認し、続いて issue に記載されている回帰テスト apply_patch.test.ts と tools.test.ts を読みます。既存ファイルの BOM と改行コードがどのように扱われているか確認します。先頭行を置き換えても BOM が保持され、CRLF と BOM を含むファイルを上書きしても両方が保持され、回帰ケースが通れば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug priority: medium severity: low

Found in the Oct 11 audit (at c5f5641). Follow-up to #246.

Where: src/main/tools/apply_patch.ts:133-157 (applyHunks); src/main/tools/files.ts:229-242 (write_file).

Cause:

  • The BOM is part of line 1's text. normalizeLoose trims it, so -Write-Host … matches \uFEFFWrite-Host …, and the replacement comes entirely from the + line. The #246 fix keeps the BOM only when line 1 is a context line.
  • write_file writes Buffer.from(content) as given; unlike applyEdit and applyHunks it keeps neither line endings nor the BOM.

Reproduction (confirmed on Windows):

  • b.ps1 = \uFEFFWrite-Host "héllo"\r\nline2\r\n, a patch replacing line 1 → Write-Host "hällo"\r\nline2\r\n (BOM gone).
  • Overwriting a BOM+CRLF file with write_file → LF only, no BOM.

Impact: Windows PowerShell 5.1 reads a .ps1 without a BOM as ANSI, so non-ASCII text becomes mojibake when the script runs; CRLF files show as entirely changed in diffs.

Fix: in applyHunks, add the BOM back when the original had one and the result does not. In write_file, when overwriting an existing file, convert to detectEol(before) and keep its BOM.

Regression test: "first line replaced in a BOM file" in apply_patch.test.ts; a CRLF+BOM overwrite in tools.test.ts.

主要言語
TypeScript
スター
2
フォーク
2
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。