-
-
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
Fix incorrect path in error overlay on Win #6679
Changes from all commits
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,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fix incorrect path to file in error overlay on Win |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,13 @@ import * as fs from 'node:fs'; | |
import { isAbsolute, join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import stripAnsi from 'strip-ansi'; | ||
import { normalizePath } from 'vite'; | ||
import type { ESBuildTransformResult } from 'vite'; | ||
import type { SSRError } from '../../../@types/astro.js'; | ||
import { AggregateError, type ErrorWithMetadata } from '../errors.js'; | ||
import { codeFrame } from '../printer.js'; | ||
import { normalizeLF } from '../utils.js'; | ||
import { removeLeadingForwardSlashWindows } from '../../path.js'; | ||
|
||
type EsbuildMessage = ESBuildTransformResult['warnings'][number]; | ||
|
||
|
@@ -32,10 +34,15 @@ export function collectErrorMetadata(e: any, rootFolder?: URL | undefined): Erro | |
// - We'll fail to show the file's content in the browser | ||
// - We'll fail to show the code frame in the terminal | ||
// - The "Open in Editor" button won't work | ||
|
||
// Normalize the paths so that we can correctly detect if it's absolute on any platform | ||
const normalizedFile = normalizePath(error.loc?.file || ''); | ||
const normalizedRootFolder = removeLeadingForwardSlashWindows(rootFolder?.pathname || ''); | ||
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. Remove a leading forward slash which |
||
|
||
if ( | ||
error.loc?.file && | ||
rootFolder && | ||
(!error.loc.file.startsWith(rootFolder.pathname) || !isAbsolute(error.loc.file)) | ||
(!normalizedFile?.startsWith(normalizedRootFolder) || !isAbsolute(normalizedFile)) | ||
) { | ||
error.loc.file = join(fileURLToPath(rootFolder), error.loc.file); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -637,7 +637,8 @@ class ErrorOverlay extends HTMLElement { | |
const codeContent = code.querySelector<HTMLDivElement>('#code-content'); | ||
|
||
if (codeHeader) { | ||
const cleanFile = err.loc.file.split('/').slice(-2).join('/'); | ||
const separator = err.loc.file.includes('/') ? '/' : '\\'; | ||
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. Account for Windows separators |
||
const cleanFile = err.loc.file.split(separator).slice(-2).join('/'); | ||
const fileLocation = [cleanFile, err.loc.line, err.loc.column].filter(Boolean).join(':'); | ||
const absoluteFileLocation = [err.loc.file, err.loc.line, err.loc.column] | ||
.filter(Boolean) | ||
|
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.
Two different tests because on Windows the path separators are different depending on the location of the error (i.e. component vs page).