From 17a1d033fe458c0e8884c773d7a784bea3186cc5 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 25 Nov 2026 16:13:58 +0800 Subject: [PATCH] refactor(javascript): share unresolved bundler node construction (#14441) --- .../JavaScriptArtifactGraphBundlers.ts | 74 ++++++------------- .../JavaScriptArtifactGraphContext.ts | 17 ++--- .../JavaScriptArtifactGraphFindings.ts | 7 +- .../javascriptBundlerAdapters.test.ts | 34 ++++++--- 4 files changed, 52 insertions(+), 80 deletions(-) diff --git a/src/application/javascript/JavaScriptArtifactGraphBundlers.ts b/src/application/javascript/JavaScriptArtifactGraphBundlers.ts index be651e133ad..134cc9a1516 100644 --- a/src/application/javascript/JavaScriptArtifactGraphBundlers.ts +++ b/src/application/javascript/JavaScriptArtifactGraphBundlers.ts @@ -5,9 +5,8 @@ import { compositeKey } from "../../domain/unicodeCodePointOrder.js"; import { addAstContainsEdge, artifactLocalIdentity, - chunkLookupKey, + bundlerLookupKey, javascriptAnalysisCoverage, - moduleLookupKey, selfReferenceOmissions, type JavaScriptArtifactGraphContext, type JavaScriptArtifactGraphCoverage, @@ -46,7 +45,7 @@ export const addJavaScriptBundlerNodes = ( }); for (const key of registration.chunk_keys) context.chunkNodes.set( - chunkLookupKey(file.path, registration.runtime, key), + bundlerLookupKey(file.path, registration.runtime, key), chunk, ); records.push({ file, registration, chunk, coverage }); @@ -160,7 +159,7 @@ const addBundlerModuleNodes = ( ], }); context.moduleNodes.set( - moduleLookupKey(file.path, registration.runtime, moduleValue.module_key), + bundlerLookupKey(file.path, registration.runtime, moduleValue.module_key), module, ); addAstContainsEdge(context, { @@ -194,10 +193,10 @@ const addBundlerRuntimeEdges = ( const { file, registration, chunk, coverage } = record; for (const moduleKey of registration.entry_module_keys) { const resolved = context.moduleNodes.get( - moduleLookupKey(file.path, registration.runtime, moduleKey), + bundlerLookupKey(file.path, registration.runtime, moduleKey), ); const target = - resolved ?? unresolvedBundlerModuleNode(context, record, moduleKey); + resolved ?? unresolvedBundlerNode(context, record, "module", moduleKey); context.accumulator.addEdge({ source_node_id: chunk.node_id, target_node_id: target.node_id, @@ -220,14 +219,14 @@ const addBundlerRuntimeEdges = ( }); } for (const chunkKey of registration.async_chunk_keys) { - const lookup = chunkLookupKey(file.path, registration.runtime, chunkKey); + const lookup = bundlerLookupKey(file.path, registration.runtime, chunkKey); const resolved = context.chunkNodes.get(lookup); if (resolved?.node_id === chunk.node_id) { selfAsyncChunks++; continue; } const target = - resolved ?? unresolvedBundlerChunkNode(context, record, chunkKey); + resolved ?? unresolvedBundlerNode(context, record, "chunk", chunkKey); context.accumulator.addEdge({ source_node_id: chunk.node_id, target_node_id: target.node_id, @@ -252,70 +251,39 @@ const addBundlerRuntimeEdges = ( return selfAsyncChunks; }; -const unresolvedBundlerModuleNode = ( +const unresolvedBundlerNode = ( context: JavaScriptArtifactGraphContext, record: BundlerGraphRecord, - moduleKey: string, -): ApplicationNode => - context.accumulator.addNode({ - kind: "javascript-module", + kind: "module" | "chunk", + key: string, +): ApplicationNode => { + const reference = kind === "module" ? "entry-module" : "async-chunk"; + return context.accumulator.addNode({ + kind: `javascript-${kind}`, identity: artifactLocalIdentity( record.file.sha256, - `${record.registration.runtime}:unresolved-entry-module`, - moduleKey, + `${record.registration.runtime}:unresolved-${reference}`, + key, ), observations: [ { - label: moduleKey, + label: key, properties: { - semantic_role: "bundler-module-reference", + semantic_role: `bundler-${kind}-reference`, bundler: record.registration.bundler, runtime: record.registration.runtime, - module_key: moduleKey, + [`${kind}_key`]: key, resolution_status: "not-found", }, evidence: staticInferenceEvidence({ sha256: record.file.sha256, path: record.file.path, range: record.registration.location, - operation: "retain-unresolved-bundler-entry-module", - coverage: record.coverage, - confidence: "low", - }), - }, - ], - }); - -const unresolvedBundlerChunkNode = ( - context: JavaScriptArtifactGraphContext, - record: BundlerGraphRecord, - chunkKey: string, -): ApplicationNode => - context.accumulator.addNode({ - kind: "javascript-chunk", - identity: artifactLocalIdentity( - record.file.sha256, - `${record.registration.runtime}:unresolved-async-chunk`, - chunkKey, - ), - observations: [ - { - label: chunkKey, - properties: { - semantic_role: "bundler-chunk-reference", - bundler: record.registration.bundler, - runtime: record.registration.runtime, - chunk_key: chunkKey, - resolution_status: "not-found", - }, - evidence: staticInferenceEvidence({ - sha256: record.file.sha256, - path: record.file.path, - range: record.registration.location, - operation: "retain-unresolved-bundler-async-chunk", + operation: `retain-unresolved-bundler-${reference}`, coverage: record.coverage, confidence: "low", }), }, ], }); +}; diff --git a/src/application/javascript/JavaScriptArtifactGraphContext.ts b/src/application/javascript/JavaScriptArtifactGraphContext.ts index 7f81095e984..8fdec05bc5d 100644 --- a/src/application/javascript/JavaScriptArtifactGraphContext.ts +++ b/src/application/javascript/JavaScriptArtifactGraphContext.ts @@ -522,19 +522,12 @@ export const selfReferenceOmissions = ( `${String(count)} ${count === 1 ? noun[0] : noun[1]} resolved back to ${owner} itself and ${count === 1 ? "was" : "were"} omitted; application graph edges cannot be self-referential.`, ]; -/** Deterministic key for one recovered module inside a bundle asset. */ -export const moduleLookupKey = ( +/** Key for a module or chunk inside one asset and bundler runtime. */ +export const bundlerLookupKey = ( path: string, runtime: string, - moduleKey: string, -): string => compositeKey([path, runtime, moduleKey]); - -/** Deterministic key for one recovered bundler chunk inside a bundle asset. */ -export const chunkLookupKey = ( - path: string, - runtime: string, - chunkKey: string, -): string => compositeKey([path, runtime, chunkKey]); + key: string, +): string => compositeKey([path, runtime, key]); /** Locate the recovered module that owns a static finding. */ export const sourceNodeFor = ( @@ -547,4 +540,4 @@ export const sourceNodeFor = ( ? context.sourceModuleNodes.get(path) : runtime === undefined ? undefined - : context.moduleNodes.get(moduleLookupKey(path, runtime, moduleKey)); + : context.moduleNodes.get(bundlerLookupKey(path, runtime, moduleKey)); diff --git a/src/application/javascript/JavaScriptArtifactGraphFindings.ts b/src/application/javascript/JavaScriptArtifactGraphFindings.ts index 3fb1e0d9d64..3e57c75e48f 100644 --- a/src/application/javascript/JavaScriptArtifactGraphFindings.ts +++ b/src/application/javascript/JavaScriptArtifactGraphFindings.ts @@ -13,11 +13,10 @@ import { applicationContainerIdentity, addApplicationRootCandidate, artifactLocalIdentity, - chunkLookupKey, + bundlerLookupKey, createElectronRoleNode, javascriptAnalysisCoverage, linkElectronRoleToAsset, - moduleLookupKey, selfReferenceOmissions, sourceNodeFor, type JavaScriptArtifactGraphContext, @@ -774,7 +773,7 @@ const resolveReference = ( reference.loader === "commonjs-loader" ? undefined : context.moduleNodes.get( - moduleLookupKey(sourcePath, reference.module_runtime, specifier), + bundlerLookupKey(sourcePath, reference.module_runtime, specifier), ); if (module !== undefined) { const file = context.filesByPath.get(sourcePath); @@ -979,7 +978,7 @@ const resolveBundlerChunkReference = ( if (registrations === undefined) return null; const candidates = registrations.flatMap(({ runtime }) => { const node = context.chunkNodes.get( - chunkLookupKey(sourcePath, runtime, key), + bundlerLookupKey(sourcePath, runtime, key), ); return node === undefined ? [] : [node]; }); diff --git a/tests/boundary/filesystem/javascript-applications/javascriptBundlerAdapters.test.ts b/tests/boundary/filesystem/javascript-applications/javascriptBundlerAdapters.test.ts index 4fb5a2f563a..248e5882443 100644 --- a/tests/boundary/filesystem/javascript-applications/javascriptBundlerAdapters.test.ts +++ b/tests/boundary/filesystem/javascript-applications/javascriptBundlerAdapters.test.ts @@ -43,7 +43,7 @@ describe("Webpack and Rspack runtime adapters", () => { bundler: "webpack", runtime_require_name: "__webpack_require__", runtime_module_cache_status: "not-observed", - entry_module_keys: ["10"], + entry_module_keys: ["10", "missing"], async_chunk_keys: ["lazy", "missing"], unknown_async_chunk_keys: 1, }); @@ -106,16 +106,27 @@ describe("Webpack and Rspack runtime adapters", () => { }), }); - expect( - graph.nodes.find((node) => - node.observations.some( - ({ properties }) => - properties.semantic_role === "bundler-chunk-reference" && - properties.chunk_key === "missing" && - properties.resolution_status === "not-found", - ), - ), - ).toBeDefined(); + for (const [kind, relation] of [ + ["module", "loads"], + ["chunk", "imports"], + ] as const) { + const missing = graph.nodes.find( + (node) => + node.kind === `javascript-${kind}` && + node.observations.some( + ({ properties }) => + properties.semantic_role === `bundler-${kind}-reference` && + properties[`${kind}_key`] === "missing" && + properties.resolution_status === "not-found", + ), + ); + expect( + findGraphEdge(graph, webpackEntry, missing, relation), + ).toMatchObject({ + evidence: { confidence: "low" }, + properties: { resolution_status: "not-found" }, + }); + } expect( graph.edges.some( ({ relation, properties }) => @@ -195,6 +206,7 @@ const bundlerFixture = async (): Promise => { }, (__webpack_require__) => { __webpack_require__.O(0, ["main"], () => __webpack_require__(10)); + __webpack_require__("missing"); } ]); (globalThis["webpackChunkcustomPortal"] = globalThis["webpackChunkcustomPortal"] || []).push([