Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(next): do not override process.env #12125

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .changeset/proud-adults-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'astro': major
---

Fixes a case where environment variables would not be refreshed.

This fix introduces some changes that are breaking for adapters adding environment variables to `process.env` in order to reach `astro:env`:

```diff
+ import { ENV_SYMBOL } from "astro/env/setup"

function setProcessEnv(config: AstroConfig, env: Record<string, unknown>) {
const getEnv = createGetEnv(env);

+ (globalThis as any)[ENV_SYMBOL] ??= {};
if (config.env.schema) {
for (const key of Object.keys(config.env.schema)) {
const value = getEnv(key);
- if (value !== undefined) {
- process.env[key] = value;
}
+ (globalThis as any)[ENV_SYMBOL] = value;
}
}
}
```
2 changes: 2 additions & 0 deletions packages/astro/src/env/constants.ts
Original file line number Diff line number Diff line change
@@ -9,3 +9,5 @@ export const ENV_TYPES_FILE = 'env.d.ts';

const PKG_BASE = new URL('../../', import.meta.url);
export const MODULE_TEMPLATE_URL = new URL('templates/env.mjs', PKG_BASE);

export const ENV_SYMBOL = Symbol.for('astro:env/dev');
6 changes: 5 additions & 1 deletion packages/astro/src/env/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { ENV_SYMBOL } from './constants.js';
import { invalidVariablesToError } from './errors.js';
import type { ValidationResultInvalid } from './validators.js';
export { validateEnvVariable, getEnvFieldType } from './validators.js';

export type GetEnv = (key: string) => string | undefined;
type OnSetGetEnv = (reset: boolean) => void;

let _getEnv: GetEnv = (key) => process.env[key];
let _getEnv: GetEnv = (key) => {
const env = (globalThis as any)[ENV_SYMBOL] ?? {};
return env[key];
};

export function setGetEnv(fn: GetEnv, reset = false) {
_getEnv = fn;
1 change: 1 addition & 0 deletions packages/astro/src/env/setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { setGetEnv, type GetEnv } from './runtime.js';
export { ENV_SYMBOL } from './constants.js';
8 changes: 3 additions & 5 deletions packages/astro/src/env/vite-plugin-env.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import { type Plugin, loadEnv } from 'vite';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import type { AstroSettings } from '../types/astro.js';
import {
ENV_SYMBOL,
MODULE_TEMPLATE_URL,
VIRTUAL_MODULES_IDS,
VIRTUAL_MODULES_IDS_VALUES,
@@ -32,11 +33,8 @@ export function astroEnv({ settings, mode, sync }: AstroEnvPluginParams): Plugin
fileURLToPath(settings.config.root),
'',
);
for (const [key, value] of Object.entries(loadedEnv)) {
if (value !== undefined) {
process.env[key] = value;
}
}
(globalThis as any)[ENV_SYMBOL] ??= {};
Object.assign((globalThis as any)[ENV_SYMBOL], loadedEnv);

const validatedVariables = validatePublicVariables({
schema,
8 changes: 5 additions & 3 deletions packages/astro/test/env-secret.test.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { afterEach, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
import { ENV_SYMBOL } from '../dist/env/setup.js';

describe('astro:env secret variables', () => {
/** @type {Awaited<ReturnType<typeof loadFixture>>} */
@@ -12,13 +13,14 @@ describe('astro:env secret variables', () => {

afterEach(async () => {
await devServer?.stop();
if (process.env.KNOWN_SECRET) {
delete process.env.KNOWN_SECRET;
if (globalThis[ENV_SYMBOL]?.KNOWN_SECRET) {
delete globalThis[ENV_SYMBOL].KNOWN_SECRET;
}
});

it('works in dev', async () => {
process.env.KNOWN_SECRET = '5';
globalThis[ENV_SYMBOL] ??= {};
globalThis[ENV_SYMBOL].KNOWN_SECRET = '5';
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
});