Skip to content

Commit c9ce1a1

Browse files
ijjkbgub
authored andcommitted
Ensure module contexts are always included in adapter traces (vercel#89508)
1 parent 77d6be0 commit c9ce1a1

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

packages/next/src/build/adapter/build-complete.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -532,50 +532,48 @@ export async function handleBuildComplete({
532532
sharedNodeAssets[path.relative(tracingRoot, setupNodeStubPath)] =
533533
require.resolve('next/dist/build/adapter/setup-node-env.external')
534534

535-
if (bundler !== Bundler.Turbopack) {
536-
const moduleTypes = ['app-page', 'pages'] as const
535+
const moduleTypes = ['app-page', 'pages'] as const
537536

538-
for (const type of moduleTypes) {
539-
const currentDependencies: string[] = []
540-
const modulePath = require.resolve(
541-
`next/dist/server/route-modules/${type}/module.compiled`
542-
)
543-
const contextDir = path.join(
544-
path.dirname(modulePath),
545-
'vendored',
546-
'contexts'
547-
)
537+
for (const type of moduleTypes) {
538+
const currentDependencies: string[] = []
539+
const modulePath = require.resolve(
540+
`next/dist/server/route-modules/${type}/module.compiled`
541+
)
542+
currentDependencies.push(modulePath)
548543

549-
for (const item of await fs.readdir(contextDir)) {
550-
if (item.match(/\.(mjs|cjs|js)$/)) {
551-
currentDependencies.push(path.join(contextDir, item))
552-
}
544+
const contextDir = path.join(
545+
path.dirname(modulePath),
546+
'vendored',
547+
'contexts'
548+
)
549+
550+
for (const item of await fs.readdir(contextDir)) {
551+
if (item.match(/\.(mjs|cjs|js)$/)) {
552+
currentDependencies.push(path.join(contextDir, item))
553553
}
554+
}
554555

555-
const { fileList, esmFileList } = await nodeFileTrace(
556-
currentDependencies,
557-
{
558-
base: tracingRoot,
559-
ignore: sharedIgnoreFn,
560-
}
556+
for (const dependencyPath of currentDependencies) {
557+
const rootRelativeFilePath = path.relative(
558+
tracingRoot,
559+
dependencyPath
561560
)
562-
esmFileList.forEach((item) => fileList.add(item))
563561

564-
for (const rootRelativeFilePath of fileList) {
565-
if (type === 'pages') {
566-
pagesSharedNodeAssets[rootRelativeFilePath] = path.join(
567-
tracingRoot,
568-
rootRelativeFilePath
569-
)
570-
} else {
571-
appPagesSharedNodeAssets[rootRelativeFilePath] = path.join(
572-
tracingRoot,
573-
rootRelativeFilePath
574-
)
575-
}
562+
if (type === 'pages') {
563+
pagesSharedNodeAssets[rootRelativeFilePath] = path.join(
564+
tracingRoot,
565+
rootRelativeFilePath
566+
)
567+
} else {
568+
appPagesSharedNodeAssets[rootRelativeFilePath] = path.join(
569+
tracingRoot,
570+
rootRelativeFilePath
571+
)
576572
}
577573
}
574+
}
578575

576+
if (bundler !== Bundler.Turbopack) {
579577
// These are modules that are necessary for bootstrapping node env
580578
const necessaryNodeDependencies = [
581579
require.resolve('next/dist/server/node-environment'),

test/production/adapter-config/adapter-config.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@ describe('adapter-config', () => {
173173
}
174174
}
175175

176+
// Verify vendored context files are traced in assets for app-page and pages outputs
177+
const appPageOutput = outputs.appPages.find(
178+
(output) =>
179+
output.pathname === '/docs/node-app' && output.runtime === 'nodejs'
180+
)
181+
const pagesOutput = outputs.pages.find(
182+
(output) =>
183+
output.pathname === '/docs/node-pages' && output.runtime === 'nodejs'
184+
)
185+
186+
expect(appPageOutput).toBeDefined()
187+
expect(pagesOutput).toBeDefined()
188+
189+
// Check that vendored context files are included in assets
190+
const appPageAssets = Object.values(appPageOutput!.assets)
191+
const pagesAssets = Object.values(pagesOutput!.assets)
192+
193+
const appPageVendoredContexts = appPageAssets.filter((asset) =>
194+
asset.includes('vendored/contexts/')
195+
)
196+
const pagesVendoredContexts = pagesAssets.filter((asset) =>
197+
asset.includes('vendored/contexts/')
198+
)
199+
200+
// app-page should have vendored context files traced
201+
expect(appPageVendoredContexts.length).toBeGreaterThan(0)
202+
// pages should have vendored context files traced
203+
expect(pagesVendoredContexts.length).toBeGreaterThan(0)
204+
176205
for (const route of edgeOutputs) {
177206
try {
178207
expect(route.id).toBeString()

0 commit comments

Comments
 (0)