0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix: do not override process.env (#12227)

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
This commit is contained in:
Florian Lefebvre 2024-10-16 15:51:03 +02:00 committed by GitHub
parent 7bcae4e3ee
commit 8b1a641be9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 12 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a case where environment variables would not be refreshed when using `astro:env`

View file

@ -0,0 +1 @@
export const ENV_SYMBOL = Symbol.for('astro:env/dev');

View file

@ -1,4 +1,5 @@
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { ENV_SYMBOL } from './runtime-constants.js';
import { invalidVariablesToError } from './errors.js';
import type { ValidationResultInvalid } from './validators.js';
export { validateEnvVariable, getEnvFieldType } from './validators.js';
@ -6,7 +7,10 @@ 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;

View file

@ -11,12 +11,7 @@ import {
import { type InvalidVariable, invalidVariablesToError } from './errors.js';
import type { EnvSchema } from './schema.js';
import { getEnvFieldType, validateEnvVariable } from './validators.js';
// TODO: reminders for when astro:env comes out of experimental
// Types should always be generated (like in types/content.d.ts). That means the client module will be empty
// and server will only contain getSecret for unknown variables. Then, specifying a schema should only add
// variables as needed. For secret variables, it will only require specifying SecretValues and it should get
// merged with the static types/content.d.ts
import { ENV_SYMBOL } from './runtime-constants.js';
interface AstroEnvVirtualModPluginParams {
settings: AstroSettings;
@ -47,11 +42,7 @@ export function astroEnv({
fileURLToPath(settings.config.root),
'',
);
for (const [key, value] of Object.entries(loadedEnv)) {
if (value !== undefined) {
process.env[key] = value;
}
}
(globalThis as any)[ENV_SYMBOL] = loadedEnv;
const validatedVariables = validatePublicVariables({
schema,