Unverified Commit 15af0dca authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Fingerprint analyzer snapshot contents

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent fa0ef04c
...@@ -3,6 +3,12 @@ ...@@ -3,6 +3,12 @@
* `packages/next/src/build/analyze/snapshot.ts`). Field semantics must stay in * `packages/next/src/build/analyze/snapshot.ts`). Field semantics must stay in
* sync. * sync.
*/ */
export interface ContentFingerprint {
algorithm: 'sha256'
version: 1
digest: string
}
export interface SnapshotMetadata { export interface SnapshotMetadata {
id: string id: string
createdAt: string createdAt: string
...@@ -11,6 +17,8 @@ export interface SnapshotMetadata { ...@@ -11,6 +17,8 @@ export interface SnapshotMetadata {
gitSha?: string gitSha?: string
gitShortSha?: string gitShortSha?: string
gitDirty?: boolean gitDirty?: boolean
worktreeFingerprint?: ContentFingerprint
analysisFingerprint?: ContentFingerprint
/** First line of the HEAD commit message when available. */ /** First line of the HEAD commit message when available. */
gitMessage?: string gitMessage?: string
appDirOnly?: boolean appDirOnly?: boolean
......
...@@ -34,6 +34,23 @@ function validateStringArray(value: unknown, label: string): string[] { ...@@ -34,6 +34,23 @@ function validateStringArray(value: unknown, label: string): string[] {
return value return value
} }
function validFingerprint(value: unknown): boolean {
if (value === undefined) return true
const fingerprint = value as {
algorithm?: unknown
version?: unknown
digest?: unknown
}
return (
typeof value === 'object' &&
value !== null &&
fingerprint.algorithm === 'sha256' &&
fingerprint.version === 1 &&
typeof fingerprint.digest === 'string' &&
/^[0-9a-f]{64}$/.test(fingerprint.digest)
)
}
function validateMetadata(value: unknown, label: string): SnapshotMetadata { function validateMetadata(value: unknown, label: string): SnapshotMetadata {
const metadata = value as SnapshotMetadata const metadata = value as SnapshotMetadata
if ( if (
...@@ -44,7 +61,9 @@ function validateMetadata(value: unknown, label: string): SnapshotMetadata { ...@@ -44,7 +61,9 @@ function validateMetadata(value: unknown, label: string): SnapshotMetadata {
typeof metadata.createdAt !== 'string' || typeof metadata.createdAt !== 'string' ||
!Number.isFinite(Date.parse(metadata.createdAt)) || !Number.isFinite(Date.parse(metadata.createdAt)) ||
!Number.isSafeInteger(metadata.routeCount) || !Number.isSafeInteger(metadata.routeCount) ||
metadata.routeCount < 0 metadata.routeCount < 0 ||
!validFingerprint(metadata.worktreeFingerprint) ||
!validFingerprint(metadata.analysisFingerprint)
) { ) {
throw new Error(`Invalid ${label}`) throw new Error(`Invalid ${label}`)
} }
......
import * as path from 'node:path' import * as path from 'node:path'
import { createHash } from 'node:crypto'
import { cp, mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises' import { cp, mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises'
import { import {
...@@ -6,6 +7,7 @@ import { ...@@ -6,6 +7,7 @@ import {
getGitCommit, getGitCommit,
getGitDirty, getGitDirty,
getGitMessage, getGitMessage,
getGitWorktreeFingerprint,
} from '../../lib/helpers/git' } from '../../lib/helpers/git'
/** /**
* Maximum number of historical snapshots to keep on disk by default. When the * Maximum number of historical snapshots to keep on disk by default. When the
...@@ -18,6 +20,12 @@ const MAX_HISTORY = 20 ...@@ -18,6 +20,12 @@ const MAX_HISTORY = 20
* side so the comparison picker can render branch / sha / timestamp labels * side so the comparison picker can render branch / sha / timestamp labels
* without re-fetching. * without re-fetching.
*/ */
export interface ContentFingerprint {
algorithm: 'sha256'
version: 1
digest: string
}
export interface SnapshotMetadata { export interface SnapshotMetadata {
/** Snapshot identifier (also the directory name under `history/`). */ /** Snapshot identifier (also the directory name under `history/`). */
id: string id: string
...@@ -33,6 +41,10 @@ export interface SnapshotMetadata { ...@@ -33,6 +41,10 @@ export interface SnapshotMetadata {
gitShortSha?: string gitShortSha?: string
/** Whether the working tree had uncommitted changes when the build ran. */ /** Whether the working tree had uncommitted changes when the build ran. */
gitDirty?: boolean gitDirty?: boolean
/** Content-sensitive identity for tracked and non-ignored untracked changes. */
worktreeFingerprint?: ContentFingerprint
/** Identity for emitted analyzer artifacts and analysis-affecting options. */
analysisFingerprint?: ContentFingerprint
/** First line of the HEAD commit message when available. */ /** First line of the HEAD commit message when available. */
gitMessage?: string gitMessage?: string
/** `true` when built with `--app-dir-only`. */ /** `true` when built with `--app-dir-only`. */
...@@ -109,6 +121,12 @@ export async function writeAnalyzeSnapshot({ ...@@ -109,6 +121,12 @@ export async function writeAnalyzeSnapshot({
const gitBranch = getGitBranch(projectDir) const gitBranch = getGitBranch(projectDir)
const gitDirty = getGitDirty(projectDir) const gitDirty = getGitDirty(projectDir)
const gitMessage = getGitMessage(projectDir) const gitMessage = getGitMessage(projectDir)
const worktreeDigest = getGitWorktreeFingerprint(projectDir)
const analysisDigest = await fingerprintAnalysisData(dataDir, {
appDirOnly,
noMangling,
nextVersion: process.env.__NEXT_VERSION,
})
const createdAt = new Date() const createdAt = new Date()
const id = makeSnapshotId(createdAt, gitSha) const id = makeSnapshotId(createdAt, gitSha)
...@@ -121,6 +139,12 @@ export async function writeAnalyzeSnapshot({ ...@@ -121,6 +139,12 @@ export async function writeAnalyzeSnapshot({
gitSha, gitSha,
gitShortSha: gitSha ? gitSha.slice(0, 7) : undefined, gitShortSha: gitSha ? gitSha.slice(0, 7) : undefined,
gitDirty, gitDirty,
worktreeFingerprint: worktreeDigest
? { algorithm: 'sha256', version: 1, digest: worktreeDigest }
: undefined,
analysisFingerprint: analysisDigest
? { algorithm: 'sha256', version: 1, digest: analysisDigest }
: undefined,
gitMessage, gitMessage,
appDirOnly, appDirOnly,
noMangling, noMangling,
...@@ -148,6 +172,48 @@ export async function writeAnalyzeSnapshot({ ...@@ -148,6 +172,48 @@ export async function writeAnalyzeSnapshot({
return metadata return metadata
} }
async function fingerprintAnalysisData(
dataDir: string,
options: {
appDirOnly?: boolean
noMangling?: boolean
nextVersion?: string
}
): Promise<string | undefined> {
try {
const files: string[] = []
async function visit(directory: string): Promise<void> {
const entries = await readdir(directory, { withFileTypes: true })
entries.sort((a, b) => a.name.localeCompare(b.name))
for (const entry of entries) {
const filename = path.join(directory, entry.name)
if (entry.isDirectory()) {
await visit(filename)
} else if (
entry.isFile() &&
path.relative(dataDir, filename) !== METADATA_FILENAME
) {
files.push(filename)
}
}
}
await visit(dataDir)
const hash = createHash('sha256')
hash.update('next-analyze-data-v1\0')
hash.update(JSON.stringify(options))
for (const filename of files) {
hash.update('\0')
hash.update(path.relative(dataDir, filename).split(path.sep).join('/'))
hash.update('\0')
hash.update(await readFile(filename))
}
return hash.digest('hex')
} catch {
return undefined
}
}
/** /**
* Build the snapshot id from the timestamp and git sha. The format is * Build the snapshot id from the timestamp and git sha. The format is
* sortable lexicographically (newest last) which keeps directory listings * sortable lexicographically (newest last) which keeps directory listings
......
import { spawnSync } from 'node:child_process' import { spawnSync } from 'node:child_process'
import { createHash } from 'node:crypto'
function gitExec(args: string[], cwd: string): string { function gitExec(args: string[], cwd: string, timeout = 2000): string {
const result = spawnSync('git', args, { const result = spawnSync('git', args, {
cwd, cwd,
timeout: 2000, timeout,
stdio: ['ignore', 'pipe', 'ignore'], stdio: ['ignore', 'pipe', 'ignore'],
encoding: 'utf8', encoding: 'utf8',
}) })
...@@ -62,6 +63,52 @@ export function getGitDirty(cwd: string): boolean | undefined { ...@@ -62,6 +63,52 @@ export function getGitDirty(cwd: string): boolean | undefined {
} }
} }
/**
* Returns a content-sensitive fingerprint for the current working tree.
* Only the final digest is exposed; paths and file hashes remain inside the
* digest input. Non-ignored untracked files are included explicitly because
* `git diff HEAD` does not report them.
*/
export function getGitWorktreeFingerprint(cwd: string): string | undefined {
try {
const status = gitExec(
['status', '--porcelain=v1', '-z', '--untracked-files=all'],
cwd,
30_000
)
const diff = gitExec(
['diff', '--binary', '--no-ext-diff', 'HEAD', '--'],
cwd,
30_000
)
const untracked = gitExec(
['ls-files', '--others', '--exclude-standard', '-z'],
cwd,
30_000
)
.split('\0')
.filter(Boolean)
.sort()
const hash = createHash('sha256')
hash.update('next-worktree-v1\0')
hash.update(status)
hash.update('\0')
hash.update(diff)
for (const filename of untracked) {
hash.update('\0untracked\0')
hash.update(filename)
hash.update('\0')
hash.update(
gitExec(['hash-object', '--no-filters', '--', filename], cwd, 30_000)
)
}
return hash.digest('hex')
} catch {
return undefined
}
}
/** /**
* Returns the first line of the HEAD commit message, or undefined when it * Returns the first line of the HEAD commit message, or undefined when it
* cannot be determined. Prefers VERCEL_GIT_COMMIT_MESSAGE when set. * cannot be determined. Prefers VERCEL_GIT_COMMIT_MESSAGE when set.
......
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