Skip to content

fix: an imported @currentEnv must win over the --env fallback - #1050

Merged
theoephraim merged 1 commit into
mainfrom
fix-imported-currentenv-fallback
Aug 30, 2026
Merged

fix: an imported @currentEnv must win over the --env fallback#1050
theoephraim merged 1 commit into
mainfrom
fix-imported-currentenv-fallback

Conversation

@theoephraim

Copy link
Copy Markdown
Member

Fixes @currentEnv arriving via @import() losing to the --env fallback, which made per-app .env.[env] files unusable in the documented monorepo layout under the Next.js integration (it always passes --env development/production).

This is @chriscors's original fix from #1036 (commit b012969), cherry-picked as-is so the authorship stays with him. The later revision on that PR was pushed in response to automated review feedback that turned out to be asking for two mutually exclusive behaviors; this original version has the semantics we want.

What this settles

--env is now a true last resort, matching what the CLI help already documents ("ignored if using @currentEnv"):

  • A resolved @currentEnv (declared in the schema or inherited from a parent) is final and its .env.[env] files load before imports, unchanged.
  • A fallback value is not acted on before imports. After imports are processed, if a real @currentEnv exists anywhere (including via an import), it wins. Only if none exists does the fallback apply.
  • Nothing is loaded provisionally, so no .env.<fallback> values linger in the graph after the real env is known.

Deliberate behavior change

Import conditions (enabled=...) can no longer read values that only exist in .env.<fallback> files, since those files now load after imports. That previously worked only because the fallback resolved early; it never worked when @currentEnv was real but imported (those env files necessarily load after imports too), so this makes the fallback path consistent rather than privileged. Only affects setups with no @currentEnv at all that gate imports on per-env file values.

Supersedes #1036.

A directory whose schema gets `@currentEnv` through `@import()` never loaded its
own `.env.[env]` files when a fallback env was supplied.

`DirectoryDataSource._finishInit()` resolves `currentEnv` before imports are
processed, which is too early for an imported `@currentEnv` — so it falls
through to `graph.envFlagFallback`. That value is truthy, so the post-import
re-check (guarded on `!currentEnv`) never runs, and the directory stays pinned
to the fallback env for the rest of the load.

There is no fallback in the CLI's default path, so this only shows up when one
is set — which the Next.js integration always does, passing `--env
development`/`production` from `next dev`/`next build` to match `@next/env`.
Its own comment says the user should be able to "ignore it by setting their own
`@currentEnv`", and that holds when the decorator is in the same schema; it does
not when the schema imports it. In a monorepo where each app's `.env.schema` is
just `@import(../../)` of a root schema that owns `@currentEnv=$APP_ENV`, the
root's `.env.[APP_ENV]` loads correctly while the app's own is silently skipped
in favour of `.env.production` — with no error, and nothing in the loaded-file
list to suggest the app file was considered.

`_resolveCurrentEnv()` now reports whether the value came from the fallback.
A fallback is treated as provisional: it is not acted on before imports, and the
post-import re-check runs for it. Deliberately not acted on early, rather than
loaded and later overridden — loading `.env.<fallback>` leaves its values in the
graph even once the real env is known, so a key present only there would leak
into the wrong environment. A resolved `@currentEnv` is still final and still
loads before imports, so import conditions can read those values.
@github-actions

Copy link
Copy Markdown
Contributor

bumpy-frog

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

patch Patch releases

  • @varlock/native-helper-darwin 1.17.1 → 1.17.2
  • @varlock/native-helper-linux-arm64 1.17.1 → 1.17.2
  • @varlock/native-helper-linux-x64 1.17.1 → 1.17.2
  • @varlock/native-helper-win32-x64 1.17.1 → 1.17.2
  • varlock 1.17.1 → 1.17.2

Bump files in this PR

Click here if you want to add another bump file to this PR


This comment is maintained by bumpy.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Bundle size

⚠️ grows the bundle by 2.0 KB (+0.0%)

Metric main This PR Δ
Total dist 4126.1 KB 4128.2 KB +2.0 KB (+0.0%)
JS 1596.1 KB 1596.7 KB +0.7 KB (+0.0%)
Sourcemaps 2444.8 KB 2446.2 KB +1.4 KB (+0.1%)
Type defs 85.2 KB 85.2 KB
Other 0.0 KB 0.0 KB

dist/ only; native binaries are versioned separately and not counted here.

@pkg-pr-new

pkg-pr-new Bot commented Aug 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

varlock

npm i https://pkg.pr.new/varlock@1050

@varlock/native-helper-darwin

npm i https://pkg.pr.new/@varlock/native-helper-darwin@1050

@varlock/native-helper-linux-arm64

npm i https://pkg.pr.new/@varlock/native-helper-linux-arm64@1050

@varlock/native-helper-linux-x64

npm i https://pkg.pr.new/@varlock/native-helper-linux-x64@1050

@varlock/native-helper-win32-x64

npm i https://pkg.pr.new/@varlock/native-helper-win32-x64@1050

commit: ea4f793

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes in ea4f7938, covering the fallback-versus-imported-@currentEnv resolution path and its regression coverage.

  • Fallback provenance: _resolveCurrentEnv() now identifies whether an environment came from a real config item or the graph-level fallback.
  • Deferred fallback loading: fallback-specific files load only after imports have had an opportunity to establish a real @currentEnv, while direct and inherited @currentEnv values still load before import conditions.
  • Regression coverage: the new test verifies that an imported @currentEnv=dev selects .env.dev instead of a staging fallback.
  • Release note: the patch changeset describes the corrected imported-@currentEnv precedence.

Pullfrog  | View workflow run | Using azure/gpt-5.6-sol𝕏

@theoephraim
theoephraim merged commit bab4fcf into main Aug 30, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants