Repository navigation
fix(env): keep env.ts in Worker builds of apps without side effects - #745
Open
morinokami wants to merge 1 commit into
Open
morinokami wants to merge 1 commit into
morinokami wants to merge 1 commit into
Conversation
When an app's package.json declares "sideEffects": false, a production build dropped env.ts from the Worker. The generated entry imports env.ts only for its side effect, registering the schema, so the bundler removed that import: env read raw values, and boot validation passed anything. envSchemaPlugin now marks env.ts as having side effects in server builds. Rolldown honors the plugin's value over the package's sideEffects field. The transform filter matches the env file's path as found and as Vite resolves it, so a symlinked env.ts is covered whatever its target is named. Client builds are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch has not been deployed
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.
When an app's
package.jsondeclares"sideEffects": false, a production build leavesenv.tsout of the Worker, soenvfromvoid/envreturns raw values:With
DEBUG=falseand noPORTin the Worker's env,env.PORTisundefinedandenv.DEBUGis the string"false", soif (env.DEBUG)takes the debug branch. No schema is registered, so the boot validation passes any value, and a missing required key goes unnoticed.vp devdoesn't bundle, so it reads validated values, and the difference only shows after a build.The generated entry imports
env.tsonly for its side effect, registering the schema, and uses none of its exports. The bundler follows the app'ssideEffectsfield and drops the import. An array that doesn't matchenv.ts, such as["**/*.css"], drops it too. An app whose code imports theenvthatenv.tsexports keeps it.The field is uncommon in apps, but some starters set it: most of TanStack Start's official examples declare
"sideEffects": false.Fix
In server builds,
envSchemaPluginreturnsmoduleSideEffects: trueforenv.tsfrom atransformhook. Rolldown uses this value over the package'ssideEffectsfield.env.tsis covered whatever its target is named.void/envreadsimport.meta.envand doesn't use the schema.Tests
packages/void/test/integration/plugin-env-schema.test.tsbuilds an entry that importsenv.tsfor its side effect, in an app that declares"sideEffects": false:env.ts, also whenenv.tsis a symlink to a file with another name. Both cases fail without this fix.Verification
vp test run: 781 files pass, 1 skipped (16,170 tests pass, 29 skipped).vp run check: 33/33 pass.playground/ssrwith theenv.tsabove,"sideEffects": false,DEBUG=falsein.env, and a route that returns both values: invp preview, the route readsPORTas3000andDEBUGasfalse. Without this fix, the Worker bundle has noenv.ts, and the route readsundefinedand"false".vp devreads the validated values without this fix too.["**/*.css"]instead: an app built withenvSchemaPluginand an entry that importsenv.tsfor its side effect dropsenv.tswithout this fix and keeps it with it.void deployagainst Cloudflare.🤖 Generated with Claude Code