Unverified Commit 5321ba84 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[segment explorer] normalize path when running inside monorepo (#82146)

When you running `pnpm next <project path>` from the monorepo root with turbopack and the `<project path>` is not same as your root directory, this lead the unrelated paths outside of the next.js project are also included into the segment explorer file paths. Because in turbopack the current directory is changed to `[project]` after bundling so that we didn't get the correct relative path when running segment explorer.

The absolute path is formatted as `<project-root>/<cwd>`,  and the `<project root>` will be included into the path if the working directory is not same as the next.js directory. The PR fixes the issue by extracting the `<project-root>` from the absolute path, and also stripped from the segment explorer paths.

Closes NEXT-4651
parent c9e5069e
...@@ -4145,9 +4145,9 @@ const getGlobalErrorStyles = async ( ...@@ -4145,9 +4145,9 @@ const getGlobalErrorStyles = async (
} }
if (ctx.renderOpts.dev) { if (ctx.renderOpts.dev) {
const dir = const dir =
process.env.NEXT_RUNTIME === 'edge' (process.env.NEXT_RUNTIME === 'edge'
? process.env.__NEXT_EDGE_PROJECT_DIR! ? process.env.__NEXT_EDGE_PROJECT_DIR
: ctx.renderOpts.dir || '' : ctx.renderOpts.dir) || ''
const globalErrorModulePath = normalizeConventionFilePath( const globalErrorModulePath = normalizeConventionFilePath(
dir, dir,
......
...@@ -417,9 +417,9 @@ async function createComponentTreeInternal( ...@@ -417,9 +417,9 @@ async function createComponentTreeInternal(
process.env.NODE_ENV === 'development' && process.env.NODE_ENV === 'development' &&
ctx.renderOpts.devtoolSegmentExplorer ctx.renderOpts.devtoolSegmentExplorer
const dir = const dir =
process.env.NEXT_RUNTIME === 'edge' (process.env.NEXT_RUNTIME === 'edge'
? process.env.__NEXT_EDGE_PROJECT_DIR! ? process.env.__NEXT_EDGE_PROJECT_DIR
: ctx.renderOpts.dir || '' : ctx.renderOpts.dir) || ''
// Use the same condition to render metadataOutlet as metadata // Use the same condition to render metadataOutlet as metadata
const metadataOutlet = StreamingMetadataOutlet ? ( const metadataOutlet = StreamingMetadataOutlet ? (
...@@ -1139,9 +1139,9 @@ async function createBoundaryConventionElement({ ...@@ -1139,9 +1139,9 @@ async function createBoundaryConventionElement({
process.env.NODE_ENV === 'development' && process.env.NODE_ENV === 'development' &&
ctx.renderOpts.devtoolSegmentExplorer ctx.renderOpts.devtoolSegmentExplorer
const dir = const dir =
process.env.NEXT_RUNTIME === 'edge' (process.env.NEXT_RUNTIME === 'edge'
? process.env.__NEXT_EDGE_PROJECT_DIR! ? process.env.__NEXT_EDGE_PROJECT_DIR
: ctx.renderOpts.dir || '' : ctx.renderOpts.dir) || ''
const { SegmentViewNode } = ctx.componentMod const { SegmentViewNode } = ctx.componentMod
const element = Component ? ( const element = Component ? (
<> <>
......
...@@ -9,11 +9,17 @@ export function normalizeConventionFilePath( ...@@ -9,11 +9,17 @@ export function normalizeConventionFilePath(
projectDir: string, projectDir: string,
conventionPath: string | undefined conventionPath: string | undefined
) { ) {
// Turbopack project path is formed as: "<project root>/<cwd>".
// When project root is not the working directory, we can extract the relative project root path.
// This is mostly used for running Next.js inside a monorepo.
const cwd = process.env.NEXT_RUNTIME === 'edge' ? '' : process.cwd() const cwd = process.env.NEXT_RUNTIME === 'edge' ? '' : process.cwd()
const relativeProjectRoot = projectDir.replace(cwd, '')
let relativePath = (conventionPath || '') let relativePath = (conventionPath || '')
// remove turbopack [project] prefix // remove turbopack [project] prefix
.replace(/^\[project\][\\/]/, '') .replace(/^\[project\]/, '')
// remove turbopack relative project path, everything after [project] and before the working directory.
.replace(relativeProjectRoot, '')
// remove the project root from the path // remove the project root from the path
.replace(projectDir, '') .replace(projectDir, '')
// remove cwd prefix // remove cwd prefix
......
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