fix(config): interpolate env() refs before schema decode#5341
Merged
Conversation
`env(VAR)` in `supabase/config.toml` numeric/boolean fields crashed `loadProjectConfig` with `ProjectConfigParseError: Expected number` because the strict Effect Schema decode ran immediately after raw TOML parse — `interpolateValue` in `project.ts` exists but only fires post-decode via `resolveProjectValue`, so it never got the chance to substitute `"env(SUPABASE_ANALYTICS_PORT)"` on `analytics.port`. `loadProjectConfigFile` now loads the project environment and runs a schema-aware pre-decode walker that traverses the parsed document and `ProjectConfigSchema.ast` in parallel: - Substitutes `env(VAR)` string leaves against `.env` / `.env.local` / ambient env. - For unset vars, preserves the literal verbatim — matches Go's `apps/cli-go/pkg/config/decode_hooks.go:14-21` (LoadEnvHook). - After substitution, if the schema at that path expects Number or Boolean, coerces the substituted string to the primitive — mirroring Go's mapstructure chain where LoadEnvHook returns a string and subsequent hooks convert it to the target type. - Fields declared with the `env()` helper opt out via a marker annotation; they continue to flow through post-decode resolution. `interpolateLeafValue` in `project.ts` now returns the literal verbatim when the referenced env var is missing instead of throwing `MissingProjectEnvVarError` (also Go parity). The error class is removed; `resolveProjectValue` / `resolveProjectSubtree` no longer have a failure channel. No schema-file edits — coercion lives in the walker, so future schemas that add numeric/boolean fields automatically work with env() refs. Fixes CLI-1489
jgoux
approved these changes
May 22, 2026
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.
Fixes the crash when
supabase/config.tomlusesenv(VAR)on numeric or boolean fields (e.g.analytics.port = "env(SUPABASE_ANALYTICS_PORT)"). The strict Effect Schema decode ran immediately after raw TOML parse, withinterpolateValueinproject.tsonly firing post-decode viaresolveProjectValue— so it never got the chance to substitute the string beforeSchema.Numberrejected it.What changed
packages/config/src/io.ts—loadProjectConfigFilenow loads the project environment (.env/.env.local/ambient) and runs a schema-aware walker on the parsed document before handing it toSchema.decodeUnknownSync(ProjectConfigSchema).packages/config/src/lib/env.ts— traverses both the parsed document andProjectConfigSchema.astin parallel. For string leaves matchingenv(VAR): substitutes against the env, then coerces to Number/Boolean if the schema at that path expects one. Mirrors Go'sLoadEnvHook+ mapstructure type chain (apps/cli-go/pkg/config/decode_hooks.go:14-21→ subsequent string→type conversion hooks).interpolateLeafValueinproject.tsno longer throwsMissingProjectEnvVarErrorwhen the referenced env var is unset. It returns the literalenv(VAR)string, matching Go parity. TheMissingProjectEnvVarErrorclass and re-export are removed;resolveProjectValue/resolveProjectSubtreeno longer have a failure channel.env()schema helper opt out via thex-env-deferredmarker annotation. They still require the literalenv(VAR)format for post-decode resolution byresolveProjectValue— the walker honors the marker and leaves those paths untouched.Reviewer-relevant context
db.ts,analytics.ts,auth/*.ts, etc.) automatically work withenv()references — no risk of a contributor forgetting to use a coerced primitive at declaration time.env() = Schema.String.check(isPattern(envRegex)).annotate({...})attaches metadata to the resulting Filter rather than the base String AST, soisDeferredEnvFieldinspects bothnode.annotationsandnode.checks[].annotations. Caught by the@supabase/stackfunctions.unit.test.tsregression forfunctions.<name>.env(env() helper at a record value position).project.tsare now non-failing. Two existing "fails when missing env var" tests inproject.unit.test.ts:223,255are rewritten to assert verbatim preservation.redactValuealready skips redaction when the value is still an env reference (!isEnvReference(value)), so unresolved literals flow through as plain strings — no Redacted wrapping on missing secrets.loadProjectConfigcall entirely to avoid the crash; reintroducing it is a follow-up refactor since the workaround still functions correctly (it only loads env, not config).apps/cli/src/next/config/project-context.layer.unit.test.ts:30is not modified — it asserts the workaround-era behavior ofprojectContextLayer, which is unchanged. Direct upstream regression coverage is added inpackages/config/src/io.unit.test.ts: numeric coercion, boolean coercion, verbatim preservation, ambient fallback, and decode failure when an unset var is referenced from a numeric field.Fixes CLI-1489