Skip to content

fix: load .env files for the Build Output's mode when --mode is omitted - #775

Merged
jamesopstad merged 2 commits into
mainfrom
james/prebuilt-mode-dotenv
Nov 25, 2026
Merged

jamesopstad merged 2 commits into
mainfrom
james/prebuilt-mode-dotenv

Conversation

@jamesopstad

Copy link
Copy Markdown
Contributor

Load .env files for the Build Output's mode when --mode is omitted

cf deploy, cf workers versions create, cf workers triggers deploy, and cf previews deploy now load .env.<mode> and .env.<mode>.local for the mode recorded in the Build Output when --mode is not passed. Previously they read only .env and .env.local, so deploying a staging build without repeating --mode staging could use the account and API token from .env instead of .env.staging.

Vite records its default production mode when a build runs without --mode, so these commands now also load .env.production for such builds, matching the files the build itself used. An explicit --mode must still match the recorded mode.

@pkg-pr-new

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

Copy link
Copy Markdown
npm i https://pkg.pr.new/cf@775

commit: 503deb5

@ask-bonk

ask-bonk Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

cf --help startup

mean stddev min max
PR 296.9 ms 7.0 ms 287.7 ms 321.7 ms
base 285.6 ms 4.8 ms 276.1 ms 294.0 ms

Δ (PR vs base): ↑ +11.3 ms (+4.0%)

Measured with hyperfine on ubuntu-latest (3 warmup runs, 20 timed runs). CI numbers carry ±a few ms of runner jitter; treat small deltas as noise.

@jamesopstad
jamesopstad marked this pull request as ready for review November 24, 2026 17:54
@jamesopstad
jamesopstad requested a review from a team as a code owner November 24, 2026 17:54

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

Copy link
Copy Markdown

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 503deb5f.

This review, of 165ea7cc

✅ Approved — No correctness, security or performance issues found. --local and --mode are still enforced at every call site, and each affected command has regression tests.

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


✅ All checks passed

🚀 Release: ✅ LGTM

The patch changeset matches the repository's convention for fixes. Release risk is moderate: deploys of builds made without --mode will now also load .env.production, which the changeset documents.


🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

The AGENTS.md addition is consistent with the code.


📋 Code Quality: ✅ LGTM

--local is still rejected by every caller. validateBuildOutputMode still runs, and the delegated build still runs before dotenv is applied. Each of the four commands has regression tests.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

The changeset and the AGENTS.md note describe the new behavior accurately.


Reviewed commit: 165ea7cc

@petebacondarwin

petebacondarwin commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Do we actually want to be loading .env.<...>.local for deployment commands like cf deploy and cf workers versions create?
Local is generally seen as development (or at least local machine) only variables.

OK, scratch that. I misunderstood.
This is just loading up the .env (and friends) files onto the process.env global for the duration of the command being run.
I thought it was passing those values through to the Worker code being deployed.

@jamesopstad

Copy link
Copy Markdown
Contributor Author

Do we actually want to be loading .env.<...>.local for deployment commands like cf deploy and cf workers versions create? Local is generally seen as development (or at least local machine) only variables.

We load the same files as the build (.env, .env.local, .env.<mode>, .env.<mode>.local), and that's unchanged by this PR. It only switches which mode is used, from --mode to the mode recorded in the Build Output.

.local here means machine-local and git-ignored rather than dev-only. Dev-only .local values belong in .env.development.local, which a production deploy won't load.

I'm working on the follow up that makes a CLOUDFLARE_ACCOUNT_ID that conflicts with the built accountId an error.

Comment thread packages/cli/AGENTS.md Resolved
@jamesopstad
jamesopstad force-pushed the james/prebuilt-mode-dotenv branch from 165ea7c to 503deb5 Compare November 25, 2026 08:27
@ask-bonk

ask-bonk Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No correctness, security or performance issues. --local and the --mode match are still enforced at every call site, and each affected command has regression tests.

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


✅ All checks passed

🚀 Release: ✅ LGTM

Release risk is moderate. Deploys can now pick up mode-specific account and API-token values, which can change where a deploy goes. The changeset documents this, and regression tests cover all four commands.


🔒 Security: ✅ LGTM

The mode recorded in the Build Output is passed to the .env loader without being checked as a file name. That data comes from the project's own build. Anyone who can write it can already write the project's .env files and the deployed code, so no trust boundary is crossed.


📖 AGENTS.md: ✅ LGTM

The AGENTS.md note matches the code.


📋 Code Quality: ✅ LGTM

--local is still rejected by every caller, and validateBuildOutputMode still enforces the --mode match. The delegated build runs before dotenv values are applied. process.env is restored afterwards. The regression tests cover all four commands, including the merge with the base .env.


⚡ Performance: ✅ LGTM

No performance issues found.


📝 Documentation: ✅ LGTM

The changeset and the doc comment describe the new behavior accurately, including the --local wording.


Reviewed commit: 503deb5f

@jamesopstad
jamesopstad added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit d4c61ea Nov 25, 2026
14 checks passed
@jamesopstad
jamesopstad deleted the james/prebuilt-mode-dotenv branch November 25, 2026 08:46
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.

2 participants