Repository navigation
fix: skip a UTF-8 BOM in the fast parser - #1041
Merged
Merged
Conversation
The character scanner used by { fast: true } skipped only spaces, tabs and
newlines between entries, so a leading BOM was treated as the start of a key.
It matches no key character, the line was discarded as invalid, and the first
variable in the file silently went missing.
The classic regex parser consumes the BOM as part of its leading \s*, so the
two parsers disagreed on any .env saved with a UTF-8 signature — the default
encoding for PowerShell redirects and Windows Notepad.
Owner
|
great catch. thank you @Jaybhade |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
parse(src, { fast: true })silently drops the first variable when the.envfile starts with a UTF-8 BOM. The classic parser reads the same file correctly, so this is a parity gap in the opt-in scanner rather than a change in documented behaviour.Reproducing
Through the CLI, the same file loses a variable with no warning at all — it just reports one fewer:
Why it happens
Between entries,
parseFastskips only space (32), tab (9) and newline (10). A BOM is none of those, so the scanner treatsas the start of a key. It matches noKEY_CHAR,i === keyStart, and the line is discarded as invalid — taking the first variable with it.parseRegexnever had the problem becauseLINEopens with\s*, and JavaScript's\sincludes.This matters in practice because a BOM is what PowerShell's
>/Out-Fileand Windows Notepad write by default, andfs.readFileSync(path, { encoding: 'utf8' })preserves it. The.envlooks completely normal in an editor.The fix
One extra code in the whitespace-skip loop, so the scanner steps over a BOM exactly where the regex's
\s*does — which covers both a leading BOM and one after a newline (concatenated files):I deliberately kept this to the BOM rather than widening the loop to all of
\s. Exotic spaces such as NBSP still differ between the two parsers (KEY=valueindented with U+00A0 parses classically but not withfast), but nothing writes those into a.envby accident, and matching them would cost more comparisons in the hot loop for no real-world gain. Happy to widen it if you'd rather the two be exactly equivalent.Tests
Added to
tests/test-parse-fast.js, using the existingassertSameParsehelper:export, before a comment, a BOM-only source, after a blank line, and mid-fileconfig({ fast: true })test against a newtests/.env.bomfixture (realEF BB BFbytes), covering thereadFileSyncpath rather than string literals onlynpm testis green: 181 pass, 1 skip, 0 fail — build,standard,dts-checkand TAP included. All eight new assertions fail onmainbefore the change.Performance
The scanner exists for speed, so I measured it rather than assuming. Median of 7 runs × 5000
parse(buf, { fast: true })calls overtests/.env× 8, patched vs. unpatched:Delta −0.6% on one run and −0.3% on a repeat — noise. The added comparison only executes on characters the loop was already inspecting.