Unverified Commit 9a0214a1 authored by Vedant Madane's avatar Vedant Madane Committed by GitHub

fix: handle file:// URLs from import.meta.resolve in cache handler path (fixes #73796) (#90370)

## Summary

Fixes #73796

### The Bug

When "type": "module" is in package.json, users cannot use
require.resolve() for the cache handler path. They must use
import.meta.resolve() instead, which returns a file:// URL. Next.js was
concatenating this URL with .next/ producing invalid paths and
ERR_MODULE_NOT_FOUND errors.

### The Fix

1. resolveCacheHandlerPathToFilesystem() - New helper that converts
file:// URLs to proper filesystem paths using Node's fileURLToPath()
2. formatDynamicImportPath() - Now calls
resolveCacheHandlerPathToFilesystem() before any path operations
3. Applied in collect-build-traces.ts, build/index.ts, and
build/swc/index.ts

This ensures ESM import.meta.resolve() cache handler paths work
correctly in all code paths.

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent c6491ae4
......@@ -10,6 +10,7 @@ import {
} from './webpack/plugins/next-trace-entrypoints-plugin'
import path from 'path'
import { resolveCacheHandlerPathToFilesystem } from '../lib/format-dynamic-import-path'
import fs from 'fs/promises'
import { nonNullable } from '../lib/non-nullable'
import * as ciEnvironment from '../server/ci-info'
......@@ -147,11 +148,12 @@ export async function collectBuildTraces({
// ensure we trace any dependencies needed for custom
// incremental cache handler
if (cacheHandler) {
const resolvedPath = resolveCacheHandlerPathToFilesystem(cacheHandler)
sharedEntriesSet.push(
require.resolve(
path.isAbsolute(cacheHandler)
? cacheHandler
: path.join(dir, cacheHandler)
path.isAbsolute(resolvedPath)
? resolvedPath
: path.join(dir, resolvedPath)
)
)
}
......@@ -178,11 +180,13 @@ export async function collectBuildTraces({
if (cacheHandlers) {
for (const handlerPath of Object.values(cacheHandlers)) {
if (handlerPath) {
const resolvedPath =
resolveCacheHandlerPathToFilesystem(handlerPath)
sharedEntriesSet.push(
require.resolve(
path.isAbsolute(handlerPath)
? handlerPath
: path.join(dir, handlerPath)
path.isAbsolute(resolvedPath)
? resolvedPath
: path.join(dir, resolvedPath)
)
)
}
......
......@@ -22,6 +22,7 @@ import devalue from 'next/dist/compiled/devalue'
import findUp from 'next/dist/compiled/find-up'
import { nanoid } from 'next/dist/compiled/nanoid/index.cjs'
import path from 'path'
import { resolveCacheHandlerPathToFilesystem } from '../lib/format-dynamic-import-path'
import {
STATIC_STATUS_PAGE_GET_INITIAL_PROPS_ERROR,
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
......@@ -1797,7 +1798,10 @@ export default async function build(
runtimeConfig.cacheHandlers || {}
)) {
if (key && value) {
normalizedCacheHandlers[key] = path.relative(distDir, value)
normalizedCacheHandlers[key] = path.relative(
distDir,
resolveCacheHandlerPathToFilesystem(value)
)
}
}
......@@ -1816,7 +1820,12 @@ export default async function build(
}
: {}),
cacheHandler: runtimeConfig.cacheHandler
? path.relative(distDir, runtimeConfig.cacheHandler)
? path.relative(
distDir,
resolveCacheHandlerPathToFilesystem(
runtimeConfig.cacheHandler
)
)
: runtimeConfig.cacheHandler,
cacheHandlers: normalizedCacheHandlers,
experimental: {
......
import path from 'path'
import { pathToFileURL } from 'url'
import { resolveCacheHandlerPathToFilesystem } from '../../lib/format-dynamic-import-path'
import { arch, platform } from 'os'
import { platformArchTriples } from 'next/dist/compiled/@napi-rs/triples'
import * as Log from '../output/log'
......@@ -936,12 +937,15 @@ function bindingToApi(
// cacheHandler can be an absolute path, we need it to be relative for turbopack.
if (nextConfigSerializable.cacheHandler) {
const resolvedCacheHandler = resolveCacheHandlerPathToFilesystem(
nextConfigSerializable.cacheHandler
)
nextConfigSerializable.cacheHandler =
'./' +
normalizePathOnWindows(
path.isAbsolute(nextConfigSerializable.cacheHandler)
? path.relative(projectPath, nextConfigSerializable.cacheHandler)
: nextConfigSerializable.cacheHandler
path.isAbsolute(resolvedCacheHandler)
? path.relative(projectPath, resolvedCacheHandler)
: resolvedCacheHandler
)
}
if (nextConfigSerializable.cacheHandlers) {
......@@ -950,15 +954,18 @@ function bindingToApi(
nextConfigSerializable.cacheHandlers as Record<string, string>
)
.filter(([_, value]) => value != null)
.map(([key, value]) => [
key,
'./' +
normalizePathOnWindows(
path.isAbsolute(value)
? path.relative(projectPath, value)
: value
),
])
.map(([key, value]) => {
const resolved = resolveCacheHandlerPathToFilesystem(value)
return [
key,
'./' +
normalizePathOnWindows(
path.isAbsolute(resolved)
? path.relative(projectPath, resolved)
: resolved
),
]
})
)
}
......
import path from 'path'
import { pathToFileURL } from 'url'
import { pathToFileURL, fileURLToPath } from 'url'
/**
* Converts a cache handler path to a filesystem path.
* ESM's import.meta.resolve() returns file:// URLs which break when concatenated
* with .next/ or used with path.join/path.relative.
* @param filePath - Absolute path, relative path, or file:// URL (e.g. from import.meta.resolve)
* @returns A filesystem path suitable for path operations
*/
export function resolveCacheHandlerPathToFilesystem(filePath: string): string {
if (filePath.startsWith('file://')) {
return fileURLToPath(filePath)
}
return filePath
}
/**
* The path for a dynamic route must be URLs with a valid scheme.
......@@ -7,12 +21,14 @@ import { pathToFileURL } from 'url'
* When an absolute Windows path is passed to it, it interprets the beginning of the path as a protocol (`C:`).
* Therefore, it is important to always construct a complete path.
* @param dir File directory
* @param filePath Absolute or relative path
* @param filePath Absolute or relative path, or file:// URL (e.g. from import.meta.resolve)
*/
export const formatDynamicImportPath = (dir: string, filePath: string) => {
const absoluteFilePath = path.isAbsolute(filePath)
? filePath
: path.join(dir, filePath)
const resolvedFilePath = resolveCacheHandlerPathToFilesystem(filePath)
const absoluteFilePath = path.isAbsolute(resolvedFilePath)
? resolvedFilePath
: path.join(dir, resolvedFilePath)
const formattedFilePath = pathToFileURL(absoluteFilePath).toString()
return formattedFilePath
......
......@@ -40,6 +40,7 @@ import { dset } from '../shared/lib/dset'
import { normalizeZodErrors } from '../shared/lib/zod'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
import { findDir } from '../lib/find-pages-dir'
import { resolveCacheHandlerPathToFilesystem } from '../lib/format-dynamic-import-path'
import { interopDefault } from '../lib/interop-default'
import { djb2Hash } from '../shared/lib/hash'
import type { NextAdapter } from '../build/adapter/build-complete'
......@@ -1297,7 +1298,10 @@ function assignDefaultsAndValidate(
}
)[key]
if (handlerPath && !existsSync(handlerPath)) {
const resolvedHandlerPath =
handlerPath && resolveCacheHandlerPathToFilesystem(handlerPath)
if (resolvedHandlerPath && !existsSync(resolvedHandlerPath)) {
invalidHandlerItems.push({
key,
reason: `cache handler path provided does not exist, received ${handlerPath}`,
......
......@@ -6,6 +6,9 @@ const originalNextConfig = fs.readFileSync(
__dirname + '/next.config.js',
'utf8'
)
const importMetaResolveNextConfig = `export default {
cacheHandler: import.meta.resolve('./cache-handler-esm.js'),
}`
function runTests(
exportType: string,
......@@ -84,3 +87,23 @@ describe('app-dir - custom-cache-handler - esm', () => {
runTests('esm default export', { next, isNextDev })
})
describe('app-dir - custom-cache-handler - esm import.meta.resolve', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: {
app: new FileRef(__dirname + '/app'),
'cache-handler-esm.js': new FileRef(__dirname + '/cache-handler-esm.js'),
'next.config.js': importMetaResolveNextConfig,
},
skipDeployment: true,
packageJson: {
type: 'module',
},
})
if (skipped) {
return
}
runTests('esm default export', { next, isNextDev })
})
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