Skip to content

Commit

Permalink
fix: astro:env sync error in content config (#11771)
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre authored Aug 19, 2024
1 parent d12dcbf commit 49650a4
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-lamps-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an error thrown by `astro sync` when an `astro:env` virtual module is imported inside the Content Collections config
7 changes: 5 additions & 2 deletions packages/astro/src/env/vite-plugin-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function astroEnv({
fs,
sync,
}: AstroEnvVirtualModPluginParams): Plugin | undefined {
if (!settings.config.experimental.env || sync) {
if (!settings.config.experimental.env) {
return;
}
const schema = settings.config.experimental.env.schema ?? {};
Expand All @@ -57,6 +57,7 @@ export function astroEnv({
schema,
loadedEnv,
validateSecrets: settings.config.experimental.env?.validateSecrets ?? false,
sync,
});

templates = {
Expand Down Expand Up @@ -100,10 +101,12 @@ function validatePublicVariables({
schema,
loadedEnv,
validateSecrets,
sync,
}: {
schema: EnvSchema;
loadedEnv: Record<string, string>;
validateSecrets: boolean;
sync: boolean;
}) {
const valid: Array<{ key: string; value: any; type: string; context: 'server' | 'client' }> = [];
const invalid: Array<InvalidVariable> = [];
Expand All @@ -125,7 +128,7 @@ function validatePublicVariables({
}
}

if (invalid.length > 0) {
if (invalid.length > 0 && !sync) {
throw new AstroError({
...AstroErrorData.EnvInvalidVariables,
message: AstroErrorData.EnvInvalidVariables.message(invalidVariablesToError(invalid)),
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/astro-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ describe('astro sync', () => {
assert.fail();
}
});
it('Does not throw if a virtual module is imported in content/config.ts', async () => {
try {
await fixture.load('./fixtures/astro-env-content-collections/');
fixture.clean();
await fixture.whenSyncing();
assert.ok(true);
} catch {
assert.fail();
}
});
});

describe('astro:actions', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig, envField } from 'astro/config';

// https://astro.build/config
export default defineConfig({
experimental: {
env: {
schema: {
FOO: envField.string({ context: "client", access: "public", optional: true, default: "ABC" }),
}
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-env-content-collections",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineCollection, z } from "astro:content";
import { FOO } from "astro:env/client"

console.log({ FOO })

export const collections = {
foo: defineCollection({
type: "data",
schema: z.object({
title: z.string()
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "astro/tsconfigs/base"
}
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49650a4

Please sign in to comment.