Unverified Commit d70b8542 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix pages index normalization for --debug-build-paths (#94341)

Recreation of https://github.com/vercel/next.js/pull/90314

---

## Summary
- Fix `--debug-build-paths` pages normalization in Turbopack so
`pages/<name>/index.tsx` maps to `/<name>` (matching JS
`getPageFromPath()` behavior).
- Handle index page edge case: `/index.tsx -> /`.
- Keep regression coverage in
`test/production/debug-build-path/debug-build-paths.test.ts` with a
generic fixture `pages/with-index/index.tsx`.

## Current Behavior Reference
- CI run (before this fix) that failed as expected for this bug:
-
https://github.com/vercel/next.js/actions/runs/22266469801/job/64413677099?pr=90314#step:35:216

## Root Cause
Rust-side debug-build-path normalization removed file extensions but did
not remove `/index`, producing route keys like `/with-index/index` while
pages route keys are `/with-index`. That mismatch filtered out the page
and caused `PageNotFoundError` during page-data collection.

---------
Co-authored-by: 's avatardevjiwonchoi <devjiwonchoi@gmail.com>
parent 3c06b5b3
...@@ -246,31 +246,47 @@ impl DebugBuildPathsRouteKeys { ...@@ -246,31 +246,47 @@ impl DebugBuildPathsRouteKeys {
.into()) .into())
} }
fn pages_route_key_from_debug_path(path: &RcStr) -> Result<RcStr> {
// Strip extension: "/foo.tsx" -> "/foo"
// Catch-all routes like "/foo/[...slug]" contain dots in the segment name;
// only treat the suffix as an extension when it is a plain alphanumeric token.
let file_name = path.rsplit('/').next().unwrap_or(path);
let result = if let Some(dot_idx) = file_name.rfind('.') {
let ext = &file_name[dot_idx + 1..];
if !ext.is_empty() && ext.chars().all(|c| c.is_ascii_alphanumeric()) {
let trimmed_len = path.len() - (file_name.len() - dot_idx);
path[..trimmed_len].into()
} else {
path.clone()
}
} else {
path.clone()
};
// Strip index suffix: "/foo/index.tsx" -> "/foo"
Ok(if let Some(stripped) = result.strip_suffix("/index") {
if stripped.is_empty() {
"/".into()
} else {
stripped.into()
}
} else {
result
})
}
fn from_debug_build_paths(paths: &DebugBuildPaths) -> Result<Self> { fn from_debug_build_paths(paths: &DebugBuildPaths) -> Result<Self> {
Ok(Self { Ok(Self {
app: paths app: paths
.app .app
.iter() .iter()
.map(|path| Self::app_route_key_from_debug_path(path)) .map(|path| Self::app_route_key_from_debug_path(path))
.collect::<Result<FxHashSet<_>>>()?, .collect::<Result<_>>()?,
pages: paths pages: paths
.pages .pages
.iter() .iter()
.map(|path| { .map(Self::pages_route_key_from_debug_path)
// Pages router: "/foo.tsx" -> "/foo" .collect::<Result<_>>()?,
// Catch-all routes like "/foo/[...slug]" contain dots in the segment name;
// only treat the suffix as an extension when it is a plain alphanumeric token.
let file_name = path.rsplit('/').next().unwrap_or(path);
if let Some(dot_idx) = file_name.rfind('.') {
let ext = &file_name[dot_idx + 1..];
if !ext.is_empty() && ext.chars().all(|c| c.is_ascii_alphanumeric()) {
let trimmed_len = path.len() - (file_name.len() - dot_idx);
return path[..trimmed_len].into();
}
}
path.clone()
})
.collect(),
}) })
} }
......
...@@ -83,7 +83,7 @@ describe('debug-build-paths', () => { ...@@ -83,7 +83,7 @@ describe('debug-build-paths', () => {
describe('glob pattern matching', () => { describe('glob pattern matching', () => {
it('should match app and pages routes with glob patterns', async () => { it('should match app and pages routes with glob patterns', async () => {
const buildResult = await next.build({ const buildResult = await next.build({
args: ['--debug-build-paths', 'pages/*.tsx,app/page.tsx'], args: ['--debug-build-paths', 'pages/**/*.tsx,app/page.tsx'],
}) })
expect(buildResult.exitCode).toBe(0) expect(buildResult.exitCode).toBe(0)
expect(buildResult.cliOutput).toBeDefined() expect(buildResult.cliOutput).toBeDefined()
...@@ -94,7 +94,8 @@ describe('debug-build-paths', () => { ...@@ -94,7 +94,8 @@ describe('debug-build-paths', () => {
└ ○ /_not-found └ ○ /_not-found
Route (pages) Route (pages)
┌ ○ /bar ┌ ○ /bar
└ ○ /foo" ├ ○ /foo
└ ○ /with-index"
`) `)
}) })
......
export default function WithIndexPage() {
return <main>With index page</main>
}
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