mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
fix: show file name with invalid frontmatter errors for MDX (#12355)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
parent
4c9a42c2da
commit
c4726d7ba8
6 changed files with 50 additions and 63 deletions
5
.changeset/wise-bulldogs-jump.md
Normal file
5
.changeset/wise-bulldogs-jump.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improves error reporting for invalid frontmatter in MDX files during the `astro build` command. The error message now includes the file path where the frontmatter parsing failed.
|
|
@ -133,8 +133,8 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
}
|
}
|
||||||
case 'sync': {
|
case 'sync': {
|
||||||
const { sync } = await import('./sync/index.js');
|
const { sync } = await import('./sync/index.js');
|
||||||
const exitCode = await sync({ flags });
|
await sync({ flags });
|
||||||
return process.exit(exitCode);
|
return;
|
||||||
}
|
}
|
||||||
case 'preferences': {
|
case 'preferences': {
|
||||||
const { preferences } = await import('./preferences/index.js');
|
const { preferences } = await import('./preferences/index.js');
|
||||||
|
|
|
@ -22,10 +22,5 @@ export async function sync({ flags }: SyncOptions) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
await _sync(flagsToAstroInlineConfig(flags), { telemetry: true });
|
||||||
await _sync(flagsToAstroInlineConfig(flags), { telemetry: true });
|
|
||||||
return 0;
|
|
||||||
} catch (_) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1388,7 +1388,7 @@ export const GenerateContentTypesError = {
|
||||||
title: 'Failed to generate content types.',
|
title: 'Failed to generate content types.',
|
||||||
message: (errorMessage: string) =>
|
message: (errorMessage: string) =>
|
||||||
`\`astro sync\` command failed to generate content collection types: ${errorMessage}`,
|
`\`astro sync\` command failed to generate content collection types: ${errorMessage}`,
|
||||||
hint: 'Check your `src/content/config.*` file for typos.',
|
hint: 'This error is often caused by a syntax error inside your content, or your content configuration file. Check your `src/content/config.*` file for typos.',
|
||||||
} satisfies ErrorData;
|
} satisfies ErrorData;
|
||||||
/**
|
/**
|
||||||
* @docs
|
* @docs
|
||||||
|
|
|
@ -298,6 +298,11 @@ export function formatErrorMessage(err: ErrorWithMetadata, showFullStacktrace: b
|
||||||
output.push(` ${cyan(underline(docsLink))}`);
|
output.push(` ${cyan(underline(docsLink))}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (showFullStacktrace && err.loc) {
|
||||||
|
output.push(` ${bold('Location:')}`);
|
||||||
|
output.push(` ${underline(`${err.loc.file}:${err.loc.line ?? 0}:${err.loc.column ?? 0}`)}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (err.stack) {
|
if (err.stack) {
|
||||||
output.push(` ${bold('Stack trace:')}`);
|
output.push(` ${bold('Stack trace:')}`);
|
||||||
output.push(dim(formatErrorStackTrace(err, showFullStacktrace)));
|
output.push(dim(formatErrorStackTrace(err, showFullStacktrace)));
|
||||||
|
|
|
@ -23,6 +23,7 @@ import {
|
||||||
AstroError,
|
AstroError,
|
||||||
AstroErrorData,
|
AstroErrorData,
|
||||||
AstroUserError,
|
AstroUserError,
|
||||||
|
type ErrorWithMetadata,
|
||||||
createSafeError,
|
createSafeError,
|
||||||
isAstroError,
|
isAstroError,
|
||||||
} from '../errors/index.js';
|
} from '../errors/index.js';
|
||||||
|
@ -62,17 +63,7 @@ export default async function sync(
|
||||||
logger,
|
logger,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Run `astro:config:done`
|
await runHookConfigDone({ settings, logger });
|
||||||
// Actions will throw if there is misconfiguration, so catch here.
|
|
||||||
try {
|
|
||||||
await runHookConfigDone({ settings, logger });
|
|
||||||
} catch (err) {
|
|
||||||
if (err instanceof Error) {
|
|
||||||
const errorMessage = err.toString();
|
|
||||||
logger.error('sync', errorMessage);
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
return await syncInternal({ settings, logger, fs, force: inlineConfig.force });
|
return await syncInternal({ settings, logger, fs, force: inlineConfig.force });
|
||||||
}
|
}
|
||||||
|
@ -112,51 +103,41 @@ export async function syncInternal({
|
||||||
|
|
||||||
const timerStart = performance.now();
|
const timerStart = performance.now();
|
||||||
|
|
||||||
try {
|
if (!skip?.content) {
|
||||||
if (!skip?.content) {
|
await syncContentCollections(settings, { fs, logger });
|
||||||
await syncContentCollections(settings, { fs, logger });
|
settings.timer.start('Sync content layer');
|
||||||
settings.timer.start('Sync content layer');
|
let store: MutableDataStore | undefined;
|
||||||
let store: MutableDataStore | undefined;
|
try {
|
||||||
try {
|
const dataStoreFile = getDataStoreFile(settings);
|
||||||
const dataStoreFile = getDataStoreFile(settings);
|
if (existsSync(dataStoreFile)) {
|
||||||
if (existsSync(dataStoreFile)) {
|
store = await MutableDataStore.fromFile(dataStoreFile);
|
||||||
store = await MutableDataStore.fromFile(dataStoreFile);
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
logger.error('content', err.message);
|
|
||||||
}
|
}
|
||||||
if (!store) {
|
} catch (err: any) {
|
||||||
store = new MutableDataStore();
|
logger.error('content', err.message);
|
||||||
}
|
|
||||||
const contentLayer = globalContentLayer.init({
|
|
||||||
settings,
|
|
||||||
logger,
|
|
||||||
store,
|
|
||||||
});
|
|
||||||
await contentLayer.sync();
|
|
||||||
settings.timer.end('Sync content layer');
|
|
||||||
} else if (fs.existsSync(fileURLToPath(getContentPaths(settings.config, fs).contentDir))) {
|
|
||||||
// Content is synced after writeFiles. That means references are not created
|
|
||||||
// To work around it, we create a stub so the reference is created and content
|
|
||||||
// sync will override the empty file
|
|
||||||
settings.injectedTypes.push({
|
|
||||||
filename: CONTENT_TYPES_FILE,
|
|
||||||
content: '',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
syncAstroEnv(settings, fs);
|
if (!store) {
|
||||||
|
store = new MutableDataStore();
|
||||||
await writeFiles(settings, fs, logger);
|
}
|
||||||
logger.info('types', `Generated ${dim(getTimeStat(timerStart, performance.now()))}`);
|
const contentLayer = globalContentLayer.init({
|
||||||
} catch (err) {
|
settings,
|
||||||
const error = createSafeError(err);
|
logger,
|
||||||
logger.error(
|
store,
|
||||||
'types',
|
});
|
||||||
formatErrorMessage(collectErrorMetadata(error), logger.level() === 'debug') + '\n',
|
await contentLayer.sync();
|
||||||
);
|
settings.timer.end('Sync content layer');
|
||||||
// Will return exit code 1 in CLI
|
} else if (fs.existsSync(fileURLToPath(getContentPaths(settings.config, fs).contentDir))) {
|
||||||
throw error;
|
// Content is synced after writeFiles. That means references are not created
|
||||||
|
// To work around it, we create a stub so the reference is created and content
|
||||||
|
// sync will override the empty file
|
||||||
|
settings.injectedTypes.push({
|
||||||
|
filename: CONTENT_TYPES_FILE,
|
||||||
|
content: '',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
syncAstroEnv(settings, fs);
|
||||||
|
|
||||||
|
await writeFiles(settings, fs, logger);
|
||||||
|
logger.info('types', `Generated ${dim(getTimeStat(timerStart, performance.now()))}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,7 +204,7 @@ async function syncContentCollections(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const safeError = createSafeError(e);
|
const safeError = createSafeError(e) as ErrorWithMetadata;
|
||||||
if (isAstroError(e)) {
|
if (isAstroError(e)) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
@ -233,6 +214,7 @@ async function syncContentCollections(
|
||||||
...AstroErrorData.GenerateContentTypesError,
|
...AstroErrorData.GenerateContentTypesError,
|
||||||
hint,
|
hint,
|
||||||
message: AstroErrorData.GenerateContentTypesError.message(safeError.message),
|
message: AstroErrorData.GenerateContentTypesError.message(safeError.message),
|
||||||
|
location: safeError.loc,
|
||||||
},
|
},
|
||||||
{ cause: e },
|
{ cause: e },
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue