Unverified Commit 329bcdc7 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[devtools] Fix "open in editor" for locations in stackframes (#82013)

parent 504fa32a
...@@ -27,6 +27,7 @@ import fs from 'fs' ...@@ -27,6 +27,7 @@ import fs from 'fs'
import fsp from 'fs/promises' import fsp from 'fs/promises'
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import { fileURLToPath } from 'url'
import shellQuote from 'next/dist/compiled/shell-quote' import shellQuote from 'next/dist/compiled/shell-quote'
function isTerminalEditor(editor: string) { function isTerminalEditor(editor: string) {
...@@ -426,15 +427,33 @@ export function launchEditor(fileName: string, line1: number, column1: number) { ...@@ -426,15 +427,33 @@ export function launchEditor(fileName: string, line1: number, column1: number) {
} }
} }
// Open the file in editor if exists, otherwise return an error /**
* Open the file in editor if exists, otherwise return an error
* @param file `file:` URL, or absolute path or relative path. Relative paths must
* relative to `nextRootDirectory`.
*/
export async function openFileInEditor( export async function openFileInEditor(
filePath: string, file: string,
line1: number, line1: number,
column1: number column1: number,
) { nextRootDirectory: string
): Promise<{ found: boolean; error: unknown | null }> {
let filePath: string
if (file.startsWith('file://')) {
try {
filePath = fileURLToPath(file)
} catch (error) {
return { found: false, error }
}
} else if (path.isAbsolute(file)) {
filePath = file
} else {
filePath = path.join(nextRootDirectory, file)
}
const result = { const result = {
found: false, found: false,
error: null as Error | null, error: null as unknown | null,
} }
const existed = await fsp.access(filePath, fs.constants.F_OK).then( const existed = await fsp.access(filePath, fs.constants.F_OK).then(
() => true, () => true,
...@@ -445,7 +464,7 @@ export async function openFileInEditor( ...@@ -445,7 +464,7 @@ export async function openFileInEditor(
launchEditor(filePath, line1, column1) launchEditor(filePath, line1, column1)
result.found = true result.found = true
} catch (err) { } catch (err) {
result.error = err as Error result.error = err
} }
} }
return result return result
......
...@@ -370,25 +370,28 @@ export function getOverlayMiddleware({ ...@@ -370,25 +370,28 @@ export function getOverlayMiddleware({
let openEditorResult let openEditorResult
if (isAppRelativePath) { if (isAppRelativePath) {
const relativeFilePath = searchParams.get('file') || '' const relativeFilePath = searchParams.get('file') || ''
const absoluteFilePath = path.join( const appPath = path.join(
projectPath,
'app', 'app',
isSrcDir ? 'src' : '', isSrcDir ? 'src' : '',
relativeFilePath relativeFilePath
) )
openEditorResult = await openFileInEditor(absoluteFilePath, 1, 1) openEditorResult = await openFileInEditor(appPath, 1, 1, projectPath)
} else { } else {
const frame = createStackFrame(searchParams) const frame = createStackFrame(searchParams)
if (!frame) return middlewareResponse.badRequest(res) if (!frame) return middlewareResponse.badRequest(res)
openEditorResult = await openFileInEditor( openEditorResult = await openFileInEditor(
frame.file, frame.file,
frame.line ?? 1, frame.line ?? 1,
frame.column ?? 1 frame.column ?? 1,
projectPath
) )
} }
if (openEditorResult.error) { if (openEditorResult.error) {
return middlewareResponse.internalServerError(res) return middlewareResponse.internalServerError(
res,
openEditorResult.error
)
} }
if (!openEditorResult.found) { if (!openEditorResult.found) {
return middlewareResponse.notFound(res) return middlewareResponse.notFound(res)
......
...@@ -600,23 +600,21 @@ export function getOverlayMiddleware(options: { ...@@ -600,23 +600,21 @@ export function getOverlayMiddleware(options: {
const isAppRelativePath = searchParams.get('isAppRelativePath') === '1' const isAppRelativePath = searchParams.get('isAppRelativePath') === '1'
if (isAppRelativePath) { if (isAppRelativePath) {
const relativeFilePath = searchParams.get('file') || '' const relativeFilePath = searchParams.get('file') || ''
const absoluteFilePath = path.join( const appPath = path.join(
rootDirectory,
'app', 'app',
isSrcDir ? 'src' : '', isSrcDir ? 'src' : '',
relativeFilePath relativeFilePath
) )
openEditorResult = await openFileInEditor(absoluteFilePath, 1, 1) openEditorResult = await openFileInEditor(appPath, 1, 1, rootDirectory)
} else { } else {
// TODO: How do we differentiate layers and actual file paths with round brackets?
// frame files may start with their webpack layer, like (middleware)/middleware.js // frame files may start with their webpack layer, like (middleware)/middleware.js
const filePath = path.resolve( const filePath = frame.file.replace(/^\([^)]+\)\//, '')
rootDirectory,
frame.file.replace(/^\([^)]+\)\//, '')
)
openEditorResult = await openFileInEditor( openEditorResult = await openFileInEditor(
filePath, filePath,
frame.line1, frame.line1,
frame.column1 ?? 1 frame.column1 ?? 1,
rootDirectory
) )
} }
if (openEditorResult.error) { if (openEditorResult.error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment