Skip to content

fix: align the fast parser with the default parser - #1044

Open
chuanghiduoc wants to merge 1 commit into
motdotla:masterfrom
chuanghiduoc:fix/fast-parser-parity
Open

fix: align the fast parser with the default parser#1044
chuanghiduoc wants to merge 1 commit into
motdotla:masterfrom
chuanghiduoc:fix/fast-parser-parity

Conversation

@chuanghiduoc

Copy link
Copy Markdown

Follow-up to #1043. Aligns the fast parser with the default parser on all three divergence classes from the issue.

Changes

1. Quoted value starting on a later line after KEY=

The default regex's quoted alternative carries a greedy leading \s* that crosses newlines, so a quote on a later line is consumed as the value. The scanner now detects when a newline was crossed after the separator and, if the next non-blank char is a quote, parses the value with full multiline support:

require('dotenv').parse('A=\n\n"hello\nworld"', { fast: true })
// before: { A: '' }
// after:  { A: 'hello\nworld' }   (matches default)

Junk after the closing quote on that line still kills the whole alternative (the raw fallback cannot cross newlines), matching A=\n\n"x" junk{ A: '' }.

2. \f, \v, U+00A0 before a key

Added to the whitespace-skip set alongside space/tab/newline/BOM:

parse('\u00A0A=1', { fast: true })
// before: {}
// after:  { A: '1' }

3. Trailing junk after a closing quote

TOKEN="abc" oops now keeps the raw single-line value "abc" oops (the quoted form only survives when nothing but spaces/tabs or a comment follows the closing quote — anything else makes the raw [^\r\n#]+ fallback take over starting at the opening quote, exactly like the regex backtracks).

Tests

  • tests/test-fast-parity.js — parity assertions for the three issue cases; failed on master (5 fail), passes now.
  • Existing test-parse-fast.js cross-checks against tests/.env and the multiline fixture: failed on master, passes with this change.

Full suite on Windows: 201 total / 197 pass / 3 skip-adjacent fails — the 3 remaining failures (deals with file:// path, displays the injected env message without tips) are pre-existing environment failures that also fail on unmodified master.

parse(src, { fast: true }) diverged from parse(src) on three classes of
input:

1. A quoted value starting on a later line after `KEY=` — the default
   regex's quoted alternative carries a greedy leading \s* that crosses
   newlines, so the quote is consumed as the value. The fast scanner ended
   the value at end-of-line, leaving the quoted text to be dropped. With a
   blank line in between this silently turned secrets into ''.

2. \f, \v and U+00A0 before a key — the default parser skips them as \s,
   the fast scanner discarded the whole line.

3. Trailing junk after a closing quote — 'TOKEN="abc" oops' now keeps the
   raw single-line value '"abc" oops' like the default parser instead of
   returning the clean quoted content.

The scanner now mirrors the regex engine: when a newline was crossed after
the separator only a quoted alternative can supply the value (the raw one
cannot span lines), any junk after its closing quote kills that alternative
leaving an empty value, and scanning resumes at the stray token so it can
still be parsed as its own entry.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant