Skip to content

fix(varlock): allow @currentEnv to reference an imported key - #1066

Merged
theoephraim merged 4 commits into
mainfrom
imported-current-env
Sep 3, 2026
Merged

fix(varlock): allow @currentEnv to reference an imported key#1066
theoephraim merged 4 commits into
mainfrom
imported-current-env

Conversation

@theoephraim

@theoephraim theoephraim commented Sep 3, 2026

Copy link
Copy Markdown
Member

Fixes #428.

Split 3 of 3 out of #1061 by @WalksWithASwagger, who wrote the original fix. The follow-up commit reshapes it; commit authorship is preserved on the first commit.

@currentEnv=$DEPLOY_ENV errored whenever the flag arrived through @import, because the flag was validated during finishInit, before imports were merged. That breaks the monorepo shape in #428, where a sub-package imports a shared DEPLOY_ENV.

How it works

When @currentEnv points at a key that is not defined in the file but the file has @imports, the check is deferred: the flag key is recorded, imports run, and at the end of that file's import processing we verify one of its own imports actually declared the key (scoped to that file's import subtree, so a declaration from another load path or an ancestor schema does not count). If none did, it errors and points at pick=[...]. The directory then loads .env.<env> files once the flag is known, the same path an imported @currentEnv already used after #1050.

Along with it:

  • an auto-loaded .env value on its own no longer satisfies the flag or triggers .env.<env> loading
  • a directory imported before the import that provides the flag is rejected with an error asking to reorder (it would otherwise load without its .env.<env> files, or load them after its own import conditions had run)
  • forEnv() in an import declared before the flag import gets a clear message instead of a TypeError
  • a @currentEnv declared in an imported file now also carries through a partial import when the flag key passes the pick/omit filter, matching the existing full-import rule; an importer's own @currentEnv still wins

Known limit (documented)

When the flag arrives via import, that file's .env.<env> files load after its imports finish, so @import(enabled=...) and @disable conditions in that file cannot depend on values set only in those files. The import and monorepo guides explain the ordering and the workaround.

Test plan

  • cd packages/varlock && bunx vitest run (1962 passed)
  • bun run --filter @varlock/website build
  • 18 new cases in environments.test.ts: pick, pick globs, positional keys, omitted flag, statically and dynamically disabled imports, flag import before/after a directory import, nested directory boundary, partial-import propagation of an imported @currentEnv (including vs --env), and the pending check staying scoped to the file's own imports (multi-path, ancestor schema, diamond alias, disabled alias original)

Peek unprocessed @import decorators during finishInit so an env flag
that arrives via import does not error out before imports are merged.
A values-only .env key still cannot act as the flag, imported
directories load .env.<env> when the flag import is declared after
them, and ancestor env propagation stops at a nested directory that
set its own @currentEnv.

Fixes #428
@github-actions

github-actions Bot commented Sep 3, 2026

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.18.0 → 1.18.1
  • @varlock/native-helper-linux-arm64 1.18.0 → 1.18.1
  • @varlock/native-helper-linux-x64 1.18.0 → 1.18.1
  • @varlock/native-helper-win32-x64 1.18.0 → 1.18.1
  • varlock 1.18.0 → 1.18.1

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

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

📦 Bundle size

⚠️ grows the bundle by 11.8 KB (+0.3%)

Metric main This PR Δ
Total dist 4452.0 KB 4463.7 KB +11.8 KB (+0.3%)
JS 1691.6 KB 1695.3 KB +3.7 KB (+0.2%)
Sourcemaps 2652.2 KB 2659.1 KB +6.9 KB (+0.3%)
Type defs 108.2 KB 109.3 KB +1.2 KB (+1.1%)
Other 0.0 KB 0.0 KB

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

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
varlock-website 57f9713 Commit Preview URL

Branch Preview URL
Sep 03 2026, 07:17 AM

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

Important

Late environment backfill can leave imported directories initialized against the wrong values and produce loading errors.

