-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Add additional handling for experimental tracing #67785
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
packages/next/src/build/flying-shuttle/detect-changed-entries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import crypto from 'crypto' | ||
import { getPageFromPath } from '../entries' | ||
import { Sema } from 'next/dist/compiled/async-sema' | ||
|
||
export interface DetectedEntriesResult { | ||
app: string[] | ||
pages: string[] | ||
} | ||
|
||
let _hasShuttle: undefined | boolean = undefined | ||
export async function hasShuttle(shuttleDir: string) { | ||
if (typeof _hasShuttle === 'boolean') { | ||
return _hasShuttle | ||
} | ||
_hasShuttle = await fs.promises | ||
.access(path.join(shuttleDir, 'server')) | ||
.then(() => true) | ||
.catch(() => false) | ||
|
||
return _hasShuttle | ||
} | ||
|
||
export async function detectChangedEntries({ | ||
appPaths, | ||
pagesPaths, | ||
pageExtensions, | ||
distDir, | ||
shuttleDir, | ||
}: { | ||
appPaths?: string[] | ||
pagesPaths?: string[] | ||
pageExtensions: string[] | ||
distDir: string | ||
shuttleDir: string | ||
}): Promise<{ | ||
changed: DetectedEntriesResult | ||
unchanged: DetectedEntriesResult | ||
}> { | ||
const changedEntries: { | ||
app: string[] | ||
pages: string[] | ||
} = { | ||
app: [], | ||
pages: [], | ||
} | ||
const unchangedEntries: typeof changedEntries = { | ||
app: [], | ||
pages: [], | ||
} | ||
|
||
if (!(await hasShuttle(shuttleDir))) { | ||
// no shuttle so consider everything changed | ||
console.log(`no shuttle. can't detect changes`) | ||
return { | ||
changed: { | ||
pages: pagesPaths || [], | ||
app: appPaths || [], | ||
}, | ||
unchanged: { | ||
pages: [], | ||
app: [], | ||
}, | ||
} | ||
} | ||
|
||
const hashCache = new Map<string, string>() | ||
|
||
async function computeHash(p: string): Promise<string> { | ||
let hash = hashCache.get(p) | ||
if (hash) { | ||
return hash | ||
} | ||
return new Promise((resolve, reject) => { | ||
const hashInst = crypto.createHash('sha1') | ||
const stream = fs.createReadStream(p) | ||
stream.on('error', (err) => reject(err)) | ||
stream.on('data', (chunk) => hashInst.update(chunk)) | ||
stream.on('end', () => { | ||
const digest = hashInst.digest('hex') | ||
resolve(digest) | ||
hashCache.set(p, digest) | ||
}) | ||
}) | ||
} | ||
|
||
const hashSema = new Sema(16) | ||
let globalEntryChanged = false | ||
|
||
async function detectChange({ | ||
normalizedEntry, | ||
entry, | ||
type, | ||
}: { | ||
entry: string | ||
normalizedEntry: string | ||
type: keyof typeof changedEntries | ||
}) { | ||
const traceFile = path.join( | ||
shuttleDir, | ||
'server', | ||
type, | ||
`${normalizedEntry}.js.nft.json` | ||
) | ||
let changed = true | ||
|
||
// we don't need to check any further entry's dependencies if | ||
// a global entry changed since that invalidates everything | ||
if (!globalEntryChanged) { | ||
try { | ||
const traceData: { | ||
fileHashes: Record<string, string> | ||
} = JSON.parse(await fs.promises.readFile(traceFile, 'utf8')) | ||
|
||
if (traceData) { | ||
let changedDependency = false | ||
await Promise.all( | ||
Object.keys(traceData.fileHashes).map(async (file) => { | ||
try { | ||
if (changedDependency) return | ||
await hashSema.acquire() | ||
const relativeTraceFile = path.relative( | ||
path.join(shuttleDir, 'server', type), | ||
traceFile | ||
) | ||
const originalTraceFile = path.join( | ||
distDir, | ||
'server', | ||
type, | ||
relativeTraceFile | ||
) | ||
const absoluteFile = path.join( | ||
path.dirname(originalTraceFile), | ||
file | ||
) | ||
|
||
if (absoluteFile.startsWith(distDir)) { | ||
return | ||
} | ||
|
||
const prevHash = traceData.fileHashes[file] | ||
const curHash = await computeHash(absoluteFile) | ||
|
||
if (prevHash !== curHash) { | ||
console.log('detected change on', { | ||
prevHash, | ||
curHash, | ||
file, | ||
entry: normalizedEntry, | ||
}) | ||
changedDependency = true | ||
} | ||
} finally { | ||
hashSema.release() | ||
} | ||
}) | ||
) | ||
|
||
if (!changedDependency) { | ||
changed = false | ||
} | ||
} else { | ||
console.error('missing trace data', traceFile, normalizedEntry) | ||
} | ||
} catch (err) { | ||
console.error(`Failed to detect change for ${entry}`, err) | ||
} | ||
} | ||
|
||
// we always rebuild global entries so we have a version | ||
// that matches the newest build/runtime | ||
const isGlobalEntry = /(_app|_document|_error)/.test(entry) | ||
|
||
if (changed || isGlobalEntry) { | ||
// if a global entry changed all entries are changed | ||
if (!globalEntryChanged && isGlobalEntry) { | ||
console.log(`global entry ${entry} changed invalidating all entries`) | ||
globalEntryChanged = true | ||
// move unchanged to changed | ||
changedEntries[type].push(...unchangedEntries[type]) | ||
} | ||
changedEntries[type].push(entry) | ||
} else { | ||
unchangedEntries[type].push(entry) | ||
} | ||
} | ||
|
||
// loop over entries and their dependency's hashes | ||
// to detect which changed | ||
for (const entry of pagesPaths || []) { | ||
let normalizedEntry = getPageFromPath(entry, pageExtensions) | ||
|
||
if (normalizedEntry === '/') { | ||
normalizedEntry = '/index' | ||
} | ||
await detectChange({ entry, normalizedEntry, type: 'pages' }) | ||
} | ||
|
||
for (const entry of appPaths || []) { | ||
const normalizedEntry = getPageFromPath(entry, pageExtensions) | ||
await detectChange({ entry, normalizedEntry, type: 'app' }) | ||
} | ||
|
||
console.log( | ||
'changed entries', | ||
JSON.stringify( | ||
{ | ||
changedEntries, | ||
unchangedEntries, | ||
}, | ||
null, | ||
2 | ||
) | ||
) | ||
|
||
return { | ||
changed: changedEntries, | ||
unchanged: unchangedEntries, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how'd we land on this concurrency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arbitrary we've used 25 concurrency for just file ops in the builder but since we're hashing too did a bit less