Skip to content
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

fix: error overlay message escape #12305

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-plums-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where the error overlay would not escape the message
2 changes: 2 additions & 0 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function enhanceViteSSRError({
}

export interface AstroErrorPayload {
__isEnhancedAstroErrorPayload: true;
type: ErrorPayload['type'];
err: Omit<ErrorPayload['err'], 'loc'> & {
name?: string;
Expand Down Expand Up @@ -164,6 +165,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
: undefined;

return {
__isEnhancedAstroErrorPayload: true,
type: 'error',
err: {
...err,
Expand Down
21 changes: 21 additions & 0 deletions packages/astro/src/core/module-loader/vite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EventEmitter } from 'node:events';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type * as vite from 'vite';
import { collectErrorMetadata } from '../errors/dev/utils.js';
import { getViteErrorPayload } from '../errors/dev/vite.js';
import type { ModuleLoader, ModuleLoaderEventEmitter } from './loader.js';

export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
Expand Down Expand Up @@ -43,6 +46,24 @@ export function createViteLoader(viteServer: vite.ViteDevServer): ModuleLoader {
}
const msg = args[0] as vite.HMRPayload;
if (msg?.type === 'error') {
// If we have an error, but it didn't go through our error enhancement program, it means that it's a HMR error from
// vite itself, which goes through a different path. We need to enhance it here.
if (!(msg as any)['__isEnhancedAstroErrorPayload']) {
const err = collectErrorMetadata(msg.err, pathToFileURL(viteServer.config.root));
getViteErrorPayload(err).then((payload) => {
events.emit('hmr-error', {
type: 'error',
err: {
message: payload.err.message,
stack: payload.err.stack,
},
});

args[0] = payload;
_wsSend.apply(this, args);
});
return;
}
events.emit('hmr-error', msg);
}
_wsSend.apply(this, args);
Expand Down
Loading