Reviewed changes Checked the imported @currentEnv validation path, import-filter prediction, late environment-file propagation, documentation, and regression coverage.

  • Imported flag recognition Defers @currentEnv processing when a static import filter can provide the referenced key, then validates that the key actually arrived.
  • Environment source selection Prevents values found only in auto-loaded .env files from independently satisfying the environment flag declaration.
  • Imported directory propagation Backfills .env.<env> files into directories imported before the flag-providing import, while respecting nested @currentEnv boundaries.
  • Coverage and docs Adds eight environment-loading cases and updates the import and root-decorator documentation.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using azure/gpt-5.6-sol𝕏

Comment thread packages/varlock/src/env-graph/lib/data-source.ts Outdated
…king the AST

Replace importStaticallyProvidesKey with a pending marker that is verified at the end
of the file's own _processImports, so there is no parallel copy of the pick/omit/positional
filter logic. Drop the post-hoc directory backfill: a directory imported before the flag
is known now errors and asks for a reorder, since backfilling loaded its env files after
its import conditions had already run. Give forEnv() before the flag import a clear message.

Also let an imported @currentEnv carry through a partial import when the flag key passes
the import filter, matching the existing full-import rule, and document the ordering
limits in the import and monorepo guides.
@pkg-pr-new

pkg-pr-new Bot commented Sep 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

@env-spec/parser

npm i https://pkg.pr.new/dmno-dev/varlock/@env-spec/parser@1066

varlock

npm i https://pkg.pr.new/dmno-dev/varlock@1066

@varlock/native-helper-darwin

npm i https://pkg.pr.new/dmno-dev/varlock/@varlock/native-helper-darwin@1066

@varlock/native-helper-linux-arm64

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

@varlock/native-helper-linux-x64

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

@varlock/native-helper-win32-x64

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

@varlock/aws-sigv4-plugin

npm i https://pkg.pr.new/dmno-dev/varlock/@varlock/aws-sigv4-plugin@1066

commit: f9554db

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

Important

Deferred flag validation is currently graph-wide, so an unrelated declaration can incorrectly satisfy @currentEnv.

Reviewed changes Reviewed the follow-up that replaced late directory backfill with import-order validation and deferred imported-flag checks.

  • Deferred validation Checked the new post-import verification and clearer failure paths for missing flags and unresolved forEnv() imports.
  • Directory ordering Verified that directories imported before the flag provider are now rejected instead of receiving environment files after initialization.
  • Partial propagation Reviewed propagation of an imported @currentEnv through partial imports when the flag key remains visible.
  • Coverage and docs Checked the added ordering and propagation cases plus the documented initialization constraints.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using azure/gpt-5.6-sol𝕏

Comment thread packages/varlock/src/env-graph/lib/data-source.ts Outdated
A declaration that reached the graph through another load path or an ancestor schema
no longer satisfies a pending @currentEnv, so an omitted pick entry is still reported.
Diamond aliases expand to their original so a re-imported shared file still counts.

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

Important

Alias traversal can still accept a disabled declaration as the imported environment flag.

Reviewed changes Reviewed the source-scoped deferred flag validation added since the prior Pullfrog review.

  • Scoped import validation Replaced the graph-wide declaration check with traversal of the declaring source's visible import subtree.
  • Regression coverage Added multi-path, ancestor-schema, and diamond-alias cases for deferred @currentEnv validation.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using azure/gpt-5.6-sol𝕏

Comment thread packages/varlock/src/env-graph/lib/data-source.ts
…ng env flag

The alias node can be enabled while the original source was disabled by its own
@disable, in which case its definitions are excluded from ConfigItem.defs and must
not satisfy a pending @currentEnv either.

@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 Reviewed the disabled-original handling added since the prior Pullfrog review.

  • Alias disabled-state validation Checked the original source's effective disabled state after expanding an ImportAliasSource, preventing a disabled declaration from satisfying a pending environment flag.
  • Regression coverage Added a diamond-import case where an enabled alias points to a source disabled by its own @disable.

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

@theoephraim
theoephraim merged commit e5dab5f into main Sep 3, 2026
27 checks passed
theoephraim added a commit that referenced this pull request Sep 4, 2026
Follow-up to #1066: the environments guide still said the flag had to live
in the same file.

Co-authored-by: Kris Krüg <140290088+WalksWithASwagger@users.noreply.github.com>
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.

[BUG]: environment flag "..." must be defined within this schema when importing

2 participants