Unverified Commit 6137a764 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix global hash for experimental tracing (#68122)

This ensures we don't over invalidate due to the global hash
invalidating too easily and also adds test cases to ensures it's
consistent across builds.
parent 65bd21d4
......@@ -29,6 +29,7 @@ export async function hasShuttle(
.readFile(path.join(shuttleDir, 'shuttle-manifest.json'), 'utf8')
.catch(() => '{}')
)
_hasShuttle = true
} catch (err: unknown) {
_hasShuttle = false
return _hasShuttle
......@@ -36,12 +37,13 @@ export async function hasShuttle(
const currentShuttleManifest = generateShuttleManifest(config)
if (
_hasShuttle !== false &&
currentShuttleManifest.nextVersion !== foundShuttleManifest?.nextVersion
) {
// we don't allow using shuttle from differing Next.js version
// as it could have internal changes
console.log(`shuttle has differing Next.js version, skipping.`)
console.log(
`shuttle has differing Next.js version ${foundShuttleManifest?.nextVersion} versus current ${currentShuttleManifest.nextVersion}, skipping.`
)
_hasShuttle = false
}
......@@ -50,7 +52,9 @@ export async function hasShuttle(
currentShuttleManifest.globalHash !== foundShuttleManifest?.globalHash
) {
// if the global hash was invalidated we bypass the cache to be safe
console.log(`shuttle has differing global hash, skipping.`)
console.log(
`shuttle has differing global hash ${foundShuttleManifest?.globalHash} versus current ${currentShuttleManifest.globalHash}, skipping.`
)
_hasShuttle = false
}
return _hasShuttle
......@@ -210,7 +214,7 @@ export async function detectChangedEntries({
if (changed || isGlobalEntry) {
// if a global entry changed all entries are changed
if (!globalEntryChanged && isGlobalEntry) {
if (changed && !globalEntryChanged && isGlobalEntry) {
console.log(`global entry ${entry} changed invalidating all entries`)
globalEntryChanged = true
// move unchanged to changed
......@@ -238,18 +242,6 @@ export async function detectChangedEntries({
await detectChange({ entry, normalizedEntry, type: 'app' })
}
console.log(
'changed entries',
JSON.stringify(
{
changedEntries,
unchangedEntries,
},
null,
2
)
)
return {
changed: changedEntries,
unchanged: unchangedEntries,
......
......@@ -19,20 +19,43 @@ export function generateShuttleManifest(config: NextConfigComplete) {
// and specific next config values that can impact the build
const globalHash = crypto.createHash('sha256')
const nextPublicEnv = getNextPublicEnvironmentVariables()
for (const key in nextPublicEnv) {
// sort for deterministic order
const publicEnvKeys = Object.keys(nextPublicEnv).sort()
for (const key of publicEnvKeys) {
globalHash.update(`${key}=${nextPublicEnv[key]}`)
}
const omittedConfigKeys = ['headers', 'rewrites', 'redirects']
// TODO: make this opt-out list instead?
// also ensure this list is complete this is minimal set
const configsToInvalidateOn = [
'basePath',
'env',
'i18n',
'images',
'productionBrowserSourceMaps',
'webpack',
'sassOptions',
'trailingSlash',
'experimental.flyingShuttle',
'experimental.ppr',
'experimental.reactCompiler',
].sort()
for (const key of configsToInvalidateOn) {
let value = config[key]
for (const key in config) {
if (omittedConfigKeys.includes(key)) {
continue
if (key.includes('.')) {
value = config
const keyParts = key.split('.')
for (let i = 0; i < keyParts.length; i++) {
value = value[keyParts[i]]
}
}
let serializedConfig =
typeof config[key] === 'function'
? config[key].toString()
: JSON.stringify(config[key])
typeof value === 'function' ? value.toString() : JSON.stringify(value)
globalHash.update(`${key}=${serializedConfig}`)
}
......
......@@ -872,7 +872,15 @@ export default async function build(
shuttleDir,
config,
})
console.log({ changedPagePathsResult })
console.log(
JSON.stringify(
{
changedPagePathsResult: changedPagePathsResult.changed.pages,
},
null,
2
)
)
pagesPaths = changedPagePathsResult.changed.pages
}
......@@ -964,7 +972,15 @@ export default async function build(
shuttleDir,
config,
})
console.log({ changedAppPathsResult })
console.log(
JSON.stringify(
{
changedAppPathsResult: changedAppPathsResult.changed.app,
},
null,
2
)
)
appPaths = changedAppPathsResult.changed.app
}
......
import fs from 'fs'
import path from 'path'
import { version as nextVersion } from 'next/package.json'
import type { Route } from 'playwright'
import { retry } from 'next-test-utils'
import { nextTestSetup, isNextStart } from 'e2e-utils'
......@@ -22,6 +23,25 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
NEXT_PRIVATE_FLYING_SHUTTLE: 'true',
},
})
let initialGlobalHash: string = ''
beforeAll(async () => {
const manifest = await next.readJSON(
'.next/cache/shuttle/shuttle-manifest.json'
)
initialGlobalHash = manifest.globalHash
})
async function checkShuttleManifest() {
const manifest = await next.readJSON(
'.next/cache/shuttle/shuttle-manifest.json'
)
expect(manifest).toEqual({
nextVersion,
globalHash: initialGlobalHash,
})
}
it('should have file hashes in trace files', async () => {
const deploymentsTracePath = path.join(
......@@ -73,6 +93,17 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
}
})
it('should have shuttle-manifest.json', async () => {
const manifest = await next.readJSON(
'.next/cache/shuttle/shuttle-manifest.json'
)
expect(manifest).toEqual({
nextVersion,
globalHash: expect.stringMatching(/[\w\d]{1,}/),
})
})
it('should hard navigate on chunk load failure', async () => {
let blockChunks = false
const browser = await next.browser('/dashboard', {
......@@ -181,6 +212,11 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
await next.patchFile(dataPath, JSON.stringify({ hello: 'again' }))
await next.start()
expect(next.cliOutput).not.toContain('/catch-all')
expect(next.cliOutput).not.toContain('/blog/[slug]')
expect(next.cliOutput).toContain('/dashboard/deployments/[id]')
await checkShuttleManifest()
await checkAppPagesNavigation()
} finally {
await next.patchFile(dataPath, originalContent)
......@@ -203,6 +239,11 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
)
await next.start()
expect(next.cliOutput).toContain('/')
expect(next.cliOutput).not.toContain('/catch-all')
expect(next.cliOutput).not.toContain('/blog/[slug]')
await checkShuttleManifest()
await checkAppPagesNavigation()
} finally {
await next.patchFile(pagePath, originalContent)
......@@ -228,6 +269,12 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
await next.patchFile(dataPath, JSON.stringify({ hello: 'again' }))
await next.start()
expect(next.cliOutput).toContain('/')
expect(next.cliOutput).toContain('/dashboard/deployments/[id]')
expect(next.cliOutput).not.toContain('/catch-all')
expect(next.cliOutput).not.toContain('/blog/[slug]')
await checkShuttleManifest()
await checkAppPagesNavigation()
} finally {
await next.patchFile(pagePath, originalPageContent)
......
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