refactor(nuget): select declared manifest occurrences in one pass (#13402)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 06:49:10 -05:00
1 parent 1d85f680f9
commit 4bb5c87d2b
2 files changed
+53 -63

No files matched your search

+34 -23
View File
@@ -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) => `<?xml version="1.0" encoding="utf-8"?>
const manifestRels = (
...targets: string[]
) => `<?xml version="1.0" encoding="utf-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Type="http://schemas.microsoft.com/packaging/2010/07/manifest" Target="${target}" Id="R1" />
${targets.map((target, index) => `<Relationship Type="http://schemas.microsoft.com/packaging/2010/07/manifest" Target="${target}" Id="R${index + 1}" />`).join("\n ")}
</Relationships>
`;
@@ -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 () => {
+19 -40
View File
@@ -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 = <T>(
targets: readonly string[],
match: (target: string) => readonly T[],
): T[] => {
const found = new Set<T>();
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<string, (typeof files)[number][]>();
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.",
);