Unverified Commit 3261044f authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack build: fix deterministic build test (#77618)

## What?

Fixes the test checking deterministic builds. The reason it was failing
is that we incorrectly wrote the build manifest directly as middleware
build manifest but it needs some adjustments. There's a function for
that already, so just using that function matches the behavior we use
with webpack. Easy fix.

Improved the test to show the file contents whenever the test fails.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent a7cad4e1
...@@ -36,10 +36,12 @@ function buildNodejsLowPriorityPath(filename: string, buildId: string) { ...@@ -36,10 +36,12 @@ function buildNodejsLowPriorityPath(filename: string, buildId: string) {
return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}` return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`
} }
function createEdgeRuntimeManifest(originAssetMap: BuildManifest): string { export function createEdgeRuntimeManifest(
originAssetMap: Partial<BuildManifest>
): string {
const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js'] const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js']
const assetMap: BuildManifest = { const assetMap: Partial<BuildManifest> = {
...originAssetMap, ...originAssetMap,
lowPriorityFiles: [], lowPriorityFiles: [],
} }
......
...@@ -40,6 +40,7 @@ import { ...@@ -40,6 +40,7 @@ import {
normalizeRewritesForBuildManifest, normalizeRewritesForBuildManifest,
srcEmptySsgManifest, srcEmptySsgManifest,
processRoute, processRoute,
createEdgeRuntimeManifest,
} from '../../../build/webpack/plugins/build-manifest-plugin' } from '../../../build/webpack/plugins/build-manifest-plugin'
import getAssetPathFromRoute from '../router/utils/get-asset-path-from-route' import getAssetPathFromRoute from '../router/utils/get-asset-path-from-route'
import { getEntryKey, type EntryKey } from './entry-key' import { getEntryKey, type EntryKey } from './entry-key'
...@@ -454,7 +455,7 @@ export class TurbopackManifestLoader { ...@@ -454,7 +455,7 @@ export class TurbopackManifestLoader {
middlewareBuildManifestPath, middlewareBuildManifestPath,
// we use globalThis here because middleware can be node // we use globalThis here because middleware can be node
// which doesn't have "self" // which doesn't have "self"
`globalThis.__BUILD_MANIFEST=${JSON.stringify(buildManifest)};` createEdgeRuntimeManifest(buildManifest)
) )
const interceptionRewrites = JSON.stringify( const interceptionRewrites = JSON.stringify(
......
import crypto from 'crypto' import crypto from 'crypto'
import { NextInstance, nextTestSetup } from 'e2e-utils' import { NextInstance, nextTestSetup } from 'e2e-utils'
function generateMD5(text: string) { type Md5Hash = string & { __brand: 'Md5Hash' }
type Record = [filePath: string, hash: string, content: string]
interface RouteFileHashRecords {
[key: string]: Record[]
}
function generateMD5(text: string): Md5Hash {
const hash = crypto.createHash('md5') const hash = crypto.createHash('md5')
hash.update(text) hash.update(text)
return hash.digest('hex') return hash.digest('hex') as Md5Hash
}
function getNodeFileHashes(next: NextInstance): RouteFileHashRecords {
const nodeBuildFileMd5Hashes: RouteFileHashRecords = {}
for (const file of nodeFilePaths) {
const content = next.readFileSync(`.next/server/${file}.js`)
const md5 = generateMD5(content)
nodeBuildFileMd5Hashes[file] = [[file, md5, content]]
}
return nodeBuildFileMd5Hashes
} }
const nodeFilePaths = [ const nodeFilePaths = [
...@@ -14,22 +33,28 @@ const nodeFilePaths = [ ...@@ -14,22 +33,28 @@ const nodeFilePaths = [
'pages/pages-page', 'pages/pages-page',
] ]
async function getEdgeRouteFilesFromManifest(next: NextInstance) { function getEdgeRouteFileHashes(next: NextInstance): RouteFileHashRecords {
const manifest: any = JSON.parse( const manifest: any = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json') next.readFileSync('.next/server/middleware-manifest.json')
) )
const routeKeys = Object.keys(manifest.functions) const routeKeys = Object.keys(manifest.functions)
const md5Map: Record<string, string[]> = {} const hashMap: RouteFileHashRecords = {}
for (const route of routeKeys) { for (const route of routeKeys) {
const files: string[] = manifest.functions[route].files const files: string[] = manifest.functions[route].files
const filesMd5Promises = files.map(async (filePath: string) => {
const content = await next.readFile(`.next/${filePath}`) const hashes: Record[] = []
return generateMD5(content) for (const filePath of files) {
}) const content = next.readFileSync(`.next/${filePath}`)
const md5s = await Promise.all(filesMd5Promises) hashes.push([filePath, generateMD5(content), content])
md5Map[route] = md5s
} }
return md5Map hashMap[route] = hashes
}
return hashMap
}
interface Runs {
run1: RouteFileHashRecords
run2: RouteFileHashRecords
} }
describe('deterministic build', () => { describe('deterministic build', () => {
...@@ -38,31 +63,30 @@ describe('deterministic build', () => { ...@@ -38,31 +63,30 @@ describe('deterministic build', () => {
skipStart: true, skipStart: true,
}) })
// Edge - { [route]: [file md5s] } const edgeBuildFileMd5Hashes: Runs = {
const edgeBuildFileMd5Hashes: Record<string, string[]>[] = [] run1: {},
// Node - { [route]: page.js or route.js md5 } run2: {},
const nodeBuildFileMd5Hashes: Record<string, string>[] = [{}, {}] }
const nodeBuildFileMd5Hashes: Runs = {
run1: {},
run2: {},
}
beforeAll(async () => { beforeAll(async () => {
// First build // First build
await next.build() await next.build()
edgeBuildFileMd5Hashes.push(await getEdgeRouteFilesFromManifest(next)) edgeBuildFileMd5Hashes.run1 = getEdgeRouteFileHashes(next)
for (const file of nodeFilePaths) { nodeBuildFileMd5Hashes.run1 = getNodeFileHashes(next)
const content = await next.readFile(`.next/server/${file}.js`)
nodeBuildFileMd5Hashes[0][file] = generateMD5(content)
}
// Second build // Second build
await next.build() await next.build()
edgeBuildFileMd5Hashes.push(await getEdgeRouteFilesFromManifest(next)) edgeBuildFileMd5Hashes.run2 = getEdgeRouteFileHashes(next)
for (const file of nodeFilePaths) { nodeBuildFileMd5Hashes.run2 = getNodeFileHashes(next)
const content = await next.readFile(`.next/server/${file}.js`)
nodeBuildFileMd5Hashes[1][file] = generateMD5(content)
}
}) })
it('should have same md5 file across build', async () => { it('should have same md5 file across build', async () => {
expect(edgeBuildFileMd5Hashes[0]).toEqual(edgeBuildFileMd5Hashes[1]) expect(edgeBuildFileMd5Hashes.run1).toEqual(edgeBuildFileMd5Hashes.run2)
expect(nodeBuildFileMd5Hashes[0]).toEqual(nodeBuildFileMd5Hashes[1]) expect(nodeBuildFileMd5Hashes.run1).toEqual(nodeBuildFileMd5Hashes.run2)
}) })
}) })
...@@ -18007,8 +18007,8 @@ ...@@ -18007,8 +18007,8 @@
"runtimeError": false "runtimeError": false
}, },
"test/production/deterministic-build/index.test.ts": { "test/production/deterministic-build/index.test.ts": {
"passed": [], "passed": ["deterministic build should have same md5 file across build"],
"failed": ["deterministic build should have same md5 file across build"], "failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
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