Unverified Commit 7a08f008 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix trace ignore handling (#56674)

This ensures we separate our ignore handling for next-server runtime entries and `.next/server` chunks. When these were combined our ignores caused modules that should were actually needed by user code to be excluded. 

Verified patch against the provided minimal repros

![CleanShot 2023-10-10 at 11 56 36@2x](https://github.com/vercel/next.js/assets/22380829/1dd83996-fa95-462a-98ed-485ce6c4d3f5)

![CleanShot 2023-10-10 at 12 01 08@2x](https://github.com/vercel/next.js/assets/22380829/c17c6147-0e3e-422b-bf2d-5fedfd6827ef)


x-ref: https://github.com/vercel/next.js/pull/56048#discussion_r1345651964
Fixes: https://github.com/vercel/next.js/issues/56357
parent 52356a0e
...@@ -233,7 +233,7 @@ export async function collectBuildTraces({ ...@@ -233,7 +233,7 @@ export async function collectBuildTraces({
}) })
} }
} }
const ignores = [ const serverIgnores = [
'**/*.d.ts', '**/*.d.ts',
'**/*.map', '**/*.map',
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*', isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
...@@ -241,6 +241,8 @@ export async function collectBuildTraces({ ...@@ -241,6 +241,8 @@ export async function collectBuildTraces({
'**/node_modules/webpack5/**/*', '**/node_modules/webpack5/**/*',
'**/next/dist/server/lib/squoosh/**/*.wasm', '**/next/dist/server/lib/squoosh/**/*.wasm',
'**/next/dist/server/lib/route-resolver*', '**/next/dist/server/lib/route-resolver*',
'**/next/dist/pages/**/*',
...(ciEnvironment.hasNextSupport ...(ciEnvironment.hasNextSupport
? [ ? [
// only ignore image-optimizer code when // only ignore image-optimizer code when
...@@ -261,12 +263,12 @@ export async function collectBuildTraces({ ...@@ -261,12 +263,12 @@ export async function collectBuildTraces({
...(config.experimental.outputFileTracingIgnores || []), ...(config.experimental.outputFileTracingIgnores || []),
].filter(nonNullable) ].filter(nonNullable)
const ignoreFn = (pathname: string) => { const serverIgnoreFn = (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) { if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true return true
} }
return isMatch(pathname, ignores, { return isMatch(pathname, serverIgnores, {
contains: true, contains: true,
dot: true, dot: true,
}) })
...@@ -321,7 +323,7 @@ export async function collectBuildTraces({ ...@@ -321,7 +323,7 @@ export async function collectBuildTraces({
[minimalServerTracedFiles, minimalFiles], [minimalServerTracedFiles, minimalFiles],
] as [Set<string>, string[]][]) { ] as [Set<string>, string[]][]) {
for (const file of files) { for (const file of files) {
if (!ignoreFn(path.join(traceContext, file))) { if (!serverIgnoreFn(path.join(traceContext, file))) {
addToTracedFiles(traceContext, file, set) addToTracedFiles(traceContext, file, set)
} }
} }
...@@ -336,7 +338,6 @@ export async function collectBuildTraces({ ...@@ -336,7 +338,6 @@ export async function collectBuildTraces({
const result = await nodeFileTrace(chunksToTrace, { const result = await nodeFileTrace(chunksToTrace, {
base: outputFileTracingRoot, base: outputFileTracingRoot,
processCwd: dir, processCwd: dir,
ignore: ignoreFn,
mixedModules: true, mixedModules: true,
}) })
const reasons = result.reasons const reasons = result.reasons
...@@ -360,12 +361,7 @@ export async function collectBuildTraces({ ...@@ -360,12 +361,7 @@ export async function collectBuildTraces({
for (const curFile of curFiles || []) { for (const curFile of curFiles || []) {
const filePath = path.join(outputFileTracingRoot, curFile) const filePath = path.join(outputFileTracingRoot, curFile)
if ( if (!serverIgnoreFn(filePath)) {
!isMatch(filePath, '**/next/dist/pages/**/*', {
dot: true,
contains: true,
})
) {
tracedFiles.add( tracedFiles.add(
path.relative(distDir, filePath).replace(/\\/g, '/') path.relative(distDir, filePath).replace(/\\/g, '/')
) )
......
...@@ -147,6 +147,28 @@ export function runTests(ctx) { ...@@ -147,6 +147,28 @@ export function runTests(ctx) {
slowImageServer.stop() slowImageServer.stop()
}) })
if (!isDev && ctx.isSharp && ctx.nextConfigImages) {
it('should handle custom sharp usage', async () => {
const res = await fetchViaHTTP(ctx.appPort, '/api/custom-sharp')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ success: true })
const traceFile = await fs.readJson(
join(
ctx.appDir,
'.next',
'server',
'pages',
'api',
'custom-sharp.js.nft.json'
)
)
expect(traceFile.files.some((file) => file.includes('sharp/build'))).toBe(
true
)
})
}
if (domains.length > 0) { if (domains.length > 0) {
it('should normalize invalid status codes', async () => { it('should normalize invalid status codes', async () => {
const url = `http://localhost:${ const url = `http://localhost:${
...@@ -1435,6 +1457,20 @@ export const setupTests = (ctx) => { ...@@ -1435,6 +1457,20 @@ export const setupTests = (ctx) => {
}) })
curCtx.nextOutput = '' curCtx.nextOutput = ''
nextConfig.replace('{ /* replaceme */ }', json) nextConfig.replace('{ /* replaceme */ }', json)
if (curCtx.isSharp) {
await fs.writeFile(
join(curCtx.appDir, 'pages', 'api', 'custom-sharp.js'),
`
import sharp from 'sharp'
export default function handler(req, res) {
console.log(sharp)
res.json({ success: true })
}
`
)
}
await nextBuild(curCtx.appDir) await nextBuild(curCtx.appDir)
await cleanImagesDir(ctx) await cleanImagesDir(ctx)
curCtx.appPort = await findPort() curCtx.appPort = await findPort()
...@@ -1452,6 +1488,11 @@ export const setupTests = (ctx) => { ...@@ -1452,6 +1488,11 @@ export const setupTests = (ctx) => {
}) })
afterAll(async () => { afterAll(async () => {
nextConfig.restore() nextConfig.restore()
if (curCtx.isSharp) {
await fs.remove(
join(curCtx.appDir, 'pages', 'api', 'custom-sharp.js')
)
}
if (curCtx.app) await killApp(curCtx.app) if (curCtx.app) await killApp(curCtx.app)
}) })
......
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