Unverified Commit 780594f7 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix recursive ignoring case in build traces (#60740)

This ensures when a dependency has a recursive require our should ignore
handling doesn't accidentally loop back and forth.

x-ref: https://github.com/lovell/sharp/issues/3944

Closes NEXT-2121
parent 18864787
...@@ -37,7 +37,8 @@ function shouldIgnore( ...@@ -37,7 +37,8 @@ function shouldIgnore(
file: string, file: string,
serverIgnoreFn: (file: string) => boolean, serverIgnoreFn: (file: string) => boolean,
reasons: NodeFileTraceReasons, reasons: NodeFileTraceReasons,
cachedIgnoreFiles: Map<string, boolean> cachedIgnoreFiles: Map<string, boolean>,
children: Set<string> = new Set()
) { ) {
if (cachedIgnoreFiles.has(file)) { if (cachedIgnoreFiles.has(file)) {
return cachedIgnoreFiles.get(file) return cachedIgnoreFiles.get(file)
...@@ -47,6 +48,7 @@ function shouldIgnore( ...@@ -47,6 +48,7 @@ function shouldIgnore(
cachedIgnoreFiles.set(file, true) cachedIgnoreFiles.set(file, true)
return true return true
} }
children.add(file)
const reason = reasons.get(file) const reason = reasons.get(file)
if (!reason || reason.parents.size === 0 || reason.type.includes('initial')) { if (!reason || reason.parents.size === 0 || reason.type.includes('initial')) {
...@@ -54,17 +56,30 @@ function shouldIgnore( ...@@ -54,17 +56,30 @@ function shouldIgnore(
return false return false
} }
if ( // if all parents are ignored the child file
[...reason.parents.values()].every((parent) => // should be ignored as well
shouldIgnore(parent, serverIgnoreFn, reasons, cachedIgnoreFiles) let allParentsIgnored = true
)
) { for (const parent of reason.parents.values()) {
cachedIgnoreFiles.set(file, true) if (!children.has(parent)) {
return true children.add(parent)
if (
!shouldIgnore(
parent,
serverIgnoreFn,
reasons,
cachedIgnoreFiles,
children
)
) {
allParentsIgnored = false
break
}
}
} }
cachedIgnoreFiles.set(file, false) cachedIgnoreFiles.set(file, allParentsIgnored)
return false return allParentsIgnored
} }
export async function collectBuildTraces({ export async function collectBuildTraces({
...@@ -265,6 +280,17 @@ export async function collectBuildTraces({ ...@@ -265,6 +280,17 @@ export async function collectBuildTraces({
} }
} }
const makeIgnoreFn = (ignores: string[]) => (pathname: string) => {
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}
return isMatch(pathname, ignores, {
contains: true,
dot: true,
})
}
const sharedIgnores = [ const sharedIgnores = [
'**/next/dist/compiled/next-server/**/*.dev.js', '**/next/dist/compiled/next-server/**/*.dev.js',
isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*', isStandalone ? null : '**/next/dist/compiled/jest-worker/**/*',
...@@ -298,8 +324,11 @@ export async function collectBuildTraces({ ...@@ -298,8 +324,11 @@ export async function collectBuildTraces({
'**/*.d.ts', '**/*.d.ts',
'**/*.map', '**/*.map',
'**/next/dist/pages/**/*', '**/next/dist/pages/**/*',
...(ciEnvironment.hasNextSupport ? ['**/node_modules/sharp/**/*'] : []), ...(ciEnvironment.hasNextSupport
? ['**/node_modules/sharp/**/*', '**/@img/sharp-libvips*/**/*']
: []),
].filter(nonNullable) ].filter(nonNullable)
const serverIgnoreFn = makeIgnoreFn(serverIgnores)
const minimalServerIgnores = [ const minimalServerIgnores = [
...serverIgnores, ...serverIgnores,
...@@ -307,6 +336,7 @@ export async function collectBuildTraces({ ...@@ -307,6 +336,7 @@ export async function collectBuildTraces({
'**/next/dist/server/web/sandbox/**/*', '**/next/dist/server/web/sandbox/**/*',
'**/next/dist/server/post-process.js', '**/next/dist/server/post-process.js',
] ]
const minimalServerIgnoreFn = makeIgnoreFn(minimalServerIgnores)
const routesIgnores = [ const routesIgnores = [
...sharedIgnores, ...sharedIgnores,
...@@ -318,16 +348,8 @@ export async function collectBuildTraces({ ...@@ -318,16 +348,8 @@ export async function collectBuildTraces({
'**/next/dist/server/post-process.js', '**/next/dist/server/post-process.js',
].filter(nonNullable) ].filter(nonNullable)
const makeIgnoreFn = (ignores: string[]) => (pathname: string) => { const routeIgnoreFn = makeIgnoreFn(routesIgnores)
if (path.isAbsolute(pathname) && !pathname.startsWith(root)) {
return true
}
return isMatch(pathname, ignores, {
contains: true,
dot: true,
})
}
const traceContext = path.join(nextServerEntry, '..', '..') const traceContext = path.join(nextServerEntry, '..', '..')
const serverTracedFiles = new Set<string>() const serverTracedFiles = new Set<string>()
const minimalServerTracedFiles = new Set<string>() const minimalServerTracedFiles = new Set<string>()
...@@ -379,10 +401,10 @@ export async function collectBuildTraces({ ...@@ -379,10 +401,10 @@ export async function collectBuildTraces({
] as [Set<string>, string[]][]) { ] as [Set<string>, string[]][]) {
for (const file of files) { for (const file of files) {
if ( if (
!makeIgnoreFn( !(
set === minimalServerTracedFiles set === minimalServerTracedFiles
? minimalServerIgnores ? minimalServerIgnoreFn
: serverIgnores : serverIgnoreFn
)(path.join(traceContext, file)) )(path.join(traceContext, file))
) { ) {
addToTracedFiles(traceContext, file, set) addToTracedFiles(traceContext, file, set)
...@@ -465,11 +487,9 @@ export async function collectBuildTraces({ ...@@ -465,11 +487,9 @@ export async function collectBuildTraces({
if ( if (
!shouldIgnore( !shouldIgnore(
curFile, curFile,
makeIgnoreFn( tracedFiles === minimalServerTracedFiles
tracedFiles === minimalServerTracedFiles ? minimalServerIgnoreFn
? minimalServerIgnores : serverIgnoreFn,
: serverIgnores
),
reasons, reasons,
tracedFiles === minimalServerTracedFiles tracedFiles === minimalServerTracedFiles
? cachedLookupIgnoreMinimal ? cachedLookupIgnoreMinimal
...@@ -529,7 +549,7 @@ export async function collectBuildTraces({ ...@@ -529,7 +549,7 @@ export async function collectBuildTraces({
if ( if (
!shouldIgnore( !shouldIgnore(
curFile, curFile,
makeIgnoreFn(routesIgnores), routeIgnoreFn,
reasons, reasons,
cachedLookupIgnoreRoutes cachedLookupIgnoreRoutes
) )
...@@ -574,7 +594,7 @@ export async function collectBuildTraces({ ...@@ -574,7 +594,7 @@ export async function collectBuildTraces({
for (const item of await fs.readdir(contextDir)) { for (const item of await fs.readdir(contextDir)) {
const itemPath = path.relative(root, path.join(contextDir, item)) const itemPath = path.relative(root, path.join(contextDir, item))
if (!makeIgnoreFn(serverIgnores)(itemPath)) { if (!serverIgnoreFn(itemPath)) {
addToTracedFiles(root, itemPath, serverTracedFiles) addToTracedFiles(root, itemPath, serverTracedFiles)
addToTracedFiles(root, itemPath, minimalServerTracedFiles) addToTracedFiles(root, itemPath, minimalServerTracedFiles)
} }
......
export default function Page() {
return <p>hello world</p>
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'sharp support with hasNextSupport',
{
files: __dirname,
dependencies: {
sharp: 'latest',
},
env: {
NOW_BUILDER: '1',
},
},
({ next }) => {
// we're mainly checking if build/start were successful so
// we have a basic assertion here
it('should work using cheerio', async () => {
const $ = await next.render$('/')
expect($('p').text()).toBe('hello world')
})
}
)
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