fix(reference): preserve exact paths and unify symlink lookup
Validation: integrated changes passed formatting, typecheck, lint, resolved module-boundary verification and git diff --check. Tests and real-provider workflows were not run.
This commit is contained in:
@@ -4,7 +4,10 @@ import { z } from "zod";
|
||||
|
||||
import { canonicalJson } from "./comparisonSemantics.js";
|
||||
import { digestCanonicalValue } from "./canonicalDigest.js";
|
||||
import { normalizeJoinedLogicalPath } from "./artifactIdentity.js";
|
||||
import {
|
||||
createReferenceSourcePathLookup,
|
||||
isPortableAbsoluteReferenceTarget,
|
||||
} from "./referenceSourcePathIdentity.js";
|
||||
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
|
||||
import { digestSchema } from "./../domain/digests.js";
|
||||
import { prefixedDigestSchema } from "./../domain/digests.js";
|
||||
@@ -82,18 +85,13 @@ const symlinkTargetSchema = z
|
||||
.min(1)
|
||||
.refine(
|
||||
(target) =>
|
||||
isPortableAbsoluteSymlinkTarget(target) ||
|
||||
isPortableAbsoluteReferenceTarget(target) ||
|
||||
(!target.startsWith("/") &&
|
||||
!target.includes("\\") &&
|
||||
posix.normalize(target) === target),
|
||||
"Expected a normalized POSIX symlink target",
|
||||
);
|
||||
|
||||
const isPortableAbsoluteSymlinkTarget = (target: string): boolean =>
|
||||
target.startsWith("/") ||
|
||||
/^[A-Za-z]:[\\/]/u.test(target) ||
|
||||
/^\\\\[^\\/]+[\\/][^\\/]+/u.test(target);
|
||||
|
||||
const sourceSymlinkSchema = z.union([
|
||||
z.strictObject({
|
||||
...entryBaseShape,
|
||||
@@ -314,18 +312,11 @@ const checkSymlinks = (
|
||||
paths: ReadonlySet<string>,
|
||||
context: z.RefinementCtx,
|
||||
): void => {
|
||||
const normalizedPathCounts = new Map<string, number>();
|
||||
for (const path of paths) {
|
||||
const identity = normalizeJoinedLogicalPath(path);
|
||||
normalizedPathCounts.set(
|
||||
identity,
|
||||
(normalizedPathCounts.get(identity) ?? 0) + 1,
|
||||
);
|
||||
}
|
||||
const lookupPath = createReferenceSourcePathLookup(paths);
|
||||
for (const [index, entry] of graph.entries.entries()) {
|
||||
if (entry.kind !== "symlink") continue;
|
||||
if (entry.target === null) continue;
|
||||
const absolute = isPortableAbsoluteSymlinkTarget(entry.target);
|
||||
const absolute = isPortableAbsoluteReferenceTarget(entry.target);
|
||||
if (
|
||||
(entry.target_state === "external" && !absolute) ||
|
||||
(entry.target_state === "internal" && absolute)
|
||||
@@ -339,11 +330,11 @@ const checkSymlinks = (
|
||||
const resolved = posix.normalize(
|
||||
posix.join(posix.dirname(entry.path), entry.target),
|
||||
);
|
||||
const normalized = normalizeJoinedLogicalPath(resolved);
|
||||
if (
|
||||
resolved.startsWith("../") ||
|
||||
resolved === ".." ||
|
||||
(!paths.has(resolved) && normalizedPathCounts.get(normalized) !== 1)
|
||||
// The inventory root is implicit rather than an entry with path ".".
|
||||
(resolved !== "." && lookupPath.resolve(resolved) === undefined)
|
||||
)
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
export interface ReferenceSourcePathLookup {
|
||||
readonly resolve: (path: string) => string | undefined;
|
||||
readonly resolveCandidates: (paths: readonly string[]) => string | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reference inventories retain filesystem spelling. Prefer that exact identity;
|
||||
* canonical Unicode equivalence is a fallback only when it identifies one entry.
|
||||
*/
|
||||
export const createReferenceSourcePathLookup = (
|
||||
paths: Iterable<string>,
|
||||
): ReferenceSourcePathLookup => {
|
||||
const exact = new Set(paths);
|
||||
const canonical = new Map<string, string | null>();
|
||||
for (const path of exact) {
|
||||
const key = path.normalize("NFC");
|
||||
canonical.set(key, canonical.has(key) ? null : path);
|
||||
}
|
||||
const equivalent = (path: string): string | undefined =>
|
||||
canonical.get(path.normalize("NFC")) ?? undefined;
|
||||
return {
|
||||
resolve: (path) => (exact.has(path) ? path : equivalent(path)),
|
||||
resolveCandidates: (candidates) =>
|
||||
candidates.find((path) => exact.has(path)) ??
|
||||
candidates.map(equivalent).find((path) => path !== undefined),
|
||||
};
|
||||
};
|
||||
|
||||
/** Symlink evidence can originate on a different OS from the graph reader. */
|
||||
export const isPortableAbsoluteReferenceTarget = (target: string): boolean =>
|
||||
target.startsWith("/") ||
|
||||
/^[A-Za-z]:[\\/]/u.test(target) ||
|
||||
/^\\\\[^\\/]+[\\/][^\\/]+/u.test(target);
|
||||
Reference in New Issue
Block a user