diff --git a/src/nuget/NugetNupkgParser.test.ts b/src/nuget/NugetNupkgParser.test.ts index ee30ce9c0f..2a18f114cc 100644 --- a/src/nuget/NugetNupkgParser.test.ts +++ b/src/nuget/NugetNupkgParser.test.ts @@ -20,11 +20,7 @@ import { ungroupedNuspec, } from "./NugetNupkg.fixture.js"; import { ArtifactReaderFailure } from "../resources/index.js"; -import { - NUPKG_LIMITATIONS, - distinctTargetMatches, - parseNugetNupkg, -} from "./NugetNupkgParser.js"; +import { NUPKG_LIMITATIONS, parseNugetNupkg } from "./NugetNupkgParser.js"; const defaults = inspectNugetNupkgInputSchema.parse({ path: "/artifacts/Demo.Package.nupkg", @@ -542,9 +538,11 @@ it.each([ }, ); -const manifestRels = (target: string) => ` +const manifestRels = ( + ...targets: string[] +) => ` - + ${targets.map((target, index) => ``).join("\n ")} `; @@ -846,6 +844,16 @@ it.each([ const report = await parseNugetNupkg(bytes, artifactOf(bytes), defaults); expect(report.nuspec?.source).toBe(source); expect(report.identity?.id).toBe(id); + const warnings = report.limitations.filter((line) => + line.includes("case-insensitive ZIP name"), + ); + expect(warnings).toEqual( + source === "opc-relationship" + ? [ + "The OPC manifest relationship matched a .nuspec member by case-insensitive ZIP name.", + ] + : [], + ); }, ); @@ -916,23 +924,26 @@ it("reports unknown coverage when the central directory leaves bytes unaccounted ).toBe(true); }); -it("stops resolving manifest targets once a second distinct member is found", () => { - const bucket = Array.from({ length: 10_000 }, (_, index) => ({ index })); - const match = vi.fn((_target: string) => bucket); - const targets = Array.from( - { length: 10_000 }, - (_, index) => `T${index}.nuspec`, +it("follows multiple relationship spellings of a single physical manifest", async () => { + const bytes = await nugetNupkgFixture({ + includeRels: false, + extraMembers: { + "_rels/.rels": manifestRels( + "/CONTENT/OTHER.nuspec", + "/content/./Other.nuspec", + ), + "content/Other.nuspec": ungroupedNuspec, + }, + }); + const report = await parseNugetNupkg(bytes, artifactOf(bytes), defaults); + expect(report.nuspec).toMatchObject({ + path: "content/Other.nuspec", + source: "opc-relationship", + }); + expect(report.identity?.id).toBe("Ungrouped.Package"); + expect(report.limitations).not.toContainEqual( + expect.stringContaining("case-insensitive ZIP name"), ); - expect(distinctTargetMatches(targets, match)).toEqual([bucket[0], bucket[1]]); - expect(match).toHaveBeenCalledTimes(1); -}); - -it("keeps one member when every manifest target resolves to it", () => { - const member = { name: "Only.nuspec" }; - const match = vi.fn((_target: string) => [member]); - const targets = Array.from({ length: 1_000 }, () => "ONLY.nuspec"); - expect(distinctTargetMatches(targets, match)).toEqual([member]); - expect(match).toHaveBeenCalledTimes(1_000); }); it("keeps a foreign group from suppressing ungrouped NuGet dependencies", async () => { diff --git a/src/nuget/NugetNupkgParser.ts b/src/nuget/NugetNupkgParser.ts index 292434e80d..1a71571bdc 100644 --- a/src/nuget/NugetNupkgParser.ts +++ b/src/nuget/NugetNupkgParser.ts @@ -473,24 +473,6 @@ const parseNuspecDocument = ( }; }; -/** - * Collect the distinct members the manifest targets resolve to, stopping at - * the second. Many package-controlled targets can each hit one large - * case-folded bucket, so the matches are never materialized together. - */ -export const distinctTargetMatches = ( - targets: readonly string[], - match: (target: string) => readonly T[], -): T[] => { - const found = new Set(); - for (const target of targets) - for (const member of match(target)) { - found.add(member); - if (found.size > 1) return [...found]; - } - return [...found]; -}; - /** OPC part-name equivalence folds ASCII case only. */ const opcPartNameKey = (name: string): string => name.replace(/[A-Z]/gu, (letter) => letter.toLowerCase()); @@ -505,32 +487,29 @@ const findNuspec = ( | undefined; readonly source: "opc-relationship" | "root-nuspec" | undefined; } => { - // Keep every ZIP occurrence in its OPC equivalence bucket. An exact - // spelling cannot resolve two occurrences with equivalent part names. - // Index once: every target is then a constant-time lookup, however many - // relationships the package declares. - const partNames = new Map(); - for (const file of files) { - const key = opcPartNameKey(file.name); - const members = partNames.get(key); - if (members === undefined) partNames.set(key, [file]); - else members.push(file); - } if (relsXml !== undefined) { const targets = opcManifestTargets(relsXml, limitations); - const resolved = distinctTargetMatches( - targets.map((target) => target.lookup), - (target) => partNames.get(opcPartNameKey(target)) ?? [], + const targetKeys = new Set( + targets.map(({ lookup }) => opcPartNameKey(lookup)), ); - if (resolved.length > 1) { - limitations.push( - "OPC manifest relationships resolve to more than one .nuspec ZIP member; identity was not decoded.", - ); - return { member: undefined, source: undefined }; + let member: (typeof files)[number] | undefined; + if (targetKeys.size > 0) { + // Visit each physical occurrence once. Aliases of one part agree, but + // even an exact target cannot choose between two equivalent members. + for (const file of files) { + if (!targetKeys.has(opcPartNameKey(file.name))) continue; + if (member !== undefined) { + limitations.push( + "OPC manifest relationships resolve to more than one .nuspec ZIP member; identity was not decoded.", + ); + return { member: undefined, source: undefined }; + } + member = file; + } } - if (resolved.length === 1) { - const member = resolved[0]!; - if (!targets.some((target) => target.lookup === member.name)) + if (member !== undefined) { + const name = member.name; + if (!targets.some((target) => target.lookup === name)) limitations.push( "The OPC manifest relationship matched a .nuspec member by case-insensitive ZIP name.", );