diff --git a/.changeset/fix-db-env-var-loading.md b/.changeset/fix-db-env-var-loading.md new file mode 100644 index 000000000000..b5c2c51ed112 --- /dev/null +++ b/.changeset/fix-db-env-var-loading.md @@ -0,0 +1,5 @@ +--- +'@astrojs/db': patch +--- + +Fixed `ASTRO_DATABASE_FILE` environment variable not being read from `.env` files during build. The `databaseFileEnvDefined()` function now uses `getAstroEnv()` which correctly loads `ASTRO_`-prefixed env vars, instead of Vite's `loadEnv()` which defaults to only loading `VITE_`-prefixed variables. diff --git a/packages/db/src/core/integration/index.ts b/packages/db/src/core/integration/index.ts index 3e4cca65d990..c0a748f95547 100644 --- a/packages/db/src/core/integration/index.ts +++ b/packages/db/src/core/integration/index.ts @@ -7,7 +7,6 @@ import colors from 'piccolore'; import { createServer, type HotPayload, - loadEnv, mergeConfig, type RunnableDevEnvironment, type UserConfig, @@ -20,7 +19,7 @@ import { CONFIG_FILE_NAMES, DB_PATH, VIRTUAL_MODULE_ID } from '../consts.js'; import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from '../errors.js'; import { resolveDbConfig } from '../load-file.js'; import { SEED_DEV_FILE_NAME } from '../queries.js'; -import { getDbDirectoryUrl, getRemoteDatabaseInfo, type VitePlugin } from '../utils.js'; +import { getAstroEnv, getDbDirectoryUrl, getRemoteDatabaseInfo, type VitePlugin } from '../utils.js'; import { fileURLIntegration } from './file-url.js'; import { getDtsContent } from './typegen.js'; import { @@ -209,7 +208,7 @@ function astroDBIntegration(options?: AstroDBConfig): AstroIntegration { } function databaseFileEnvDefined() { - const env = loadEnv('', process.cwd()); + const env = getAstroEnv(); return env.ASTRO_DATABASE_FILE != null || process.env.ASTRO_DATABASE_FILE != null; }