Skip to content
Open
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
19 changes: 15 additions & 4 deletions packages/react-grab/src/utils/open-file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { checkIsNextProject } from "../core/context.js";
import { buildOpenFileUrl } from "./build-open-file-url.js";

// Next.js App Router dev server injects a virtual path segment into stack frames.
// Strip it before passing the path to the launch-editor endpoint.
const stripAppRouterVirtualSegments = (filePath: string): string =>
filePath.replace(/\/\(app-pages-browser\)\//g, "/");

const tryDevServerOpen = async (
filePath: string,
lineNumber: number | undefined,
): Promise<boolean> => {
const isNextProject = checkIsNextProject();
const params = new URLSearchParams({ file: filePath });
const params = new URLSearchParams({
file: isNextProject ? stripAppRouterVirtualSegments(filePath) : filePath,
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path stripped twice due to redundant normalization

Low Severity

stripAppRouterVirtualSegments is applied twice for Next.js projects — once in openFile before passing normalizedPath to tryDevServerOpen, and again inside tryDevServerOpen on the already-stripped path. Since tryDevServerOpen is private and only called from openFile, one of these two call sites is redundant. The cleanest fix is to remove the conditional strip inside tryDevServerOpen (line 15), since the caller already normalizes the path.

Additional Locations (1)

Fix in Cursor Fix in Web


const lineKey = isNextProject ? "line1" : "line";
const columnKey = isNextProject ? "column1" : "column";
Expand All @@ -25,15 +32,19 @@ export const openFile = async (
lineNumber: number | undefined,
transformUrl?: (url: string, filePath: string, lineNumber?: number) => string,
): Promise<void> => {
const normalizedPath = checkIsNextProject()
? stripAppRouterVirtualSegments(filePath)
: filePath;

const wasOpenedByDevServer = await tryDevServerOpen(
filePath,
normalizedPath,
lineNumber,
).catch(() => false);
if (wasOpenedByDevServer) return;

const rawUrl = buildOpenFileUrl(filePath, lineNumber);
const rawUrl = buildOpenFileUrl(normalizedPath, lineNumber);
const url = transformUrl
? transformUrl(rawUrl, filePath, lineNumber)
? transformUrl(rawUrl, normalizedPath, lineNumber)
: rawUrl;
window.open(url, "_blank", "noopener,noreferrer");
};