Skip to content

Commit

Permalink
fix: do not override process.env (#12227)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
florian-lefebvre and bluwy authored Oct 16, 2024
1 parent 7bcae4e commit 8b1a641
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-actors-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where environment variables would not be refreshed when using `astro:env`
1 change: 1 addition & 0 deletions packages/astro/src/env/runtime-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
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 './runtime-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;
Expand Down
13 changes: 2 additions & 11 deletions packages/astro/src/env/vite-plugin-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8b1a641

Please sign in to comment.