From 838f5f44d272613f58770e4e4f591b0d778a458c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:40:36 +0800 Subject: [PATCH] fix(reference): preserve incomplete resolution uncertainty --- .../ReferenceSourceImportEntries.test.ts | 173 ++++++++++++++++++ .../ReferenceSourceImportEntries.ts | 116 +++++++++++- 2 files changed, 286 insertions(+), 3 deletions(-) diff --git a/src/application/ReferenceSourceImportEntries.test.ts b/src/application/ReferenceSourceImportEntries.test.ts index 8d01d079a9..6f934cf886 100644 --- a/src/application/ReferenceSourceImportEntries.test.ts +++ b/src/application/ReferenceSourceImportEntries.test.ts @@ -364,3 +364,176 @@ it("resolves extensionless imports and requires to .mts and .cts files and direc ]), ); }); + +it("does not infer missing candidates or manifests from partial directory enumeration", () => { + const file = ( + path: string, + content: string, + ): ReferenceSourceRead["entries"][number] => ({ + status: "read", + kind: "file", + path, + bytes: Buffer.from(content), + size: Buffer.byteLength(content), + }); + const read: ReferenceSourceRead = { + root: "/selected/source", + bytesRead: 0, + limitations: [], + entries: [ + file("package.json", '{"type":"commonjs"}'), + file( + "main.cjs", + 'require("./pkg"); require("./other"); require("./exact.js");', + ), + file("pkg/index.js", "module.exports = 1;"), + file("other/index.js", "module.exports = 1;"), + file("exact.js", "module.exports = 1;"), + file("pkg/source.js", 'require("../exact.js");'), + { + status: "failed", + kind: "directory", + path: "pkg", + code: "io", + message: "Partial enumeration", + }, + { status: "read", kind: "directory", path: "other" }, + ], + }; + const parsed = parseReferenceSourceEntries(read, []); + expect( + parsed.relationships.filter(({ from_path }) => from_path === "main.cjs"), + ).toEqual([ + expect.objectContaining({ to: "pkg", resolution: "unresolved" }), + expect.objectContaining({ to: "other/index.js", resolution: "internal" }), + expect.objectContaining({ to: "exact.js", resolution: "internal" }), + ]); + expect( + parsed.relationships.filter( + ({ from_path }) => from_path === "pkg/source.js", + ), + ).not.toContainEqual(expect.objectContaining({ resolution: "internal" })); + const partialRoot = parseReferenceSourceEntries( + { ...read, rootFailure: { code: "io", message: "Partial root" } }, + [], + ); + expect( + partialRoot.relationships.find( + ({ from_path, to }) => + from_path === "main.cjs" && to === "other/index.js", + ), + ).toBeUndefined(); + expect(partialRoot.relationships).toContainEqual( + expect.objectContaining({ + from_path: "main.cjs", + to: "exact.js", + resolution: "internal", + }), + ); +}); + +it.each(["failed", "excluded"] as const)( + "keeps higher priority %s candidates unknown", + (state) => { + const files = [ + ["main.cjs", 'require("./pkg"); require("./pkg/index.js");'], + ["pkg/index.js", "module.exports = 1;"], + ] as const; + const read: ReferenceSourceRead = { + root: "/selected/source", + bytesRead: 0, + limitations: [], + entries: [ + ...files.map(([path, content]) => ({ + status: "read" as const, + kind: "file" as const, + path, + bytes: Buffer.from(content), + size: Buffer.byteLength(content), + })), + ...(state === "failed" + ? [ + { + status: "failed" as const, + kind: "file" as const, + path: "pkg.js", + code: "io" as const, + message: "Unreadable", + }, + ] + : []), + ], + }; + const parsed = parseReferenceSourceEntries( + read, + state === "excluded" + ? [ + { + path: "pkg.js", + entry_kind: "file", + reason: "caller-excluded", + pattern: "pkg.js", + }, + ] + : [], + ); + expect(parsed.relationships).toEqual([ + expect.objectContaining({ to: "pkg", resolution: "unresolved" }), + expect.objectContaining({ to: "pkg/index.js", resolution: "internal" }), + ]); + }, +); + +it("retains observed manifests and closer completed scopes inside a partial inventory", () => { + const files = [ + ["main.cjs", 'require("./pkg");'], + ["pkg/package.json", '{"main":"entry.js","type":"commonjs"}'], + ["pkg/entry.js", 'require("./known.js");'], + ["pkg/known.js", "module.exports = 1;"], + ["complete/source.js", 'require("./known.js");'], + ["complete/known.js", "module.exports = 1;"], + ["complete/package.json", '{"type":"commonjs"}'], + ] as const; + const parsed = parseReferenceSourceEntries( + { + root: "/selected/source", + bytesRead: 0, + limitations: [], + entries: [ + ...files.map(([path, content]) => ({ + status: "read" as const, + kind: "file" as const, + path, + bytes: Buffer.from(content), + size: Buffer.byteLength(content), + })), + { + status: "failed", + kind: "directory", + path: "pkg", + code: "io", + message: "Partial enumeration", + }, + { status: "read", kind: "directory", path: "complete" }, + ], + }, + [], + ); + expect(parsed.relationships).toEqual([ + expect.objectContaining({ + from_path: "main.cjs", + to: "pkg/entry.js", + resolution: "internal", + }), + expect.objectContaining({ + from_path: "pkg/entry.js", + to: "pkg/known.js", + resolution: "internal", + }), + expect.objectContaining({ + from_path: "complete/source.js", + to: "complete/known.js", + resolution: "internal", + }), + ]); +}); diff --git a/src/application/ReferenceSourceImportEntries.ts b/src/application/ReferenceSourceImportEntries.ts index a1de8ed465..c7571579e4 100644 --- a/src/application/ReferenceSourceImportEntries.ts +++ b/src/application/ReferenceSourceImportEntries.ts @@ -124,6 +124,7 @@ const UNKNOWN_PACKAGE: PackageMetadata = { const readPackageMetadata = ( read: ReferenceSourceRead, exclusions: HistoricalSourceGraphInput["exclusions"], + unobservedPath: (path: string) => boolean, ): { readonly packages: ReadonlyMap; readonly pathLookup: ReferenceSourcePathLookup; @@ -176,6 +177,22 @@ const readPackageMetadata = ( packages.set(directory, UNKNOWN_PACKAGE); } } + // An incomplete directory cannot establish that its manifest is absent. + const uncertainDirectories = [ + ...(read.rootFailure === undefined ? [] : ["."]), + ...read.entries + .filter( + (entry) => entry.kind === "directory" && entry.status === "failed", + ) + .map(({ path }) => path), + ]; + for (const directory of uncertainDirectories) { + const path = posix.join(directory, "package.json"); + if (!packages.has(directory) && unobservedPath(path)) { + manifestPaths.push(path); + packages.set(directory, UNKNOWN_PACKAGE); + } + } return { packages, pathLookup: createReferenceSourcePathLookup(manifestPaths), @@ -226,8 +243,74 @@ interface SpecifierContext { readonly lookupPath: ReferenceSourcePathLookup; readonly packages: ReadonlyMap; readonly packageManifestPathLookup: ReferenceSourcePathLookup; + readonly unobservedPath: (path: string) => boolean; } +/** Earlier unobserved candidates must not be treated as absent. */ +const selectObservedCandidate = ( + candidates: readonly string[], + context: SpecifierContext, +): Result => { + const selected = context.lookupPath.resolveCandidates(candidates); + for (const candidate of candidates) { + if ( + context.lookupPath.resolve(candidate) === selected && + selected !== undefined + ) + break; + if (context.unobservedPath(candidate)) return err(undefined); + } + return ok(selected); +}; + +/** The closest directory observation owns whether an absent child was examined. */ +const unobservedReferencePath = ( + read: ReferenceSourceRead, + exclusions: HistoricalSourceGraphInput["exclusions"], +): ((path: string) => boolean) => { + const observed = createReferenceSourcePathLookup( + read.entries.map(({ path }) => path), + ); + const unavailable = createReferenceSourcePathLookup([ + ...read.entries + .filter( + (entry) => + entry.kind !== "directory" && + (entry.status === "failed" || entry.kind === "symlink"), + ) + .map(({ path }) => path), + ...exclusions + .filter((entry) => entry.entry_kind !== "directory") + .map(({ path }) => path), + ]); + const excludedDirectories = createReferenceSourcePathLookup( + exclusions + .filter((entry) => entry.entry_kind === "directory") + .map(({ path }) => path), + ); + const directories = new Map( + read.entries + .filter((entry) => entry.kind === "directory") + .map((entry) => [entry.path, entry.status]), + ); + const directoryLookup = createReferenceSourcePathLookup(directories.keys()); + return (path) => { + if (unavailable.has(path)) return true; + if (observed.has(path)) return false; + for ( + let parent = posix.dirname(path); + parent !== "." && parent !== "/"; + parent = posix.dirname(parent) + ) { + if (excludedDirectories.has(parent)) return true; + const directory = directoryLookup.resolve(parent); + if (directory !== undefined) + return directories.get(directory) === "failed"; + } + return read.rootFailure !== undefined; + }; +}; + const packageMainForDirectory = ( directory: string, context: SpecifierContext, @@ -291,7 +374,9 @@ const selectPackageMain = ( .filter((suffix) => suffix !== "") .map((suffix) => posix.join(target, `index${suffix}`)), ]; - const selected = context.lookupPath.resolveCandidates(candidates); + const selection = selectObservedCandidate(candidates, context); + if (!selection.ok) return { to: packageDirectory, resolution: "unresolved" }; + const selected = selection.value; return selected === undefined ? indexFallback === undefined ? { to: packageDirectory, resolution: "unresolved" } @@ -390,7 +475,26 @@ const resolveInternalSpecifier = ( ({ path }) => path, ), ); - const match = context.lookupPath.resolveCandidates(candidates); + const selection = selectObservedCandidate(candidates, context); + if (!selection.ok) { + // Node consults a known package main before probing directory indexes. + // Incomplete index coverage cannot invalidate that observed declaration. + if ( + mode === "runtime-require" && + packageMainForDirectory(normalized, context)?.main.state === "declared" + ) { + const files = directoryReference + ? ok(undefined) + : selectObservedCandidate( + nodeRequireFileCandidates(normalized), + context, + ); + if (files.ok && files.value === undefined) + return selectPackageMain(normalized, undefined, context, mode); + } + return { to: normalized, resolution: "unresolved" }; + } + const match = selection.value; if (match === undefined) return packageMainForDirectory(normalized, context) !== undefined ? selectPackageMain(normalized, undefined, context, mode) @@ -547,13 +651,19 @@ export const parseReferenceSourceEntries = ( output.limitations.push( `Reference root enumeration is incomplete: ${read.rootFailure.message}`, ); - const packageInventory = readPackageMetadata(read, exclusions); + const unobservedPath = unobservedReferencePath(read, exclusions); + const packageInventory = readPackageMetadata( + read, + exclusions, + unobservedPath, + ); const context: SpecifierContext = { lookupPath: createReferenceSourcePathLookup( read.entries .filter((entry) => entry.status === "read" && entry.kind === "file") .map((entry) => entry.path), ), + unobservedPath, packages: packageInventory.packages, packageManifestPathLookup: packageInventory.pathLookup, };