Unverified Commit 7498fd4b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Restructure trace experiment manifest (#68131)

This restructures the experimental trace manifest to output the values
instead of just hashes for better debugging experience and removes
public env from the manifest for now.
parent ab8e395d
...@@ -3,7 +3,7 @@ import path from 'path' ...@@ -3,7 +3,7 @@ import path from 'path'
import crypto from 'crypto' import crypto from 'crypto'
import { getPageFromPath } from '../entries' import { getPageFromPath } from '../entries'
import { Sema } from 'next/dist/compiled/async-sema' import { Sema } from 'next/dist/compiled/async-sema'
import { generateShuttleManifest } from './store-shuttle' import { generateShuttleManifest, type ShuttleManifest } from './store-shuttle'
import type { NextConfigComplete } from '../../server/config-shared' import type { NextConfigComplete } from '../../server/config-shared'
export interface DetectedEntriesResult { export interface DetectedEntriesResult {
...@@ -11,6 +11,32 @@ export interface DetectedEntriesResult { ...@@ -11,6 +11,32 @@ export interface DetectedEntriesResult {
pages: string[] pages: string[]
} }
function deepEqual(obj1: any, obj2: any) {
if (obj1 === obj2) return true
if (
typeof obj1 !== 'object' ||
obj1 === null ||
typeof obj2 !== 'object' ||
obj2 === null
) {
return false
}
let keys1 = Object.keys(obj1)
let keys2 = Object.keys(obj2)
if (keys1.length !== keys2.length) return false
for (let key of keys1) {
if (!keys2.includes(key) || !deepEqual(obj1[key], obj2[key])) {
return false
}
}
return true
}
let _hasShuttle: undefined | boolean = undefined let _hasShuttle: undefined | boolean = undefined
export async function hasShuttle( export async function hasShuttle(
config: NextConfigComplete, config: NextConfigComplete,
...@@ -19,44 +45,41 @@ export async function hasShuttle( ...@@ -19,44 +45,41 @@ export async function hasShuttle(
if (typeof _hasShuttle === 'boolean') { if (typeof _hasShuttle === 'boolean') {
return _hasShuttle return _hasShuttle
} }
let foundShuttleManifest: let foundShuttleManifest: ShuttleManifest
| ReturnType<typeof generateShuttleManifest>
| undefined
try { try {
foundShuttleManifest = JSON.parse( foundShuttleManifest = JSON.parse(
await fs.promises await fs.promises.readFile(
.readFile(path.join(shuttleDir, 'shuttle-manifest.json'), 'utf8') path.join(shuttleDir, 'shuttle-manifest.json'),
.catch(() => '{}') 'utf8'
)
) )
_hasShuttle = true _hasShuttle = true
} catch (err: unknown) { } catch (err: unknown) {
_hasShuttle = false _hasShuttle = false
console.log(`Failed to read shuttle manifest`)
return _hasShuttle return _hasShuttle
} }
const currentShuttleManifest = generateShuttleManifest(config) const currentShuttleManifest = JSON.parse(generateShuttleManifest(config))
if ( if (currentShuttleManifest.nextVersion !== foundShuttleManifest.nextVersion) {
currentShuttleManifest.nextVersion !== foundShuttleManifest?.nextVersion
) {
// we don't allow using shuttle from differing Next.js version // we don't allow using shuttle from differing Next.js version
// as it could have internal changes // as it could have internal changes
console.log( console.log(
`shuttle has differing Next.js version ${foundShuttleManifest?.nextVersion} versus current ${currentShuttleManifest.nextVersion}, skipping.` `shuttle has differing Next.js version ${foundShuttleManifest.nextVersion} versus current ${currentShuttleManifest.nextVersion}, skipping.`
) )
_hasShuttle = false _hasShuttle = false
} }
if ( if (!deepEqual(currentShuttleManifest.config, foundShuttleManifest.config)) {
_hasShuttle !== false && _hasShuttle = false
currentShuttleManifest.globalHash !== foundShuttleManifest?.globalHash
) {
// if the global hash was invalidated we bypass the cache to be safe
console.log( console.log(
`shuttle has differing global hash ${foundShuttleManifest?.globalHash} versus current ${currentShuttleManifest.globalHash}, skipping.` `Mismatching shuttle configs`,
currentShuttleManifest.config,
foundShuttleManifest.config
) )
_hasShuttle = false
} }
return _hasShuttle return _hasShuttle
} }
......
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import crypto from 'crypto'
import { recursiveCopy } from '../../lib/recursive-copy' import { recursiveCopy } from '../../lib/recursive-copy'
import { version as nextVersion } from 'next/package.json' import { version as nextVersion } from 'next/package.json'
import type { NextConfigComplete } from '../../server/config-shared' import type { NextConfigComplete } from '../../server/config-shared'
...@@ -12,58 +11,31 @@ import { ...@@ -12,58 +11,31 @@ import {
PAGES_MANIFEST, PAGES_MANIFEST,
ROUTES_MANIFEST, ROUTES_MANIFEST,
} from '../../shared/lib/constants' } from '../../shared/lib/constants'
import { getNextPublicEnvironmentVariables } from '../webpack/plugins/define-env-plugin'
export function generateShuttleManifest(config: NextConfigComplete) { export interface ShuttleManifest {
// NEXT_PUBLIC_ changes for now since they are inlined nextVersion: string
// and specific next config values that can impact the build config: Record<string, any>
const globalHash = crypto.createHash('sha256') }
const nextPublicEnv = getNextPublicEnvironmentVariables()
// sort for deterministic order
const publicEnvKeys = Object.keys(nextPublicEnv).sort()
for (const key of publicEnvKeys) {
globalHash.update(`${key}=${nextPublicEnv[key]}`)
}
// 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]
if (key.includes('.')) {
value = config
const keyParts = key.split('.')
for (let i = 0; i < keyParts.length; i++) {
value = value[keyParts[i]]
}
}
let serializedConfig =
typeof value === 'function' ? value.toString() : JSON.stringify(value)
globalHash.update(`${key}=${serializedConfig}`)
}
return { export function generateShuttleManifest(config: NextConfigComplete) {
return JSON.stringify({
nextVersion, nextVersion,
globalHash: globalHash.digest('hex'), config: {
} env: config.env,
i18n: config.i18n,
basePath: config.basePath,
sassOptions: config.sassOptions,
trailingSlash: config.trailingSlash,
productionBrowserSourceMaps: config.productionBrowserSourceMaps,
experimental: {
ppr: config.experimental.ppr,
reactCompiler: config.experimental.reactCompiler,
serverSourceMaps: config.experimental.serverSourceMaps,
serverMinification: config.experimental.serverMinification,
},
},
} satisfies ShuttleManifest)
} }
// we can create a new shuttle with the outputs before env values have // we can create a new shuttle with the outputs before env values have
...@@ -80,10 +52,9 @@ export async function storeShuttle({ ...@@ -80,10 +52,9 @@ export async function storeShuttle({
await fs.promises.rm(shuttleDir, { force: true, recursive: true }) await fs.promises.rm(shuttleDir, { force: true, recursive: true })
await fs.promises.mkdir(shuttleDir, { recursive: true }) await fs.promises.mkdir(shuttleDir, { recursive: true })
const shuttleManifest = generateShuttleManifest(config)
await fs.promises.writeFile( await fs.promises.writeFile(
path.join(shuttleDir, 'shuttle-manifest.json'), path.join(shuttleDir, 'shuttle-manifest.json'),
JSON.stringify(shuttleManifest) generateShuttleManifest(config)
) )
// copy all server entries // copy all server entries
......
...@@ -23,13 +23,13 @@ import { nextTestSetup, isNextStart } from 'e2e-utils' ...@@ -23,13 +23,13 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
NEXT_PRIVATE_FLYING_SHUTTLE: 'true', NEXT_PRIVATE_FLYING_SHUTTLE: 'true',
}, },
}) })
let initialGlobalHash: string = '' let initialConfig: Record<string, any> = {}
beforeAll(async () => { beforeAll(async () => {
const manifest = await next.readJSON( const manifest = await next.readJSON(
'.next/cache/shuttle/shuttle-manifest.json' '.next/cache/shuttle/shuttle-manifest.json'
) )
initialGlobalHash = manifest.globalHash initialConfig = manifest.config
}) })
async function checkShuttleManifest() { async function checkShuttleManifest() {
...@@ -39,7 +39,7 @@ import { nextTestSetup, isNextStart } from 'e2e-utils' ...@@ -39,7 +39,7 @@ import { nextTestSetup, isNextStart } from 'e2e-utils'
expect(manifest).toEqual({ expect(manifest).toEqual({
nextVersion, nextVersion,
globalHash: initialGlobalHash, config: initialConfig,
}) })
} }
...@@ -93,17 +93,6 @@ import { nextTestSetup, isNextStart } from 'e2e-utils' ...@@ -93,17 +93,6 @@ 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 () => { it('should hard navigate on chunk load failure', async () => {
let blockChunks = false let blockChunks = false
const browser = await next.browser('/dashboard', { const browser = await next.browser('/dashboard', {
......
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