Skip to content

Commit 2410754

Browse files
authored
fix(gatsby): resolve url file paths before reading (#38854)
resolve url file paths before reading
1 parent 5bd4c25 commit 2410754

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

packages/gatsby/src/utils/stack-trace-utils.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import stackTrace, { StackFrame } from "stack-trace"
2+
import url from "url"
23
import { codeFrameColumns } from "@babel/code-frame"
34
import {
45
TraceMap,
@@ -71,23 +72,32 @@ export const getNonGatsbyCodeFrame = ({
7172
const line = callSite.getLineNumber()
7273
const column = callSite.getColumnNumber()
7374

74-
const code = fs.readFileSync(fileName, { encoding: `utf-8` })
75-
return {
76-
fileName,
77-
line,
78-
column,
79-
codeFrame: codeFrameColumns(
80-
code,
81-
{
82-
start: {
83-
line,
84-
column,
75+
const normalizedFileName = fileName.startsWith(`file://`)
76+
? url.fileURLToPath(fileName)
77+
: fileName
78+
79+
try {
80+
const code = fs.readFileSync(normalizedFileName, { encoding: `utf-8` })
81+
return {
82+
fileName,
83+
line,
84+
column,
85+
codeFrame: codeFrameColumns(
86+
code,
87+
{
88+
start: {
89+
line,
90+
column,
91+
},
8592
},
86-
},
87-
{
88-
highlightCode,
89-
}
90-
),
93+
{
94+
highlightCode,
95+
}
96+
),
97+
}
98+
} catch (e) {
99+
console.error(`Errored getting code frame: ${e.stack}`)
100+
return null
91101
}
92102
}
93103

0 commit comments

Comments
 (0)