0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00

fix: astro:env sync error in content config (#11771)

This commit is contained in:
Florian Lefebvre 2024-08-19 16:01:31 +02:00 committed by GitHub
parent d12dcbff60
commit 49650a4555
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 62 additions and 4 deletions

View file

@ -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

View file

@ -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 ?? {};
@ -57,6 +57,7 @@ export function astroEnv({
schema,
loadedEnv,
validateSecrets: settings.config.experimental.env?.validateSecrets ?? false,
sync,
});
templates = {
@ -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> = [];
@ -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)),

View file

@ -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', () => {

View file

@ -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" }),
}
}
}
});

View file

@ -0,0 +1,8 @@
{
"name": "@test/astro-env-content-collections",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -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()
})
})
}

View file

@ -0,0 +1,3 @@
{
"extends": "astro/tsconfigs/base"
}

8
pnpm-lock.yaml generated
View file

@ -2163,6 +2163,12 @@ importers:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/astro-env-content-collections:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/astro-env-required-public:
dependencies:
astro:
@ -9457,12 +9463,10 @@ packages:
libsql@0.3.19:
resolution: {integrity: sha512-Aj5cQ5uk/6fHdmeW0TiXK42FqUlwx7ytmMLPSaUQPin5HKKKuUPD62MAbN4OEweGBBI7q1BekoEN4gPUEL6MZA==}
cpu: [x64, arm64, wasm32]
os: [darwin, linux, win32]
libsql@0.4.1:
resolution: {integrity: sha512-qZlR9Yu1zMBeLChzkE/cKfoKV3Esp9cn9Vx5Zirn4AVhDWPcjYhKwbtJcMuHehgk3mH+fJr9qW+3vesBWbQpBg==}
cpu: [x64, arm64, wasm32]
os: [darwin, linux, win32]
lilconfig@2.1.0: