Skip to content

[wrangler] Reject non-UTF-8 .dev.vars and .env files instead of loading no variables - #16719

Merged
petebacondarwin merged 3 commits into
cloudflare:mainfrom
jaeminbird:fix/dev-vars-utf16
Nov 18, 2026
Merged

petebacondarwin merged 3 commits into
cloudflare:mainfrom
jaeminbird:fix/dev-vars-utf16

Conversation

@jaeminbird

@jaeminbird jaeminbird commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #16718.

"API_KEY=abc" > .dev.vars in Windows PowerShell 5.1 writes UTF-16LE. Wrangler read the file as UTF-8, dotenv found no variables, and wrangler dev still printed Using secrets defined in .dev.vars while the Worker saw env.API_KEY as undefined.

This follows #16521, which made the same check for .assetsignore: the new readEnvFile decodes with TextDecoder("utf-8", { fatal: true }) and rejects null characters (BOM-less UTF-16). A bad .dev.vars stops wrangler dev with a UserError that says to save the file as UTF-8. A bad .env is skipped with one warning, because .env files are also loaded by every wrangler command, not just dev. UTF-8 files, with or without a BOM, load as before.


  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: bug fix; valid UTF-8 files behave as before.
Testing
  • dev.test.ts: 3 new tests (UTF-16LE .dev.vars with and without a BOM, and a UTF-16LE .env with a BOM) fail on main and pass with the fix on Windows 11 and on Linux (node:24). A control test, a UTF-8 .dev.vars with a BOM and CRLF, passes on both.
  • Built CLI on Windows: the UTF-16 .dev.vars now gives the error; a UTF-8 file still shows both bindings.
  • dev, loadDotEnv, case-insensitive-env, print-bindings, secret and type-generation tests pass (378). oxlint, oxfmt and tsc are clean.
  • The full monorepo pnpm check and remote E2E tests were not run.

Devin Review

@changeset-bot

changeset-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6583573

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@cloudflare/vitest-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Hi @jaeminbird. Thanks for taking the time to contribute to Workers SDK.

This PR has been automatically closed because you are not currently a registered (vouched) collaborator for this repository.

AI tools now let our team generate proposed code changes quickly. Reviewing changes, checking that they solve the right problem, and maintaining code quality over time still take substantial care and effort. We therefore limit PRs to registered collaborators so we can focus that effort where it is most useful.

For the majority of external contributors, the most effective way to help is through issues: describing a problem or use case, providing a minimal reproduction, investigating its cause, or helping us verify a fix. Those contributions help us understand what needs to change and are very welcome.

We will take an initial look at this PR and decide whether it is something we want to invest time in reviewing and maintaining. We may reopen it if we decide to proceed, but we cannot guarantee that it will be reopened or receive a full review.

@github-actions github-actions Bot closed this Nov 5, 2026
@github-project-automation github-project-automation Bot moved this from Untriaged to Done in workers-sdk Nov 5, 2026
@workers-devprod
workers-devprod requested review from a team and dario-piotrowicz and removed request for a team November 5, 2026 03:46
@workers-devprod

workers-devprod commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • ✅ @cloudflare/wrangler
Show detailed file reviewers

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@github-project-automation github-project-automation Bot moved this from Done to Untriaged in workers-sdk Nov 5, 2026
@clanky-the-clanker

clanky-the-clanker Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

This pull request's reviews are posted as pull request reviews: see the review of 7459bf43.

@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@16719

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@16719

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@16719

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@16719

@cloudflare/containers-shared

npm i https://pkg.pr.new/@cloudflare/containers-shared@16719

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@16719

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@16719

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@16719

miniflare

npm i https://pkg.pr.new/miniflare@16719

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@16719

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@16719

@cloudflare/runtime-types

npm i https://pkg.pr.new/@cloudflare/runtime-types@16719

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@16719

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@16719

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@16719

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@16719

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@16719

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@16719

wrangler

npm i https://pkg.pr.new/wrangler@16719

commit: 6583573

@dario-piotrowicz

Copy link
Copy Markdown
Member

@clanky-the-clanker review

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Superseded by the review of 65835736.

This review, of 7459bf43

💬 Approved with comments — The fix is correct and well tested. The only note is an optional readability point about the try/catch layout in tryLoadDotDevDotVars.

Findings: 1 suggestion (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 suggestion

The change is correct. One optional readability point in dev-vars.ts.

Recommendation: 💬 Optional Nits

  • [SUGGESTION] tryLoadDotDevDotVars now reads the file outside its try/catch, so the "Failed to load local dev variables file" wrapper no longer covers the read. (packages/wrangler/src/dev/dev-vars.ts:158)
View full review
  • packages/wrangler/src/dev/dev-vars.ts:158: readEnvFile(basePath) now runs before the try. That is intentional: wrapping it would hide the "save as UTF-8" UserError behind a generic Error. The try now only guards dotenv.parse, though, and the layout still reads as if the read were protected.

No problems found in the rest of the change:

  • The loadDotEnv rewrite keeps the override semantics.
  • UTF-8 BOM handling is the same on both paths.
  • logger.once.warn prevents repeated warnings for the same file.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The changeset accurately describes the change.


Reviewed commit: 7459bf43

Comment thread packages/wrangler/src/dev/dev-vars.ts Resolved

@dario-piotrowicz dario-piotrowicz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR @jaeminbird 🙏

Generally looks good to me, with a few suggestions 🙂

Comment thread .changeset/dev-vars-utf16-encoding.md Resolved
Comment thread packages/wrangler/src/dev/dev-vars.ts Resolved
Comment thread packages/wrangler/src/config/dot-env.ts Resolved

@workers-devprod workers-devprod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Codeowners reviews satisfied

@jaeminbird

Copy link
Copy Markdown
Contributor Author

Pushed af969c1: removed the try/catch in tryLoadDotDevDotVars, used the suggested changeset text and dropped the PowerShell mention from the error message. Thanks @dario-piotrowicz.

@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • ✅ @cloudflare/wrangler
Show detailed file reviewers

@jaeminbird

Copy link
Copy Markdown
Contributor Author

Merged current main in (6583573); the .dev.vars/.env tests (dev, dev-vars, loadDotEnv, secrets) still pass. Anything else needed before this can go in?

@cjol

cjol commented Nov 12, 2026

Copy link
Copy Markdown
Member

@clanky-the-clanker review

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Approved — No issues found. The earlier note about the try/catch layout is fixed, and the encoding check is correct and covered by tests.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The new messages contain only the file path, never variable values. The change adds no injection or secret-exposure risk.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The try/catch in tryLoadDotDevDotVars was removed, so the encoding UserError reaches the user unwrapped. The code no longer looks as if the read were guarded.

readEnvFile returns undefined on ENOENT and on ENOTDIR. loadDotEnv merges files with Object.assign, which keeps the override behaviour. A bad .env produces a single logger.once.warn. The tests cover:

  • UTF-16LE .dev.vars with and without a BOM
  • UTF-16LE .env
  • a UTF-8 file with a BOM, as a control

⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The changeset accurately describes the new behaviour.


Reviewed commit: 65835736

@petebacondarwin
petebacondarwin merged commit f833c8a into cloudflare:main Nov 18, 2026
84 checks passed
@github-project-automation github-project-automation Bot moved this from Untriaged to Done in workers-sdk Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[wrangler] wrangler dev loads no variables from a UTF-16 .dev.vars or .env file, but says it is using them

5 participants