Skip to content

fix: handle inner quotes in double-quoted env values in .env file#32930

Merged
bartlomieju merged 3 commits intodenoland:mainfrom
bartlomieju:fix/env-file-inner-quotes
Mar 25, 2026
Merged

fix: handle inner quotes in double-quoted env values in .env file#32930
bartlomieju merged 3 commits intodenoland:mainfrom
bartlomieju:fix/env-file-inner-quotes

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

Summary

  • Fix --env-file parsing of double-quoted values containing inner
    (unescaped) quotes, e.g. KEY="foo'bar"bazqux"`
  • Fix \n escape expansion in double-quoted values with inner quotes,
    e.g. KEY="foo\ni am "on" newline"

Root cause: find_char found the first matching " after the opening
quote, which was an inner quote — truncating the value. The dotenv npm
package instead treats the outermost quotes as delimiters.

Fix: New find_closing_quote function tries the nearest matching
quote first. If there's non-whitespace content after it on the same line,
inner quotes are present — it falls back to the last matching quote on the
line instead.

Closes #32928

Test plan

  • New unit tests in deno_dotenv: inner_quotes_in_double_quoted_values
    and inner_single_quotes_preserved
  • New spec tests eval::env_file_inner_quotes (both inner_quotes and
    inner_quotes_with_newline)
  • All 32 dotenv unit tests pass
  • All 12 env_file spec tests pass

🤖 Generated with Claude Code

bartlomieju and others added 2 commits March 23, 2026 14:17
When a double-quoted value contains inner (unescaped) quotes like
`KEY="foo'bar"baz`qux"`, the parser would stop at the first inner
quote instead of treating the outermost quotes as the delimiters.

Fix: introduce `find_closing_quote` which first tries the nearest
matching quote. If there's non-whitespace content after it on the
same line, inner quotes are present — fall back to the last matching
quote on the line. This matches the behavior of the dotenv npm package.

Also handles escaped newlines correctly within values that contain
inner quotes.

Closes denoland#32928

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- When content follows the first closing quote but contains no more
  matching quotes (e.g. KEY='VALUE'JUNK), use the first quote as the
  closer (matching Node.js/dotenv behavior)
- Rename .env to env_file in spec test to avoid .gitignore exclusion

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju bartlomieju changed the title fix(dotenv): handle inner quotes in double-quoted env values fix: handle inner quotes in double-quoted env values in .env file Mar 23, 2026
@bartlomieju bartlomieju requested a review from Copilot March 23, 2026 15:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Deno’s --env-file dotenv parsing to correctly handle double-quoted values containing inner (unescaped) quotes, aligning behavior more closely with the dotenv npm package and preventing truncation/incorrect \n expansion in those cases.

Changes:

  • Replace first-quote matching with a new find_closing_quote heuristic to prefer the “outermost” quote when inner quotes are detected.
  • Add new Rust unit tests in libs/dotenv for inner-quote parsing and \n expansion behavior.
  • Add new spec tests under tests/specs/eval/env_file_inner_quotes/ to validate CLI behavior with --env-file.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
libs/dotenv/lib.rs Updates quote-closing detection and adds regression tests for inner quotes + newline escape expansion.
tests/specs/eval/env_file_inner_quotes/env_file Adds .env fixture containing inner quotes and \n escape sequences.
tests/specs/eval/env_file_inner_quotes/__test__.jsonc Adds spec tests validating deno eval --env-file outputs for the new fixture.

Comment thread libs/dotenv/lib.rs Outdated
The backward scan now prefers a quote candidate followed by a `#`
comment (strong signal) over one followed by just whitespace/EOL. This
correctly handles the edge case where an inline comment contains the
same quote character, e.g. `KEY="a"b" # "comment"` now yields `a"b`
instead of including the comment text.

Also adds tests for same-type inner quotes and inline comments with
inner quotes, and clarifies existing test comments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bartlomieju bartlomieju merged commit ea9214c into denoland:main Mar 25, 2026
220 of 222 checks passed
@bartlomieju bartlomieju deleted the fix/env-file-inner-quotes branch March 25, 2026 07:37
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.

--env-file: inner quotes and escaped newlines not parsed correctly

2 participants