util.parseEnv and --env-file keep a leading UTF-8 BOM in the first key

未關閉 適合新手
#67,102 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@helmipradita 已經在處理了。

開始於 2026年10月30日。

  • #67132 來自 @helmipradita —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
78/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
javascript
領域
backend

研究方向

先閱讀 src/node_dotenv.cc,尤其是 issue 中引用行的 Dotenv::ParseContent。檢查現有測試中 --env-file 和 util.parseEnv 對以 UTF-8 BOM 開頭的輸入的處理行為。完成標準是兩個入口都能解析不含 BOM 的第一個鍵,並保留預期值。

由索引模型根據 Issue 內容生成。

描述

Version

v24.18.0 (the code is the same on main)

Platform

Windows 11 x64 (not platform-specific; the file bytes are what matter)

Subsystem

dotenv (--env-file, util.parseEnv)

What steps will reproduce the bug?
printf '\xef\xbb\xbfA=1\r\nB=2\r\n' > .env
node --env-file=.env -e "console.log(process.env.A, process.env.B)"
node -e "console.log(require('util').parseEnv('A=1\nB=2'))"

Such files are common on Windows: PowerShell 5.1 Set-Content -Encoding utf8 / Out-File -Encoding utf8 and Notepad's "UTF-8 with BOM" write a leading EF BB BF.

How often does it reproduce? Is there a required condition?

Always, when the file starts with a UTF-8 BOM.

What is the expected behavior? Why is that the expected behavior?

1 2 and { A: '1', B: '2' }: the BOM is an encoding marker, not part of the first key. The dotenv package returns { A: '1', B: '2' } for the same bytes, and Node already skips a BOM when loading CommonJS/ESM sources and JSON.

What do you see instead?
undefined 2
{ B: '2', 'A': '1' }

The first variable is silently lost under its real name.

Additional information

Dotenv::ParseContent (src/node_dotenv.cc:147) strips \r but not a leading BOM, so it stays in the first key. Skipping a leading \xEF\xBB\xBF at the start of the input there would fix both --env-file and util.parseEnv. This also affects tools that moved to util.parseEnv, e.g. Vite's .env loading.

主要語言
JavaScript
星號
123k
分支
39.3k
平均合併
4 天 16 小時
30 天內合併 PR
227

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。