Unverified Commit b3bc0d8c authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Show relative path from cwd for Proxy Middleware file conflict error (#84993)

Show the relative path from cwd when a Proxy Middleware file conflict
error occurs to better inform the users. Also, good for double-checking
the detection logic.

Closes NEXT-4740
parent c5678959
...@@ -881,5 +881,6 @@ ...@@ -881,5 +881,6 @@
"880": "Config options `experimental.proxyPrefetch` and `experimental.middlewarePrefetch` cannot be set at the same time. Please use `experimental.proxyPrefetch` instead.", "880": "Config options `experimental.proxyPrefetch` and `experimental.middlewarePrefetch` cannot be set at the same time. Please use `experimental.proxyPrefetch` instead.",
"881": "Invalid render stage: %s", "881": "Invalid render stage: %s",
"882": "The %s file \"./%s\" must export a function named \\`%s\\` or a default function.", "882": "The %s file \"./%s\" must export a function named \\`%s\\` or a default function.",
"883": "The %s file \"%s\" must export a function named \\`%s\\` or a default function." "883": "The %s file \"%s\" must export a function named \\`%s\\` or a default function.",
"884": "Both %s file \"./%s\" and %s file \"./%s\" are detected. Please use \"./%s\" only."
} }
...@@ -1190,17 +1190,42 @@ export default async function build( ...@@ -1190,17 +1190,42 @@ export default async function build(
.sort(sortByPageExts(config.pageExtensions)) .sort(sortByPageExts(config.pageExtensions))
.map((file) => path.join(rootDir, file).replace(dir, '')) .map((file) => path.join(rootDir, file).replace(dir, ''))
const hasInstrumentationHook = rootPaths.some((p) => let instrumentationHookFilePath: string | undefined
p.includes(INSTRUMENTATION_HOOK_FILENAME) let proxyFilePath: string | undefined
) let middlewareFilePath: string | undefined
const hasMiddlewareFile = rootPaths.some((p) =>
p.includes(MIDDLEWARE_FILENAME) for (const rootPath of rootPaths) {
) const { name: fileBaseName, dir: fileDir } = path.parse(rootPath)
const hasProxyFile = rootPaths.some((p) => p.includes(PROXY_FILENAME)) const isAtConventionLevel =
if (hasMiddlewareFile) { fileDir === '/' ||
if (hasProxyFile) { fileDir === '/src' ||
// rootPaths are currently relative paths from the root directory.
// Add safety check here for unexpected future changes.
fileDir === dir ||
fileDir === path.join(dir, 'src')
if (isAtConventionLevel && fileBaseName === MIDDLEWARE_FILENAME) {
middlewareFilePath = rootPath
}
if (isAtConventionLevel && fileBaseName === PROXY_FILENAME) {
proxyFilePath = rootPath
}
if (
isAtConventionLevel &&
fileBaseName === INSTRUMENTATION_HOOK_FILENAME
) {
instrumentationHookFilePath = rootPath
}
}
if (middlewareFilePath) {
if (proxyFilePath) {
const cwd = process.cwd()
const absoluteProxyPath = path.join(rootDir, proxyFilePath)
const absoluteMiddlewarePath = path.join(rootDir, middlewareFilePath)
throw new Error( throw new Error(
`Both "${MIDDLEWARE_FILENAME}" and "${PROXY_FILENAME}" files are detected. Please use "${PROXY_FILENAME}" instead.` `Both ${MIDDLEWARE_FILENAME} file "./${path.relative(cwd, absoluteMiddlewarePath)}" and ${PROXY_FILENAME} file "./${path.relative(cwd, absoluteProxyPath)}" are detected. Please use "./${path.relative(cwd, absoluteProxyPath)}" only.`
) )
} }
Log.warnOnce( Log.warnOnce(
...@@ -1208,6 +1233,10 @@ export default async function build( ...@@ -1208,6 +1233,10 @@ export default async function build(
) )
} }
const hasInstrumentationHook = Boolean(instrumentationHookFilePath)
const hasMiddlewareFile = Boolean(middlewareFilePath)
const hasProxyFile = Boolean(proxyFilePath)
NextBuildContext.hasInstrumentationHook = hasInstrumentationHook NextBuildContext.hasInstrumentationHook = hasInstrumentationHook
const previewProps: __ApiPreviewProps = await generatePreviewKeys({ const previewProps: __ApiPreviewProps = await generatePreviewKeys({
......
...@@ -391,8 +391,8 @@ async function startWatcher( ...@@ -391,8 +391,8 @@ async function startWatcher(
sortByPageExts(nextConfig.pageExtensions) sortByPageExts(nextConfig.pageExtensions)
) )
let hasMiddlewareFile = false let proxyFilePath: string | undefined
let hasProxyFile = false let middlewareFilePath: string | undefined
for (const fileName of sortedKnownFiles) { for (const fileName of sortedKnownFiles) {
if ( if (
...@@ -408,16 +408,18 @@ async function startWatcher( ...@@ -408,16 +408,18 @@ async function startWatcher(
fileDir === dir || fileDir === path.join(dir, 'src') fileDir === dir || fileDir === path.join(dir, 'src')
if (isAtConventionLevel && fileBaseName === MIDDLEWARE_FILENAME) { if (isAtConventionLevel && fileBaseName === MIDDLEWARE_FILENAME) {
hasMiddlewareFile = true middlewareFilePath = fileName
} }
if (isAtConventionLevel && fileBaseName === PROXY_FILENAME) { if (isAtConventionLevel && fileBaseName === PROXY_FILENAME) {
hasProxyFile = true proxyFilePath = fileName
} }
if (hasMiddlewareFile) { if (middlewareFilePath) {
if (hasProxyFile) { if (proxyFilePath) {
const cwd = process.cwd()
throw new Error( throw new Error(
`Both "${MIDDLEWARE_FILENAME}" and "${PROXY_FILENAME}" files are detected. Please use "${PROXY_FILENAME}" instead.` `Both ${MIDDLEWARE_FILENAME} file "./${path.relative(cwd, middlewareFilePath)}" and ${PROXY_FILENAME} file "./${path.relative(cwd, proxyFilePath)}" are detected. Please use "./${path.relative(cwd, proxyFilePath)}" only.`
) )
} }
Log.warnOnce( Log.warnOnce(
......
...@@ -12,16 +12,15 @@ describe('proxy-with-middleware', () => { ...@@ -12,16 +12,15 @@ describe('proxy-with-middleware', () => {
} }
it('should error when both middleware and proxy files are detected', async () => { it('should error when both middleware and proxy files are detected', async () => {
const message =
'Both middleware file "./middleware.ts" and proxy file "./proxy.ts" are detected. Please use "./proxy.ts" only.'
if (isNextDev) { if (isNextDev) {
await next.start().catch(() => {}) await next.start().catch(() => {})
expect(next.cliOutput).toContain( expect(next.cliOutput).toContain(message)
'Both "middleware" and "proxy" files are detected. Please use "proxy" instead.'
)
} else { } else {
const { cliOutput } = await next.build() const { cliOutput } = await next.build()
expect(cliOutput).toContain( expect(cliOutput).toContain(message)
'Both "middleware" and "proxy" files are detected. Please use "proxy" instead.'
)
} }
}) })
}) })
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