Skip to content

Commit ff257df

Browse files
florian-lefebvreematipicodelucissarah11918
authored
feat: move astro config validation after astro:config:setup (#13482)
* feat: move astro config validation after astro:config:setup * feat: superRefine * fix: test * feat: update test * feat: update test * fix: test * feat: feedback * feat: improve logging * feat: refactor * fix: no spread * feat: split schemas * chore: changeset * Update packages/astro/src/core/config/schemas/README.md Co-authored-by: Emanuele Stoppa <[email protected]> * Update packages/astro/src/integrations/hooks.ts * Update packages/astro/src/core/config/schemas/README.md * Update .changeset/easy-vans-laugh.md * Update .changeset/easy-vans-laugh.md * grammar nit in changeset --------- Co-authored-by: Emanuele Stoppa <[email protected]> Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: ematipico <[email protected]> Co-authored-by: delucis <[email protected]> Co-authored-by: sarah11918 <[email protected]>
1 parent 4db2c68 commit ff257df

File tree

16 files changed

+864
-848
lines changed

16 files changed

+864
-848
lines changed

.changeset/easy-vans-laugh.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Updates Astro config validation to also run for the Integration API. An error log will specify which integration is failing the validation.
6+
7+
Now, Astro will first validate the user configuration, then validate the updated configuration after each integration `astro:config:setup` hook has run. This means `updateConfig()` calls will no longer accept invalid configuration.
8+
9+
This fixes a situation where integrations could potentially update a project with a malformed configuration. These issues should now be caught and logged so that you can update your integration to only set valid configurations.

.changeset/three-masks-see.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Improves integrations error handling
6+
7+
If an error is thrown from an integration hook, an error log will now provide information about the concerned integration and hook

packages/astro/src/container/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './polyfill.js';
22
import { posix } from 'node:path';
33
import { getDefaultClientDirectives } from '../core/client-directive/index.js';
4-
import { ASTRO_CONFIG_DEFAULTS } from '../core/config/schema.js';
4+
import { ASTRO_CONFIG_DEFAULTS } from '../core/config/schemas/index.js';
55
import { validateConfig } from '../core/config/validate.js';
66
import { createKey } from '../core/encryption.js';
77
import { Logger } from '../core/logger/core.js';

packages/astro/src/core/build/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class AstroBuilder {
173173

174174
/** Run the build logic. build() is marked private because usage should go through ".run()" */
175175
private async build({ viteConfig }: { viteConfig: vite.InlineConfig }) {
176-
await runHookBuildStart({ config: this.settings.config, logging: this.logger });
176+
await runHookBuildStart({ config: this.settings.config, logger: this.logger });
177177
this.validateConfig();
178178

179179
this.logger.info('build', `output: ${blue('"' + this.settings.config.output + '"')}`);
@@ -248,7 +248,7 @@ class AstroBuilder {
248248
.flat()
249249
.map((pageData) => pageData.route)
250250
.concat(hasServerIslands ? getServerIslandRouteData(this.settings.config) : []),
251-
logging: this.logger,
251+
logger: this.logger,
252252
});
253253

254254
if (this.logger.level && levels[this.logger.level()] <= levels['info']) {

packages/astro/src/core/config/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export {
66
} from './config.js';
77
export { createNodeLogger } from './logging.js';
88
export { mergeConfig } from './merge.js';
9-
export type { AstroConfigType } from './schema.js';
9+
export type { AstroConfigType } from './schemas/index.js';
1010
export { createSettings } from './settings.js';
1111
export { loadTSConfig, updateTSConfigForFramework } from './tsconfig.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Config schemas
2+
3+
There are 3 zod schemas needed to validate the Astro config properly:
4+
5+
- `AstroConfigSchema` (base): a schema that matches the `AstroConfig` type
6+
- `createRelativeSchema` (relative): a function that uses the base schema, and adds transformations relative to the project root. Transformations occur at runtime, and the paths are resolved against the `cwd` of the CLI.
7+
- `AstroConfigRefinedSchema` (refined): a schema that handles extra validations. Due to constraints imposed by the Astro architecture, refinements can't be done on the `AstroConfig` schema because integrations deal with the output of the `AstroConfig` schema. As a result, this schema runs after parsing the user config and after every integration `astro:config:setup` hook (to make sure `updateConfig() has been called with valid config)`.

0 commit comments

Comments
 (0)