Skip to content

fix: load .env files before TOML config interpolation#358

Open
kevinke wants to merge 1 commit into
bytebase:mainfrom
kevinke:fix/toml-env-loading
Open

fix: load .env files before TOML config interpolation#358
kevinke wants to merge 1 commit into
bytebase:mainfrom
kevinke:fix/toml-env-loading

Conversation

@kevinke

@kevinke kevinke commented Jul 12, 2026

Copy link
Copy Markdown

Problem

When using --config with dbhub.toml, .env files are never loaded. This is because loadEnvFiles() (which calls dotenv.config()) is only called inside resolveDSN() — the fallback path that runs ONLY when no TOML config is found.

As a result, ${VAR_NAME} interpolation in dbhub.toml (handled by interpolateEnvVars() in toml-loader.ts) can only resolve from system environment variables, not from .env files.

This contradicts the official documentation which states that .env files are supported.

Root Cause

In resolveSourceConfigs() (src/config/env.ts):

  1. If a TOML config exists → loadTomlConfig() runs interpolateEnvVars() reading from process.env → returns immediately
  2. loadEnvFiles() / dotenv.config() lives inside resolveDSN() — which is never reached when TOML config exists

Fix

Call loadEnvFiles() at the start of resolveSourceConfigs(), before loadTomlConfig(). This ensures .env variables are loaded into process.env and available for TOML interpolation.

The redundant call inside resolveDSN() is harmless (dotenv just re-reads the same file).

Testing

  • Verified that with the fix, .env files are loaded when using --config dbhub.toml
  • ${VAR_NAME} interpolation in TOML now correctly resolves from .env files
  • No breaking changes to existing resolveDSN() fallback path

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.
Copilot AI review requested due to automatic review settings July 12, 2026 09:34
@kevinke kevinke requested a review from tianzhou as a code owner July 12, 2026 09:34

Copilot AI 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.

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 .env files 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 thread src/config/env.ts
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();
Comment thread src/config/env.ts
// 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();
Comment thread src/config/env.ts
// 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants