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