Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with outputFileTracingExcludes and pages/api edge runtime #59157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/next/src/build/collect-build-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {

import path from 'path'
import fs from 'fs/promises'
import type { PageInfo } from './utils'
import {
deserializePageInfos,
type PageInfos,
type SerializedPageInfos,
} from './utils'
import { loadBindings } from './swc'
import { nonNullable } from '../lib/non-nullable'
import * as ciEnvironment from '../telemetry/ci-info'
Expand Down Expand Up @@ -79,7 +83,8 @@ export async function collectBuildTraces({
staticPages: string[]
hasSsrAmpPages: boolean
outputFileTracingRoot: string
pageInfos: [string, PageInfo][]
// pageInfos is serialized when this function runs in a worker.
pageInfos: PageInfos | SerializedPageInfos
nextBuildSpan?: Span
config: NextConfigComplete
buildTraceContext?: BuildTraceContext
Expand Down Expand Up @@ -623,6 +628,9 @@ export async function collectBuildTraces({
}

const { entryNameFilesMap } = buildTraceContext?.chunksTrace || {}
const infos =
pageInfos instanceof Map ? pageInfos : deserializePageInfos(pageInfos)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that this is potentially a worker boundary means the interface should be as small as possible. PageInfos is only used for one check so it’d be even better if we only passed the info needed. I’d just turn this into an array of edge runtime routes. We can construct this parallel data structure when we build page infos. The. I’ve thing is that if you aren’t using edge at all then we pay zero when crossing the worker boundary and when you do we only send the bare minimum info

await Promise.all(
[
...(entryNameFilesMap ? Object.entries(entryNameFilesMap) : new Map()),
Expand All @@ -642,7 +650,7 @@ export async function collectBuildTraces({
}

// edge routes have no trace files
const [, pageInfo] = pageInfos.find((item) => item[0] === route) || []
const pageInfo = infos.get(route)
if (pageInfo?.runtime === 'edge') {
return
}
Expand Down
39 changes: 21 additions & 18 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ import {
copyTracedFiles,
isReservedPage,
isAppBuiltinNotFoundPage,
serializePageInfos,
} from './utils'
import type { PageInfo, AppConfig } from './utils'
import type { PageInfo, PageInfos, AppConfig } from './utils'
import { writeBuildId } from './write-build-id'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import isError from '../lib/is-error'
Expand Down Expand Up @@ -361,8 +362,8 @@ export default async function build(
turboNextBuildRoot = null,
buildMode: 'default' | 'experimental-compile' | 'experimental-generate'
): Promise<void> {
const isCompile = buildMode === 'experimental-compile'
const isGenerate = buildMode === 'experimental-generate'
const isCompileMode = buildMode === 'experimental-compile'
const isGenerateMode = buildMode === 'experimental-generate'

try {
const nextBuildSpan = trace('next-build', undefined, {
Expand Down Expand Up @@ -407,7 +408,7 @@ export default async function build(

let buildId: string = ''

if (isGenerate) {
if (isGenerateMode) {
buildId = await fs.readFile(path.join(distDir, 'BUILD_ID'), 'utf8')
} else {
buildId = await nextBuildSpan
Expand Down Expand Up @@ -497,7 +498,8 @@ export default async function build(
// For app directory, we run type checking after build. That's because
// we dynamically generate types for each layout and page in the app
// directory.
if (!appDir && !isCompile) await startTypeChecking(typeCheckingOptions)
if (!appDir && !isCompileMode)
await startTypeChecking(typeCheckingOptions)

if (appDir && 'exportPathMap' in config) {
Log.error(
Expand Down Expand Up @@ -529,7 +531,7 @@ export default async function build(
expFeatureInfo,
})

if (!isGenerate) {
if (!isGenerateMode) {
buildSpinner = createSpinner('Creating an optimized production build')
}

Expand Down Expand Up @@ -909,7 +911,7 @@ export default async function build(
)
}

if (config.cleanDistDir && !isGenerate) {
if (config.cleanDistDir && !isGenerateMode) {
await recursiveDelete(distDir, /^cache/)
}

Expand Down Expand Up @@ -974,7 +976,7 @@ export default async function build(
? path.relative(distDir, incrementalCacheHandlerPath)
: undefined,

isExperimentalCompile: isCompile,
isExperimentalCompile: isCompileMode,
},
},
appDir: dir,
Expand Down Expand Up @@ -1111,8 +1113,8 @@ export default async function build(
)
}

if (!isGenerate) {
if (isCompile && useBuildWorker) {
if (!isGenerateMode) {
if (isCompileMode && useBuildWorker) {
let durationInSeconds = 0

await webpackBuild(useBuildWorker, ['server']).then((res) => {
Expand All @@ -1131,7 +1133,8 @@ export default async function build(
dir,
config,
distDir,
pageInfos: [],
// Serialize Map as this is sent to the worker.
pageInfos: serializePageInfos(new Map()),
staticPages: [],
hasSsrAmpPages: false,
buildTraceContext,
Expand Down Expand Up @@ -1177,7 +1180,7 @@ export default async function build(
}

// For app directory, we run type checking after build.
if (appDir && !(isCompile || isGenerate)) {
if (appDir && !(isCompileMode || isGenerateMode)) {
await startTypeChecking(typeCheckingOptions)
}

Expand Down Expand Up @@ -1205,7 +1208,7 @@ export default async function build(
const appNormalizedPaths = new Map<string, string>()
const appDynamicParamPaths = new Set<string>()
const appDefaultConfigs = new Map<string, AppConfig>()
const pageInfos = new Map<string, PageInfo>()
const pageInfos: PageInfos = new Map<string, PageInfo>()
const pagesManifest = JSON.parse(
await fs.readFile(pagesManifestPath, 'utf8')
) as PagesManifest
Expand Down Expand Up @@ -1388,7 +1391,7 @@ export default async function build(
hasSsrAmpPages,
hasNonStaticErrorPage,
} = await staticCheckSpan.traceAsyncFn(async () => {
if (isCompile) {
if (isCompileMode) {
return {
customAppGetInitialProps: false,
namedExports: [],
Expand Down Expand Up @@ -1590,7 +1593,7 @@ export default async function build(
? 'edge'
: staticInfo?.runtime

if (!isCompile) {
if (!isCompileMode) {
isServerComponent =
pageType === 'app' &&
staticInfo?.rsc !== RSC_MODULE_TYPES.client
Expand Down Expand Up @@ -1940,12 +1943,12 @@ export default async function build(
)
}

if (!isGenerate && config.outputFileTracing && !buildTracesPromise) {
if (!isGenerateMode && config.outputFileTracing && !buildTracesPromise) {
buildTracesPromise = collectBuildTraces({
dir,
config,
distDir,
pageInfos: Object.entries(pageInfos),
pageInfos,
staticPages: [...staticPages],
nextBuildSpan,
hasSsrAmpPages,
Expand Down Expand Up @@ -2086,7 +2089,7 @@ export default async function build(
// - getStaticProps paths
// - experimental app is enabled
if (
!isCompile &&
!isCompileMode &&
(combinedPages.length > 0 ||
useStaticPages404 ||
useDefaultStatic500 ||
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ export interface PageInfo {
isDynamicAppRoute?: boolean
}

export type PageInfos = Map<string, PageInfo>

export type SerializedPageInfos = [string, PageInfo][]

export function serializePageInfos(input: PageInfos): SerializedPageInfos {
return Array.from(input.entries())
}

export function deserializePageInfos(input: SerializedPageInfos): PageInfos {
return new Map(input)
}

export async function printTreeView(
lists: {
pages: ReadonlyArray<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'Edge runtime pages-api route',
{
files: __dirname,
},
({ next }) => {
it('should work edge runtime', async () => {
const res = await next.fetch('/api/edge')
const text = await res.text()
expect(text).toContain('All Good')
})

it('should work with node runtime', async () => {
const res = await next.fetch('/api/node')
const text = await res.text()
expect(text).toContain('All Good')
})
}
)
10 changes: 10 additions & 0 deletions test/e2e/edge-runtime-pages-api-route/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('next').NextConfig} */
const config = {
experimental: {
outputFileTracingExcludes: {
'*': ['anyvaluewillcauseit'],
},
},
}

module.exports = config
5 changes: 5 additions & 0 deletions test/e2e/edge-runtime-pages-api-route/pages/api/edge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const config = {
runtime: 'edge',
}

export default (_req) => new Response('All Good')
1 change: 1 addition & 0 deletions test/e2e/edge-runtime-pages-api-route/pages/api/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (_req, res) => res.send('All Good')