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:
parent
d12dcbff60
commit
49650a4555
8 changed files with 62 additions and 4 deletions
5
.changeset/tiny-lamps-lick.md
Normal file
5
.changeset/tiny-lamps-lick.md
Normal 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
|
7
packages/astro/src/env/vite-plugin-env.ts
vendored
7
packages/astro/src/env/vite-plugin-env.ts
vendored
|
@ -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)),
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
12
packages/astro/test/fixtures/astro-env-content-collections/astro.config.mjs
vendored
Normal file
12
packages/astro/test/fixtures/astro-env-content-collections/astro.config.mjs
vendored
Normal 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" }),
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
8
packages/astro/test/fixtures/astro-env-content-collections/package.json
vendored
Normal file
8
packages/astro/test/fixtures/astro-env-content-collections/package.json
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@test/astro-env-content-collections",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
13
packages/astro/test/fixtures/astro-env-content-collections/src/content/config.ts
vendored
Normal file
13
packages/astro/test/fixtures/astro-env-content-collections/src/content/config.ts
vendored
Normal 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()
|
||||
})
|
||||
})
|
||||
}
|
3
packages/astro/test/fixtures/astro-env-content-collections/tsconfig.json
vendored
Normal file
3
packages/astro/test/fixtures/astro-env-content-collections/tsconfig.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"extends": "astro/tsconfigs/base"
|
||||
}
|
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
|
@ -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:
|
||||
|
|
Loading…
Add table
Reference in a new issue