Skip to content

Commit

Permalink
fix(astro): getSecret in dev and build (#11198)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Valladares <[email protected]>
  • Loading branch information
florian-lefebvre and dreyfus92 authored Jun 6, 2024
1 parent 48d5309 commit 8b9a499
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-onions-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where `astro:env` `getSecret` would not retrieve environment variables properly in dev and build modes
6 changes: 6 additions & 0 deletions packages/astro/src/env/vite-plugin-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export function astroEnv({
fileURLToPath(settings.config.root),
''
);
for (const [key, value] of Object.entries(loadedEnv)) {
if (value !== undefined) {
process.env[key] = value;
}
}

const validatedVariables = validatePublicVariables({ schema, loadedEnv });

const clientTemplates = getClientTemplates({ validatedVariables });
Expand Down
40 changes: 35 additions & 5 deletions packages/astro/test/env-secret.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { after, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
import { writeFileSync } from 'node:fs';

describe('astro:env public variables', () => {
/** @type {Awaited<ReturnType<typeof loadFixture>>} */
let fixture;
/** @type {Awaited<ReturnType<(typeof fixture)["loadTestAdapterApp"]>>} */
let app;
/** @type {Awaited<ReturnType<(typeof fixture)["startDevServer"]>>} */
let devServer = undefined;

describe('Server variables', () => {
before(async () => {
after(async () => {
await devServer?.stop();
});

it('works in dev', async () => {
writeFileSync(
new URL('./fixtures/astro-env-server-secret/.env', import.meta.url),
'KNOWN_SECRET=5',
'utf-8'
);
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
});
devServer = await fixture.startDevServer();
const response = await fixture.fetch('/');
assert.equal(response.status, 200);
});

it('builds without throwing', async () => {
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
output: 'server',
Expand All @@ -24,13 +45,22 @@ describe('astro:env public variables', () => {
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
});

it('builds without throwing', async () => {
assert.equal(true, true);
});

it('adapter can set how env is retrieved', async () => {
fixture = await loadFixture({
root: './fixtures/astro-env-server-secret/',
output: 'server',
adapter: testAdapter({
env: {
KNOWN_SECRET: '123456',
UNKNOWN_SECRET: 'abc',
},
}),
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
assert.equal(response.status, 200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
experimental: {
env: {
schema: {
KNOWN_SECRET: envField.number({ context: "server", access: "secret", optional: true })
KNOWN_SECRET: envField.number({ context: "server", access: "secret" })
}
}
}
Expand Down

0 comments on commit 8b9a499

Please sign in to comment.