Skip to content

Commit dfc5543

Browse files
h1whelanclaude
andcommitted
Fix: strip UTF-8 BOM from .env files to prevent silent first-variable loss
When a .env file is saved with a UTF-8 BOM (common with JetBrains IDEs on Windows), the BOM character (\ufeff) was prepended to the first variable name, making it silently inaccessible via its intended key. Strip the BOM in Reader.__init__ so all variables are parsed correctly regardless of whether the file contains a BOM. Fixes #637 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fa4e6a9 commit dfc5543

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/dotenv/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Error(Exception):
6868

6969
class Reader:
7070
def __init__(self, stream: IO[str]) -> None:
71-
self.string = stream.read()
71+
self.string = stream.read().removeprefix("\ufeff")
7272
self.position = Position.start()
7373
self.mark = Position.start()
7474

tests/test_parser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,35 @@
545545
),
546546
],
547547
),
548+
# UTF-8 BOM at the start of the file should be stripped
549+
(
550+
"\ufeffa=b",
551+
[
552+
Binding(
553+
key="a",
554+
value="b",
555+
original=Original(string="a=b", line=1),
556+
error=False,
557+
)
558+
],
559+
),
560+
(
561+
"\ufeffa=b\nc=d",
562+
[
563+
Binding(
564+
key="a",
565+
value="b",
566+
original=Original(string="a=b\n", line=1),
567+
error=False,
568+
),
569+
Binding(
570+
key="c",
571+
value="d",
572+
original=Original(string="c=d", line=2),
573+
error=False,
574+
),
575+
],
576+
),
548577
],
549578
)
550579
def test_parse_stream(test_input, expected):

0 commit comments

Comments
 (0)