-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve config error handling #7852
Changes from all commits
b245002
efb2206
8059c8e
07277ee
5b931f3
6992d4b
1bda5c5
f2033a2
4060c07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Arguments as Flags } from 'yargs-parser'; | ||
import type { AstroInlineConfig } from '../@types/astro.js'; | ||
|
||
export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig { | ||
return { | ||
configFile: typeof flags.config === 'string' ? flags.config : undefined, | ||
root: typeof flags.root === 'string' ? flags.root : undefined, | ||
site: typeof flags.site === 'string' ? flags.site : undefined, | ||
base: typeof flags.base === 'string' ? flags.base : undefined, | ||
markdown: { | ||
drafts: typeof flags.drafts === 'boolean' ? flags.drafts : undefined, | ||
}, | ||
server: { | ||
port: typeof flags.port === 'number' ? flags.port : undefined, | ||
host: | ||
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined, | ||
open: typeof flags.open === 'boolean' ? flags.open : undefined, | ||
}, | ||
experimental: { | ||
assets: typeof flags.experimentalAssets === 'boolean' ? flags.experimentalAssets : undefined, | ||
}, | ||
}; | ||
Comment on lines
+19
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't; I believe we forgot about it. We should probably add it. |
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,11 @@ import * as colors from 'kleur/colors'; | |
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { ZodError } from 'zod'; | ||
import { eventConfigError, telemetry } from '../../events/index.js'; | ||
import { trackAstroConfigZodError } from '../errors/errors.js'; | ||
import { AstroError, AstroErrorData } from '../errors/index.js'; | ||
import { formatConfigErrorMessage } from '../messages.js'; | ||
import { mergeConfig } from './merge.js'; | ||
import { createRelativeSchema } from './schema.js'; | ||
import { loadConfigWithVite } from './vite-load.js'; | ||
|
@@ -80,7 +84,21 @@ export async function validateConfig( | |
const AstroConfigRelativeSchema = createRelativeSchema(cmd, root); | ||
|
||
// First-Pass Validation | ||
const result = await AstroConfigRelativeSchema.parseAsync(userConfig); | ||
let result: AstroConfig; | ||
try { | ||
result = await AstroConfigRelativeSchema.parseAsync(userConfig); | ||
} catch (e) { | ||
// Improve config zod error messages | ||
if (e instanceof ZodError) { | ||
Comment on lines
+88
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Errors are now logged more directly. |
||
// Mark this error so the callee can decide to suppress Zod's error if needed. | ||
// We still want to throw the error to signal an error in validation. | ||
trackAstroConfigZodError(e); | ||
// eslint-disable-next-line no-console | ||
console.error(formatConfigErrorMessage(e) + '\n'); | ||
telemetry.record(eventConfigError({ cmd, err: e, isFatal: true })); | ||
} | ||
throw e; | ||
} | ||
|
||
// If successful, return the result as a verified AstroConfig object. | ||
return result; | ||
|
@@ -172,11 +190,19 @@ async function loadConfig( | |
if (!configPath) return {}; | ||
|
||
// Create a vite server to load the config | ||
return await loadConfigWithVite({ | ||
root, | ||
configPath, | ||
fs: fsMod, | ||
}); | ||
try { | ||
return await loadConfigWithVite({ | ||
root, | ||
configPath, | ||
fs: fsMod, | ||
}); | ||
} catch (e) { | ||
const configPathText = configFile ? colors.bold(configFile) : 'your Astro config'; | ||
// Config errors should bypass log level as it breaks startup | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About the comment: Is it because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually there's no |
||
// eslint-disable-next-line no-console | ||
console.error(`${colors.bold(colors.red('[astro]'))} Unable to load ${configPathText}\n`); | ||
throw e; | ||
} | ||
} | ||
|
||
interface ResolveConfigResult { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import { createContainerWithAutomaticRestart } from './restart.js'; | |
|
||
export interface DevOptions { | ||
logging: LogOptions; | ||
handleConfigError: (error: Error) => void; | ||
} | ||
|
||
export interface DevServer { | ||
|
@@ -36,9 +35,6 @@ export default async function dev( | |
inlineConfig, | ||
logging: options.logging, | ||
fs, | ||
handleConfigError: options.handleConfigError, | ||
// eslint-disable-next-line no-console | ||
beforeRestart: () => console.clear(), | ||
Comment on lines
-40
to
-41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Personally I find clearing the console moves and cuts out the terminal a lot that it's better to not do it instead, so I removed it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally agree, especially if there are scripts/users that rely on stdout/stderr |
||
}); | ||
|
||
// Start listening to the port | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is a rename from
load-settings.ts
but Git doesn't see it that way.