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

Ensure experimental trace invalidates correctly #68076

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 44 additions & 1 deletion packages/next/src/build/flying-shuttle/detect-changed-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path'
import crypto from 'crypto'
import { getPageFromPath } from '../entries'
import { Sema } from 'next/dist/compiled/async-sema'
import { getShuttleManifest } from './store-shuttle'
import type { NextConfigComplete } from '../../server/config-shared'

export interface DetectedEntriesResult {
app: string[]
Expand All @@ -15,7 +17,7 @@ export async function hasShuttle(shuttleDir: string) {
return _hasShuttle
}
_hasShuttle = await fs.promises
.access(path.join(shuttleDir, 'server'))
.access(path.join(shuttleDir, 'shuttle-manifest.json'))
.then(() => true)
.catch(() => false)

Expand All @@ -28,12 +30,14 @@ export async function detectChangedEntries({
pageExtensions,
distDir,
shuttleDir,
config,
}: {
appPaths?: string[]
pagesPaths?: string[]
pageExtensions: string[]
distDir: string
shuttleDir: string
config: NextConfigComplete
}): Promise<{
changed: DetectedEntriesResult
unchanged: DetectedEntriesResult
Expand Down Expand Up @@ -64,6 +68,45 @@ export async function detectChangedEntries({
},
}
}
const foundShuttleManifest: ReturnType<typeof getShuttleManifest> =
JSON.parse(
await fs.promises.readFile(
path.join(shuttleDir, 'shuttle-manifest.json'),
'utf8'
)
)
const currentShuttleManifest = getShuttleManifest(config)

if (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.`)
return {
changed: {
pages: pagesPaths || [],
app: appPaths || [],
},
unchanged: {
pages: [],
app: [],
},
}
}

if (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.`)
return {
changed: {
pages: pagesPaths || [],
app: appPaths || [],
},
unchanged: {
pages: [],
app: [],
},
}
}

const hashCache = new Map<string, string>()

Expand Down
43 changes: 42 additions & 1 deletion packages/next/src/build/flying-shuttle/store-shuttle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import fs from 'fs'
import path from 'path'
import crypto from 'crypto'
import { recursiveCopy } from '../../lib/recursive-copy'
import { version as nextVersion } from 'next/package.json'
import type { NextConfigComplete } from '../../server/config-shared'
import {
BUILD_MANIFEST,
APP_BUILD_MANIFEST,
Expand All @@ -8,20 +12,57 @@ import {
PAGES_MANIFEST,
ROUTES_MANIFEST,
} from '../../shared/lib/constants'
import { recursiveCopy } from '../../lib/recursive-copy'

export function getShuttleManifest(config: NextConfigComplete) {
const globalHash = crypto.createHash('sha256')

for (const key of Object.keys(process.env || {})) {
if (key.startsWith('NEXT_PUBLIC_')) {
globalHash.update(`${key}=${process.env[key]}`)
}
}

const omittedConfigKeys = ['headers', 'rewrites', 'redirects']

for (const key of Object.keys(config)) {
if (omittedConfigKeys.includes(key)) {
continue
}
let serializedConfig =
typeof config[key] === 'function'
? config[key].toString()
: JSON.stringify(config[key])

globalHash.update(`${key}=${serializedConfig}`)
}

return {
nextVersion,
// NEXT_PUBLIC_ changes for now and specific next config values
globalHash: globalHash.digest('hex'),
}
}

// we can create a new shuttle with the outputs before env values have
// been inlined, can be done after stitching takes place
export async function storeShuttle({
config,
distDir,
shuttleDir,
}: {
distDir: string
shuttleDir: string
config: NextConfigComplete
}) {
await fs.promises.rm(shuttleDir, { force: true, recursive: true })
await fs.promises.mkdir(shuttleDir, { recursive: true })

const shuttleManifest = getShuttleManifest(config)
await fs.promises.writeFile(
path.join(shuttleDir, 'shuttle-manifest.json'),
JSON.stringify(shuttleManifest)
)

// copy all server entries
await recursiveCopy(
path.join(distDir, 'server'),
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ export default async function build(
pageExtensions: config.pageExtensions,
distDir,
shuttleDir,
config,
})
console.log({ changedPagePathsResult })
pagesPaths = changedPagePathsResult.changed.pages
Expand Down Expand Up @@ -960,6 +961,7 @@ export default async function build(
pageExtensions: config.pageExtensions,
distDir,
shuttleDir,
config,
})
console.log({ changedAppPathsResult })
appPaths = changedAppPathsResult.changed.app
Expand Down Expand Up @@ -2518,6 +2520,7 @@ export default async function build(

console.log('storing shuttle')
await storeShuttle({
config,
distDir,
shuttleDir,
})
Expand Down
Loading