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

Explain bundle source inclusion

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent e32086b0
import type { AnalyzeData } from '../../shared/lib/analyze-data' import type { AnalyzeData, ModulesData } from '../../shared/lib/analyze-data'
import type { SnapshotMetadata } from './snapshot' import type { SnapshotMetadata } from './snapshot'
import type { AnalyzeRepository } from './repository' import type { AnalyzeRepository } from './repository'
import z from 'next/dist/compiled/zod' import z from 'next/dist/compiled/zod'
...@@ -6,8 +6,12 @@ import z from 'next/dist/compiled/zod' ...@@ -6,8 +6,12 @@ import z from 'next/dist/compiled/zod'
const DEFAULT_LIMIT = 50 const DEFAULT_LIMIT = 50
const MAX_LIMIT = 100 const MAX_LIMIT = 100
const MAX_CHUNKS = 100 const MAX_CHUNKS = 100
const MAX_MODULE_CANDIDATES = 100
const MAX_GRAPH_NODES = 10_000
const COMPRESSED_CAVEAT = const COMPRESSED_CAVEAT =
'Compressed source sizes are estimates because attributed parts are compressed independently.' 'Compressed source sizes are estimates because attributed parts are compressed independently.'
const ENTRY_HEURISTIC_CAVEAT =
'Route entries are detected heuristically from internal Next.js module identifiers.'
type Environment = 'total' | 'client' | 'server' type Environment = 'total' | 'client' | 'server'
type Metric = 'raw' | 'compressed' type Metric = 'raw' | 'compressed'
...@@ -48,6 +52,15 @@ interface SourcesArgs { ...@@ -48,6 +52,15 @@ interface SourcesArgs {
limit?: number limit?: number
} }
interface ExplainArgs {
route: string
sourcePath: string
snapshot?: string
moduleIdent?: string
environment?: Environment
maxDepth?: number
}
export class AnalyzeQueryError extends Error { export class AnalyzeQueryError extends Error {
constructor(readonly output: Record<string, unknown>) { constructor(readonly output: Record<string, unknown>) {
super( super(
...@@ -64,7 +77,11 @@ function publicError(error: unknown): Record<string, unknown> { ...@@ -64,7 +77,11 @@ function publicError(error: unknown): Record<string, unknown> {
} }
if (error.message.startsWith('Unknown route')) if (error.message.startsWith('Unknown route'))
return { error: 'Unknown route' } return { error: 'Unknown route' }
if (/^Invalid /.test(error.message)) return { error: error.message } if (error.message.startsWith('Unknown source'))
return { error: 'Unknown source' }
if (/^(?:Invalid |moduleIdent )/.test(error.message)) {
return { error: error.message }
}
return { error: 'Unable to query analyzer data' } return { error: 'Unable to query analyzer data' }
} }
...@@ -259,6 +276,170 @@ function sortBySize(rows: SourceRow[], metric: Metric): SourceRow[] { ...@@ -259,6 +276,170 @@ function sortBySize(rows: SourceRow[], metric: Metric): SourceRow[] {
) )
} }
const POTENTIAL_ENTRY_DEPENDENTS = [
'next-app-loader',
'next-edge-app-route-loader',
'next-edge-ssr-loader',
'next-route-loader',
'app-page',
'pages-page',
]
const CLIENT_ENTRIES = ['app-pages-internals', 'pages-dir-browser']
function activeEntries(modules: ModulesData, data: AnalyzeData): Set<number> {
const result = new Set<number>()
for (let index = 0; index < modules.moduleCount(); index++) {
const module = modules.module(index)
if (!module) continue
if (
POTENTIAL_ENTRY_DEPENDENTS.some((entry) => module.ident.includes(entry))
) {
let matchedRouteSource = false
for (const dependency of modules.moduleDependencies(index)) {
const dependencyModule = modules.module(dependency)
if (
dependencyModule &&
!dependencyModule.path.includes('next/dist/') &&
data.getSourceIndexFromPath(dependencyModule.path) !== undefined
) {
matchedRouteSource = true
result.add(dependency)
}
}
if (matchedRouteSource) result.add(index)
}
if (
CLIENT_ENTRIES.some((entry) => module.ident.includes(entry)) &&
data.getSourceIndexFromPath(module.path) !== undefined
) {
result.add(index)
}
}
return result
}
function routeModules(
modules: ModulesData,
entries: Set<number>
): { modules: Set<number>; truncated: boolean } {
const reachable = new Set(entries)
const queue = [...entries]
while (queue.length) {
const current = queue.shift()!
const dependencies = [
...modules.moduleDependencies(current),
...modules.asyncModuleDependencies(current),
...modules.tracedModuleDependencies(current),
]
for (const dependency of dependencies) {
if (reachable.has(dependency)) continue
if (reachable.size >= MAX_GRAPH_NODES) {
return { modules: reachable, truncated: true }
}
reachable.add(dependency)
queue.push(dependency)
}
}
return { modules: reachable, truncated: false }
}
function moduleMatchesEnvironment(
ident: string,
environment: Environment
): boolean {
if (environment === 'total') return true
if (environment === 'client') {
return !['ssr', 'rsc', 'route', 'api'].some((layer) =>
ident.includes(layer)
)
}
return !ident.includes('client')
}
function moduleIdentity(modules: ModulesData, index: number) {
const module = modules.module(index)
return module ? { ident: module.ident, path: module.path } : undefined
}
function findImporterChain(
modules: ModulesData,
start: number,
entries: Set<number>,
reachable: Set<number>,
maxDepth: number,
graphTruncated: boolean
): {
chain: Array<{
module: { ident: string; path: string }
edgeKind?: 'sync' | 'async' | 'traced'
}>
truncated: boolean
} {
type Node = {
index: number
chain: Array<{ index: number; edgeKind?: 'sync' | 'async' | 'traced' }>
}
const queue: Node[] = [{ index: start, chain: [{ index: start }] }]
const visited = new Set([start])
let truncated = graphTruncated
while (queue.length) {
const current = queue.shift()!
if (entries.has(current.index)) {
return {
chain: current.chain.map((item, index) => ({
module: moduleIdentity(modules, item.index)!,
...(index > 0 ? { edgeKind: item.edgeKind } : {}),
})),
truncated: false,
}
}
if (current.chain.length - 1 >= maxDepth) {
truncated = true
continue
}
const edges: Array<[number, 'sync' | 'async' | 'traced']> = [
...modules
.moduleDependents(current.index)
.map((index) => [index, 'sync'] as [number, 'sync']),
...modules
.asyncModuleDependents(current.index)
.map((index) => [index, 'async'] as [number, 'async']),
...modules
.tracedModuleDependents(current.index)
.map((index) => [index, 'traced'] as [number, 'traced']),
]
edges.sort((a, b) => {
const moduleA = modules.module(a[0])
const moduleB = modules.module(b[0])
return (
compareText(moduleA?.ident ?? '', moduleB?.ident ?? '') ||
compareText(moduleA?.path ?? '', moduleB?.path ?? '') ||
compareText(a[1], b[1]) ||
a[0] - b[0]
)
})
for (const [index, edgeKind] of edges) {
if (
visited.has(index) ||
!reachable.has(index) ||
!modules.module(index)
) {
continue
}
if (visited.size >= MAX_GRAPH_NODES) {
truncated = true
continue
}
visited.add(index)
queue.push({
index,
chain: [...current.chain, { index, edgeKind }],
})
}
}
return { chain: [], truncated }
}
function provenance(metadata: SnapshotMetadata) { function provenance(metadata: SnapshotMetadata) {
return { return {
id: metadata.id, id: metadata.id,
...@@ -383,5 +564,88 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) { ...@@ -383,5 +564,88 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
}) })
) )
registerQuery(
'explain_bundle_source',
{
route: z.string().max(4096),
sourcePath: z.string().max(4096),
snapshot: snapshotSchema,
moduleIdent: z.string().max(4096).optional(),
environment: environmentSchema,
maxDepth: z.number().int().min(1).max(100).optional(),
},
safeQuery(async (args: ExplainArgs) => {
const environment = args.environment ?? 'total'
const data = await repository.loadRoute(args.snapshot, args.route)
const sourceIndex = data.getSourceIndexFromPath(args.sourcePath)
if (sourceIndex === undefined)
throw new Error(`Unknown source: ${args.sourcePath}`)
const modules = await repository.loadModules(args.snapshot)
const candidates = modules
.getModuleIndiciesFromPath(args.sourcePath)
.filter((index) => {
const module = modules.module(index)
return module && moduleMatchesEnvironment(module.ident, environment)
})
.map((index) => ({ index, module: moduleIdentity(modules, index)! }))
.sort(
(a, b) =>
compareText(a.module.ident, b.module.ident) ||
compareText(a.module.path, b.module.path)
)
const publicCandidates = candidates
.slice(0, MAX_MODULE_CANDIDATES)
.map(({ module }) => module)
const selected = args.moduleIdent
? candidates.find(({ module }) => module.ident === args.moduleIdent)
: candidates.length === 1
? candidates[0]
: undefined
if (args.moduleIdent && !selected) {
throw new Error(
'moduleIdent does not match a module candidate for this source'
)
}
const sizes = sourceSizes(data, sourceIndex, environment)
const flags = data.getSourceFlags(sourceIndex)
const chunks = data.sourceChunks(sourceIndex)
const entries = activeEntries(modules, data)
const routeGraph = routeModules(modules, entries)
return {
route: args.route,
snapshot: provenance(
(await repository.getSnapshot(args.snapshot)).metadata
),
sourcePath: args.sourcePath,
...sizes,
flags: {
client: flags.client,
server: flags.server,
traced: flags.traced,
},
chunks: chunks.slice(0, MAX_CHUNKS),
chunkCount: chunks.length,
chunksTruncated: chunks.length > MAX_CHUNKS,
moduleCandidates: publicCandidates,
moduleCandidateCount: candidates.length,
moduleCandidatesTruncated: candidates.length > MAX_MODULE_CANDIDATES,
ambiguous: candidates.length > 1 && !args.moduleIdent,
selectedModule: selected?.module,
importerChain: selected
? findImporterChain(
modules,
selected.index,
entries,
routeGraph.modules,
args.maxDepth ?? 25,
routeGraph.truncated
)
: undefined,
routeEntryDetection: { heuristic: true },
caveats: [COMPRESSED_CAVEAT, ENTRY_HEURISTIC_CAVEAT],
}
})
)
return new AnalyzeQueryRegistry(queries) return new AnalyzeQueryRegistry(queries)
} }
...@@ -121,10 +121,29 @@ describe('next experimental-analyze', () => { ...@@ -121,10 +121,29 @@ describe('next experimental-analyze', () => {
'{"route":"/","limit":1}', '{"route":"/","limit":1}',
]) ])
expect(sources.exitCode).toBe(0) expect(sources.exitCode).toBe(0)
expect(JSON.parse(sources.stdout)).toMatchObject({ const sourceResult = JSON.parse(sources.stdout)
expect(sourceResult).toMatchObject({
route: '/', route: '/',
sources: [{ sourcePath: expect.any(String) }], sources: [{ sourcePath: expect.any(String) }],
}) })
const explanation = await next.runCommand([
'experimental-analyze',
'--query',
'explain_bundle_source',
'--input',
JSON.stringify({
route: '/',
sourcePath: sourceResult.sources[0].sourcePath,
maxDepth: 2,
}),
])
expect(explanation.exitCode).toBe(0)
expect(JSON.parse(explanation.stdout)).toMatchObject({
route: '/',
sourcePath: sourceResult.sources[0].sourcePath,
routeEntryDetection: { heuristic: true },
})
}) })
}) })
}) })
......
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