fix(javascript): share source-map resolution and preserve path uncertainty
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
} from "./javascriptApplicationGraph.js";
|
||||
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
|
||||
import type { ApplicationGraphEvidence } from "./javascriptApplicationEvidenceSchemas.js";
|
||||
import { resolveJavaScriptSourceMapReferences } from "./javascriptSourceMapPaths.js";
|
||||
|
||||
export const APPLICATION_GRAPH_DIGESTS = {
|
||||
package: "1".repeat(64),
|
||||
@@ -22,6 +23,20 @@ export const completeCoverage = {
|
||||
limits: [],
|
||||
} satisfies ApplicationGraphEvidence["coverage"];
|
||||
|
||||
export const sourceMapReference = (
|
||||
sourceName: string,
|
||||
sourceRoot: string | null,
|
||||
mapPath: string,
|
||||
) => {
|
||||
const reference = resolveJavaScriptSourceMapReferences(
|
||||
[sourceName],
|
||||
sourceRoot,
|
||||
mapPath,
|
||||
)[0];
|
||||
if (reference == null) throw new TypeError("Expected a source-map reference");
|
||||
return reference;
|
||||
};
|
||||
|
||||
const extractor = (operation: string) => ({
|
||||
name: "rea-test-fixture",
|
||||
version: "1.0.0",
|
||||
|
||||
@@ -20,10 +20,9 @@ import {
|
||||
inferredArtifactEvidence,
|
||||
nodeByLabel,
|
||||
runtimeEvidence,
|
||||
sourceMapReference,
|
||||
unknownEvidence,
|
||||
} from "./javascriptApplicationGraph.fixture.js";
|
||||
|
||||
import { resolveJavaScriptSourceMapReference } from "./javascriptSourceMapPaths.js";
|
||||
const createLargeNativeExportGraph = (): JavaScriptApplicationGraph => {
|
||||
const properties = Object.fromEntries(
|
||||
Array.from({ length: 1_001 }, (_, index) => [
|
||||
@@ -438,7 +437,7 @@ describe("JavaScript Application Graph: source maps and observations", () => {
|
||||
observations: [
|
||||
{
|
||||
label: "src/editor.ts",
|
||||
source_map_reference: resolveJavaScriptSourceMapReference(
|
||||
source_map_reference: sourceMapReference(
|
||||
"webpack:///src/editor.ts",
|
||||
null,
|
||||
"dist/app.js.map",
|
||||
@@ -535,7 +534,7 @@ describe("JavaScript Application Graph: source-map digest consistency", () => {
|
||||
observations: [
|
||||
{
|
||||
label: null,
|
||||
source_map_reference: resolveJavaScriptSourceMapReference(
|
||||
source_map_reference: sourceMapReference(
|
||||
"src/editor.ts",
|
||||
null,
|
||||
"dist/app.js.map",
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { posix } from "node:path";
|
||||
import { domainToASCII, fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { TraceMap } from "@jridgewell/trace-mapping";
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -25,22 +26,64 @@ export type JavaScriptSourceMapReference = z.infer<
|
||||
typeof javascriptSourceMapReferenceSchema
|
||||
>;
|
||||
|
||||
/** Interpret a declaration once at the source-map ingestion boundary. */
|
||||
export const resolveJavaScriptSourceMapReference = (
|
||||
sourceName: string,
|
||||
/** Interpret declarations together once at their source-map ingestion boundary. */
|
||||
export const resolveJavaScriptSourceMapReferences = (
|
||||
sourceNames: readonly (string | null)[],
|
||||
sourceRoot: string | null,
|
||||
mapPath: string,
|
||||
): JavaScriptSourceMapReference => {
|
||||
// TraceMap's producer prefixes sourceRoot before resolving the URL reference.
|
||||
const reference = `${sourceRoot ? `${sourceRoot}/` : ""}${sourceName}`;
|
||||
return {
|
||||
source_name: sourceName,
|
||||
source_root: sourceRoot,
|
||||
map_path: mapPath,
|
||||
resolution: resolveJavaScriptSourceMapPath(reference, mapPath),
|
||||
};
|
||||
): (JavaScriptSourceMapReference | null)[] => {
|
||||
if (sourceNames.length === 0) return [];
|
||||
const resolvedSources = resolveSourceMapUrls(
|
||||
sourceNames.map((sourceName) => sourceName ?? ""),
|
||||
sourceRoot,
|
||||
"",
|
||||
);
|
||||
return sourceNames.map((sourceName, index) => {
|
||||
if (sourceName === null) return null;
|
||||
const reference = resolvedSources[index];
|
||||
if (reference === undefined)
|
||||
throw new TypeError(
|
||||
`TraceMap did not resolve source declaration ${String(index)}`,
|
||||
);
|
||||
return {
|
||||
source_name: sourceName,
|
||||
source_root: sourceRoot,
|
||||
map_path: mapPath,
|
||||
resolution: resolveJavaScriptSourceMapPath(reference, mapPath),
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/** Resolve a source declaration using the same pinned format implementation as map consumers. */
|
||||
export const resolveJavaScriptSourceMapUrl = (
|
||||
source: string | null,
|
||||
sourceRoot: string | null | undefined,
|
||||
mapUrl: string,
|
||||
): string => {
|
||||
const resolved = resolveSourceMapUrls([source ?? ""], sourceRoot, mapUrl)[0];
|
||||
if (resolved === undefined)
|
||||
throw new TypeError(
|
||||
"TraceMap did not resolve its single source declaration",
|
||||
);
|
||||
return resolved;
|
||||
};
|
||||
|
||||
const resolveSourceMapUrls = (
|
||||
sources: string[],
|
||||
sourceRoot: string | null | undefined,
|
||||
mapUrl: string,
|
||||
): readonly string[] =>
|
||||
new TraceMap(
|
||||
{
|
||||
version: 3,
|
||||
names: [],
|
||||
sources,
|
||||
...(sourceRoot == null ? {} : { sourceRoot }),
|
||||
mappings: "",
|
||||
},
|
||||
mapUrl,
|
||||
).resolvedSources;
|
||||
|
||||
/** Resolve a source-map URL reference without changing its recorded identity. */
|
||||
const resolveJavaScriptSourceMapPath = (
|
||||
source: string,
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { TraceMap } from "@jridgewell/trace-mapping";
|
||||
|
||||
import { resolveJavaScriptSourceMapReference } from "./javascriptSourceMapPaths.js";
|
||||
import {
|
||||
createJavaScriptApplicationGraph,
|
||||
createJavaScriptApplicationNode,
|
||||
} from "./javascriptApplicationGraph.js";
|
||||
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
|
||||
import { createHistoricalSourceGraph } from "../referenceSourceGraph.js";
|
||||
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
|
||||
import { compareSourceToBundle } from "./sourceToBundleComparison.js";
|
||||
@@ -12,7 +13,10 @@ import {
|
||||
SOURCE_TO_BUNDLE_SIGNAL_WEIGHTS,
|
||||
sourceToBundleComparisonResultSchema,
|
||||
} from "./sourceToBundleComparisonSchemas.js";
|
||||
import { artifactEvidence } from "./javascriptApplicationGraph.fixture.js";
|
||||
import {
|
||||
artifactEvidence,
|
||||
sourceMapReference,
|
||||
} from "./javascriptApplicationGraph.fixture.js";
|
||||
|
||||
const HASH = {
|
||||
artifact: "1".repeat(64),
|
||||
@@ -42,9 +46,6 @@ describe("historical source to bundle comparison", () => {
|
||||
const second = compareSourceToBundle(input);
|
||||
|
||||
expect(first).toEqual(second);
|
||||
expect(() =>
|
||||
sourceToBundleComparisonResultSchema.parse(first),
|
||||
).not.toThrow();
|
||||
expect(first.summary).toEqual({
|
||||
unchanged: 1,
|
||||
modified: 0,
|
||||
@@ -153,7 +154,7 @@ describe("source to bundle digest changes", () => {
|
||||
observations: [
|
||||
{
|
||||
label: "display label",
|
||||
source_map_reference: resolveJavaScriptSourceMapReference(
|
||||
source_map_reference: sourceMapReference(
|
||||
"webpack://fixture/./src/modified.ts",
|
||||
null,
|
||||
"dist/main.js.map",
|
||||
@@ -202,6 +203,90 @@ describe("source to bundle digest changes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("source to bundle Unicode path collisions", () => {
|
||||
it("keeps colliding canonical-equivalent current paths ambiguous", () => {
|
||||
const historicalPath = "src/a\u0301\u0323.ts";
|
||||
const currentPaths = ["src/a\u0323\u0301.ts", "src/\u1ea1\u0301.ts"];
|
||||
const nodes = currentPaths.map((path, index) =>
|
||||
moduleNode(`unicode-${index}`, { path }),
|
||||
);
|
||||
const result = comparePathToNodes(historicalPath, nodes);
|
||||
const mapping = item(result, historicalPath);
|
||||
|
||||
expect(mapping).toMatchObject({
|
||||
status: "unknown",
|
||||
confidence: "unknown",
|
||||
current_node_ids: [],
|
||||
candidates: [
|
||||
expect.objectContaining({
|
||||
signals: expect.arrayContaining([
|
||||
expect.objectContaining({ kind: "unicode-equivalent-path" }),
|
||||
]),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
signals: expect.arrayContaining([
|
||||
expect.objectContaining({ kind: "unicode-equivalent-path" }),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
limitations: [
|
||||
expect.stringContaining(
|
||||
"filesystem paths with distinct Unicode spellings",
|
||||
),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("prefers a raw source-map path over canonically equivalent alternatives", () => {
|
||||
const historicalPath = "src/cafe\u0301.ts";
|
||||
const exact = sourceMapNode(
|
||||
"exact",
|
||||
"cafe\u0301.ts",
|
||||
"../src/",
|
||||
HASH.modified,
|
||||
);
|
||||
const equivalent = sourceMapNode(
|
||||
"equivalent",
|
||||
"caf\u00e9.ts",
|
||||
"../src/",
|
||||
HASH.removed,
|
||||
);
|
||||
const result = comparePathToNodes(historicalPath, [exact, equivalent]);
|
||||
|
||||
expect(item(result, historicalPath)).toMatchObject({
|
||||
status: "modified",
|
||||
current_node_ids: [exact.node_id],
|
||||
});
|
||||
});
|
||||
|
||||
it("finds Unicode-equivalent suffixes of absolute source-map URLs", () => {
|
||||
const historicalPath = "src/caf\u00e9.ts";
|
||||
const node = sourceMapNode(
|
||||
"remote",
|
||||
"cafe\u0301.ts",
|
||||
"https://cdn.example/src/",
|
||||
HASH.modified,
|
||||
);
|
||||
const result = comparePathToNodes(historicalPath, [node]);
|
||||
|
||||
expect(item(result, historicalPath)).toMatchObject({
|
||||
status: "unknown",
|
||||
current_node_ids: [],
|
||||
candidates: [
|
||||
expect.objectContaining({
|
||||
current_node_id: node.node_id,
|
||||
signals: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
kind: "unicode-equivalent-path",
|
||||
current_values: ["cdn.example/src/cafe\u0301.ts"],
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("source to bundle path syntax", () => {
|
||||
it.each([
|
||||
"src/modified#part.ts",
|
||||
@@ -282,9 +367,60 @@ describe("source to bundle path syntax", () => {
|
||||
}),
|
||||
],
|
||||
});
|
||||
expect(
|
||||
item(result, "src/modified.ts").candidates.flatMap(
|
||||
({ signals }) => signals,
|
||||
),
|
||||
).not.toContainEqual(
|
||||
expect.objectContaining({ kind: "unicode-equivalent-path" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("source-map resolver equivalence", () => {
|
||||
it.each([
|
||||
{
|
||||
sourceRoot: "https://cdn.example/src/",
|
||||
expectedSource: "https://cdn.example/src/a.js",
|
||||
expectedResolution: { kind: "suffix", path: "cdn.example/src/a.js" },
|
||||
},
|
||||
{
|
||||
sourceRoot: "https://cdn.example/src//",
|
||||
expectedSource: "https://cdn.example/src/a.js",
|
||||
expectedResolution: { kind: "suffix", path: "cdn.example/src/a.js" },
|
||||
},
|
||||
{
|
||||
sourceRoot: "webpack://",
|
||||
expectedSource: "webpack:///a.js",
|
||||
expectedResolution: { kind: "suffix", path: "a.js" },
|
||||
},
|
||||
])(
|
||||
"matches TraceMap for sourceRoot $sourceRoot",
|
||||
({ sourceRoot, expectedSource, expectedResolution }) => {
|
||||
const producerSource = new TraceMap(
|
||||
{
|
||||
version: 3,
|
||||
file: "main.js",
|
||||
sourceRoot,
|
||||
sources: ["a.js"],
|
||||
names: [],
|
||||
mappings: "",
|
||||
},
|
||||
"file:///artifact/dist/main.js.map",
|
||||
).resolvedSources[0];
|
||||
const reference = sourceMapReference(
|
||||
"a.js",
|
||||
sourceRoot,
|
||||
"dist/main.js.map",
|
||||
);
|
||||
|
||||
expect(producerSource).toBe(expectedSource);
|
||||
expect(reference.source_root).toBe(sourceRoot);
|
||||
expect(reference.resolution).toEqual(expectedResolution);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("source to bundle complete metadata", () => {
|
||||
it("accepts long source metadata and all reported limitations", () => {
|
||||
const result = compareSourceToBundle({
|
||||
@@ -328,6 +464,61 @@ describe("source to bundle complete metadata", () => {
|
||||
});
|
||||
});
|
||||
|
||||
const sourceMapNode = (
|
||||
key: string,
|
||||
sourceName: string,
|
||||
sourceRoot: string,
|
||||
digest: string,
|
||||
) =>
|
||||
createJavaScriptApplicationNode({
|
||||
kind: "source-module",
|
||||
identity: {
|
||||
strategy: "source-map-original",
|
||||
source_root: sourceRoot,
|
||||
stability: "source-map-exact",
|
||||
source_map_sha256: HASH.artifact,
|
||||
original_source: sourceName,
|
||||
source_sha256: digest,
|
||||
},
|
||||
observations: [
|
||||
{
|
||||
label: `${key} source-map spelling`,
|
||||
source_map_reference: sourceMapReference(
|
||||
sourceName,
|
||||
sourceRoot,
|
||||
`dist/${key}.map`,
|
||||
),
|
||||
properties: { content_available: true, content_sha256: digest },
|
||||
evidence: artifactEvidence(HASH.artifact, `dist/${key}.map`),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const comparePathToNodes = (
|
||||
sourcePath: string,
|
||||
nodes: readonly ApplicationNode[],
|
||||
) =>
|
||||
compareSourceToBundle({
|
||||
reference: historicalGraph("complete", [sourcePath]),
|
||||
application: {
|
||||
evidenceId: EVIDENCE_ID,
|
||||
rootArtifactSha256: HASH.artifact,
|
||||
graph: createJavaScriptApplicationGraph({
|
||||
schema: "JavaScriptApplicationGraph",
|
||||
root_node_ids: nodes.map(({ node_id: nodeId }) => nodeId),
|
||||
nodes,
|
||||
edges: [],
|
||||
coverage: {
|
||||
status: "complete",
|
||||
truncated: false,
|
||||
omitted_count: 0,
|
||||
limits: [],
|
||||
},
|
||||
limitations: [],
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
const historicalGraph = (
|
||||
inventoryState: "complete" | "partial",
|
||||
paths = [
|
||||
|
||||
@@ -258,7 +258,11 @@ const classifyMapping = (
|
||||
confidence: "unknown",
|
||||
currentNodeIds: [],
|
||||
limitations: [
|
||||
"Only weak basename or extension signals were available; no mapping was forced.",
|
||||
candidates.some((candidate) =>
|
||||
hasSignal(candidate, "unicode-equivalent-path"),
|
||||
)
|
||||
? "Unicode-equivalent paths are inferred candidates only; filesystem paths with distinct Unicode spellings may identify different files, so no mapping was forced."
|
||||
: "Only weak basename or extension signals were available; no mapping was forced.",
|
||||
],
|
||||
};
|
||||
return absenceComplete
|
||||
|
||||
@@ -15,6 +15,7 @@ export const SOURCE_TO_BUNDLE_SIGNAL_WEIGHTS = [
|
||||
["source-map-original-path", 70],
|
||||
["current-path-exact", 60],
|
||||
["current-path-suffix", 40],
|
||||
["unicode-equivalent-path", 20],
|
||||
["basename-match", 20],
|
||||
["language-extension", 5],
|
||||
] as const;
|
||||
@@ -24,6 +25,7 @@ const sourceToBundleSignalKindSchema = z.enum([
|
||||
"source-map-original-path",
|
||||
"current-path-exact",
|
||||
"current-path-suffix",
|
||||
"unicode-equivalent-path",
|
||||
"basename-match",
|
||||
"language-extension",
|
||||
]);
|
||||
|
||||
@@ -102,8 +102,12 @@ export const buildSourceToBundleCandidateIndex = (
|
||||
for (const path of projection.paths) {
|
||||
for (const suffix of path.allowSuffix
|
||||
? pathSuffixes(path.value)
|
||||
: [path.value])
|
||||
: [path.value]) {
|
||||
addIndexValue(byPathSuffix, suffix, projection.node.node_id);
|
||||
const canonical = suffix.normalize("NFC");
|
||||
if (canonical !== suffix)
|
||||
addIndexValue(byPathSuffix, canonical, projection.node.node_id);
|
||||
}
|
||||
addIndexValue(
|
||||
byBasename,
|
||||
posix.basename(path.value),
|
||||
@@ -126,9 +130,13 @@ export const candidateIdsForSource = (
|
||||
source: SourceFile,
|
||||
index: SourceToBundleCandidateIndex,
|
||||
): string[] => {
|
||||
const canonicalPath = source.path.normalize("NFC");
|
||||
const groups = [
|
||||
source.sha256 === null ? undefined : index.byDigest.get(source.sha256),
|
||||
index.byPathSuffix.get(source.path),
|
||||
canonicalPath === source.path
|
||||
? undefined
|
||||
: index.byPathSuffix.get(canonicalPath),
|
||||
index.byBasename.get(posix.basename(source.path)),
|
||||
];
|
||||
const seen = new Set<string>();
|
||||
@@ -220,6 +228,14 @@ const candidateSignals = (
|
||||
signals.push(
|
||||
signal("source-map-original-path", source.path, sourceMapPaths),
|
||||
);
|
||||
const unicodeEquivalentPaths = matchingUnicodePaths(
|
||||
source.path,
|
||||
current.paths,
|
||||
);
|
||||
if (unicodeEquivalentPaths.length > 0)
|
||||
signals.push(
|
||||
signal("unicode-equivalent-path", source.path, unicodeEquivalentPaths),
|
||||
);
|
||||
const otherPaths = current.paths.filter(
|
||||
({ kind }) => kind !== "source-map-original",
|
||||
);
|
||||
@@ -286,6 +302,27 @@ const matchingPaths = (
|
||||
)
|
||||
.map(({ value }) => value);
|
||||
|
||||
const matchingUnicodePaths = (
|
||||
sourcePath: string,
|
||||
currentPaths: readonly CurrentPath[],
|
||||
): string[] => {
|
||||
const canonicalSource = sourcePath.normalize("NFC");
|
||||
return currentPaths
|
||||
.filter(({ value, allowSuffix }) => {
|
||||
if (
|
||||
value === sourcePath ||
|
||||
(allowSuffix && value.endsWith(`/${sourcePath}`))
|
||||
)
|
||||
return false;
|
||||
const canonicalValue = value.normalize("NFC");
|
||||
return (
|
||||
canonicalValue === canonicalSource ||
|
||||
(allowSuffix && canonicalValue.endsWith(`/${canonicalSource}`))
|
||||
);
|
||||
})
|
||||
.map(({ value }) => value);
|
||||
};
|
||||
|
||||
const addJsonPath = (paths: CurrentPath[], value: unknown): void => {
|
||||
if (typeof value === "string") addPath(paths, "observation-path", value);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user