github / github/copilot-cli

`edit` tool adds UTF-8 BOM (`EF BB BF`) to files on Windows

Đang mở
#3,389 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
area:platform-windows area:tools
Ngôn ngữ chính
Shell
Star
11.2k
Fork
1.9k
Merge trung bình
14 giờ 16 phút
Pull request đã merge (30 ngày)
6

Mô tả

### Describe the bug

On Windows, Copilot CLI's `edit` tool prepends a UTF-8 Byte Order Mark (`\xef\xbb\xbf`)
to the output file after a modification, even when the original file had no BOM.

### Affected version

v1.0.49

### Steps to reproduce the behavior

**Steps to reproduce**

1. Create a UTF-8 file without BOM (e.g., via PowerShell with explicit UTF-8 no-BOM write)
2. Use the `edit` tool to replace any `old_str` with a `new_str`
3. Read the output file's first 3 bytes:
```powershell
$b = [IO.File]::ReadAllBytes("test.md")
"{0:X2} {1:X2} {2:X2}" -f $b[0], $b[1], $b[2]
# Expected: first 3 bytes of actual content (e.g., "23 20 74" for "# t")
# Actual: EF BB BF ← UTF-8 BOM
```

### Expected behavior

no edit failure

### Additional context

**Evidence (v1.0.49, Windows 11 / PowerShell 7, 2026-05-19)**

Byte-level measurement before and after `edit` tool call:

```
Before edit: CRLF=3 LF-only=0 bytes=35 BOM=false
After edit: CRLF=3 LF-only=0 bytes=42 BOM=true
Hex header: EF BB BF 6C 69 6E 65 20 6F 6E 65 0D 0A ...
```

The `edit` tool correctly normalizes line endings for `old_str` matching (LF `old_str`
matched CRLF file content successfully), but the **output file gains a BOM** and retains
CRLF — so the normalization only applies to matching, not to the written output.

**Impact**

- Python `open(encoding='utf-8')` reads the BOM as the `\ufeff` character prepended to
the file content, breaking JSON parsers, YAML parsers, and string-prefix matching.
- `git diff` displays BOM bytes as binary noise, cluttering diffs.
- `.gitattributes eol=lf` does **not** strip BOM — git treats it as opaque file content.
Without a dedicated pre-commit hook, BOM is committed to the repository permanently.

**Requested change**

Use `new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)` (or equivalent) when
writing files on Windows, so that the output is BOM-free UTF-8 — the format expected by
virtually all modern tooling and consistent with behavior on macOS/Linux.

**Related**

- #1148 — CRLF injection (same root cause area: Windows file API defaults)

**Workaround (current)**

After using the `edit` tool, re-write the file with Python to strip BOM:
```python
with open(path, encoding='utf-8-sig') as f:
content = f.read() # strips BOM
with open(path, 'w', encoding='utf-8', newline='\n') as f:
f.write(content)
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện thao tác chỉnh sửa trên Windows với một tệp UTF-8 không có BOM, sau đó kiểm tra đường dẫn ghi tệp trên Windows của công cụ chỉnh sửa. Thay đổi được hoàn tất khi tệp đã sửa đổi vẫn không có BOM, đồng thời giữ nguyên nội dung và các ký tự kết thúc dòng CRLF hiện có; hãy xác minh ba byte đầu tiên bằng kiểm tra PowerShell từ issue.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
powershell
Lĩnh vực
cli, operating-systems
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
65/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.