fix: load .env files before TOML config interpolation#358
Open
kevinke wants to merge 1 commit into
Open
Conversation
When using --config with dbhub.toml, .env files were never loaded
because loadEnvFiles() was only called inside resolveDSN() — the
fallback path that runs when no TOML config is found.
This meant ${VAR_NAME} interpolation in dbhub.toml could only
resolve from system environment variables, not from .env files,
contradicting the documentation.
The fix calls loadEnvFiles() at the start of resolveSourceConfigs()
so that .env variables are available for TOML interpolation regardless
of which config path is taken.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes TOML config environment-variable interpolation by ensuring .env files are loaded before dbhub.toml is parsed/interpolated, aligning runtime behavior with the TOML documentation.
Changes:
- Load
.envfiles earlier in the config-resolution flow so${VAR_NAME}placeholders in TOML can resolve from.env. - Add explanatory comments clarifying the TOML-vs-DSN fallback interaction.
Comment on lines
+663
to
+667
| // 0. Load .env files first so that environment variables are available | ||
| // for TOML interpolation (${VAR_NAME}) when using --config. | ||
| // Without this, .env is only loaded inside resolveDSN() (the fallback | ||
| // path), meaning TOML configs never get .env-backed variable resolution. | ||
| loadEnvFiles(); |
| // for TOML interpolation (${VAR_NAME}) when using --config. | ||
| // Without this, .env is only loaded inside resolveDSN() (the fallback | ||
| // path), meaning TOML configs never get .env-backed variable resolution. | ||
| loadEnvFiles(); |
| // for TOML interpolation (${VAR_NAME}) when using --config. | ||
| // Without this, .env is only loaded inside resolveDSN() (the fallback | ||
| // path), meaning TOML configs never get .env-backed variable resolution. | ||
| loadEnvFiles(); |
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.
Problem
When using
--configwithdbhub.toml,.envfiles are never loaded. This is becauseloadEnvFiles()(which callsdotenv.config()) is only called insideresolveDSN()— the fallback path that runs ONLY when no TOML config is found.As a result,
${VAR_NAME}interpolation indbhub.toml(handled byinterpolateEnvVars()intoml-loader.ts) can only resolve from system environment variables, not from.envfiles.This contradicts the official documentation which states that
.envfiles are supported.Root Cause
In
resolveSourceConfigs()(src/config/env.ts):loadTomlConfig()runsinterpolateEnvVars()reading fromprocess.env→ returns immediatelyloadEnvFiles()/dotenv.config()lives insideresolveDSN()— which is never reached when TOML config existsFix
Call
loadEnvFiles()at the start ofresolveSourceConfigs(), beforeloadTomlConfig(). This ensures.envvariables are loaded intoprocess.envand available for TOML interpolation.The redundant call inside
resolveDSN()is harmless (dotenv just re-reads the same file).Testing
.envfiles are loaded when using--config dbhub.toml${VAR_NAME}interpolation in TOML now correctly resolves from.envfilesresolveDSN()fallback path