fix(nuget): read packaging parts and dependency groups as NuGet does (#11845)

Members under package/ are OPC packaging parts, like _rels/ and
[Content_Types].xml; nuget pack writes the core properties .psmdcp there.

Dependencies come from every <dependencies> element. When any <group>
exists, ungrouped <dependency> elements are omitted with a limitation that
counts them, because NuGet's restore-time NuspecReader ignores them.
This commit is contained in:
RRFZ authored and GitHub committed 2026-11-18 09:23:27 -05:00
1 parent 002716a998
commit 9c1e98f1cd
6 files changed
+97 -25

No files matched your search

+10 -4
View File
@@ -46,15 +46,21 @@ relationship diagnostics apply the same exclusion.
`dependency_groups` lists each `.nuspec` dependency group with its
`target_framework` and dependencies (`id`, `version` range as spelled,
`include`, `exclude`, `development_dependency`). Ungrouped dependencies appear
as a group with `target_framework: null`. Version ranges are recorded as text;
restore results are unknown.
as a group with `target_framework: null` when the `.nuspec` has no `<group>`.
Beside `<group>` elements, in any `<dependencies>` element, they are omitted and
a limitation gives their count: NuGet's restore-time reader ignores them, and
`nuget pack` refuses that mix. Version ranges are recorded as text; restore
results are unknown.
`contents` lists file ZIP members with the stored ZIP `name`, a logical `path`
(backslashes as `/`, leading `./` removed), uncompressed and compressed sizes,
an `encrypted` flag, and a `path`-derived `role` (`lib`, `ref`, `runtimes`,
`native`, `analyzers`, `build`, `buildTransitive`, `buildMultiTargeting`,
`content`, `contentFiles`, `tools`, `nuspec`, `opc`, or `unknown`). Directory
markers are omitted. Encrypted members are listed and not extracted.
`content`, `contentFiles`, `tools`, `nuspec`, `opc`, or `unknown`). `opc` marks
OPC packaging parts: `[Content_Types].xml`, `.rels` relationship parts, and
members under `_rels/` or `package/`, where `nuget pack` writes the core
properties `.psmdcp`. Directory markers are omitted. Encrypted members are
listed and not extracted.
Unflagged non-ASCII names use the ZIP CP437 fallback unless a valid Unicode Path
extra field supplies the name. For fallback names, limitations identify the
member, retain the exact raw filename bytes as hex, and mark the source encoding
+6 -4
View File
@@ -16,11 +16,13 @@ executing anything in it or contacting a NuGet feed.
`.nuspec`. Missing, ambiguous or malformed manifests succeed with the ZIP
inventory, `identity: null` and an explicit limitation.
- Report `identity`, `license`, `repository` and `dependency_groups` as spelled
in the `.nuspec`. Ungrouped dependencies form a group with
`target_framework: null`. Version ranges stay text; restore results are
unknown.
in the `.nuspec`. Without any `<group>`, ungrouped dependencies form a group
with `target_framework: null`. Beside groups, in any `<dependencies>` element,
they are omitted with a limitation that counts them, as NuGet's restore-time
reader ignores them. Version ranges stay text; restore results are unknown.
- List file ZIP members with sizes, an `encrypted` flag and a path-derived
NuGet folder `role`. Omit directory markers.
NuGet folder `role`. `[Content_Types].xml`, `.rels` parts, and members under
`_rels/` or `package/` are OPC packaging parts. Omit directory markers.
- Bound the file size, ZIP entry count and `.nuspec` bytes with named
allowances. Enforce the entry allowance while enumerating the ZIP. Refuse a
file that is not a valid ZIP without a partial inventory.
+10
View File
@@ -103,5 +103,15 @@ it("derives NuGet folder roles from ZIP names", () => {
expect(nugetNupkgEntryRole("Demo.Package.nuspec")).toBe("nuspec");
expect(nugetNupkgEntryRole("_rels/.rels")).toBe("opc");
expect(nugetNupkgEntryRole("[Content_Types].xml")).toBe("opc");
expect(
nugetNupkgEntryRole(
"package/services/metadata/core-properties/0f07893ec48a4e929e4f9637270a574f.psmdcp",
),
).toBe("opc");
expect(nugetNupkgEntryRole("PACKAGE\\services\\metadata\\x.psmdcp")).toBe(
"opc",
);
expect(nugetNupkgEntryRole("package.txt")).toBe("unknown");
expect(nugetNupkgEntryRole("lib/package/Demo.dll")).toBe("lib");
expect(nugetNupkgEntryRole("README.md")).toBe("unknown");
});
+4
View File
@@ -64,9 +64,13 @@ export const nugetNupkgLogicalPath = (name: string): string =>
export const nugetNupkgEntryRole = (name: string): NugetNupkgEntryRole => {
const logical = nugetNupkgLogicalPath(name);
const lower = logical.toLowerCase();
// NuGet excludes `_rels/`, `package/` and `[Content_Types].xml` from package
// files; `nuget pack` writes the OPC core properties part under
// `package/services/metadata/core-properties/`.
if (
lower === "[content_types].xml" ||
lower.startsWith("_rels/") ||
lower.startsWith("package/") ||
lower.endsWith(".rels")
)
return "opc";
+45
View File
@@ -141,6 +141,9 @@ it("reads identity, license, repository, dependency groups and folder roles", as
);
expect(report.coverage.status).toBe("complete");
expect(report.limitations).toContain(NUPKG_LIMITATIONS.memberRoles);
expect(report.limitations).not.toContainEqual(
expect.stringContaining("beside <group> elements were omitted"),
);
});
it("keeps the stored ZIP member name beside its logical path", async () => {
@@ -185,8 +188,50 @@ it("records ungrouped dependencies as a group with a null target framework", asy
],
},
]);
expect(report.limitations).not.toContain(
NUPKG_LIMITATIONS.ungroupedBesideGroups(1),
);
});
const NET8_GROUP =
'<group targetFramework="net8.0"><dependency id="Bar" version="2.0.0" /></group>';
it.for([
["in one <dependencies> element", `<dependencies>\n ${NET8_GROUP}`],
[
"in a separate <dependencies> element",
`<dependencies>${NET8_GROUP}</dependencies>\n <dependencies>`,
],
])(
"omits ungrouped dependencies beside groups %s, as NuGet does",
async ([, opening]) => {
const bytes = await nugetNupkgFixture({
nuspec: ungroupedNuspec.replace("<dependencies>", opening!),
includeRels: false,
});
const report = nugetNupkgSchema.parse(
await parseNugetNupkg(bytes, artifactOf(bytes), defaults),
);
expect(report.dependency_groups).toEqual([
{
target_framework: "net8.0",
dependencies: [
{
id: "Bar",
version: "2.0.0",
include: null,
exclude: null,
development_dependency: null,
},
],
},
]);
expect(report.limitations).toContain(
NUPKG_LIMITATIONS.ungroupedBesideGroups(1),
);
},
);
it("inventories a package with no .nuspec and leaves identity unknown", async () => {
const bytes = await nugetNupkgMissingNuspecFixture();
const report = nugetNupkgSchema.parse(
+22 -17
View File
@@ -44,6 +44,8 @@ export const NUPKG_LIMITATIONS = {
ambiguousRootNuspec:
"Multiple root .nuspec members are present; identity was not decoded.",
malformedNuspec: "The selected .nuspec is not well-formed XML.",
ungroupedBesideGroups: (count: number): string =>
`${String(count)} ungrouped <dependency> element(s) beside <group> elements were omitted: NuGet's restore-time NuspecReader reads only the groups when any group is present, and nuget pack refuses this mix.`,
} as const;
const BASE_LIMITATIONS = [
@@ -280,11 +282,17 @@ const parseBooleanAttribute = (value: string | null): boolean | null => {
const parseDependencies = (
metadata: Element,
limitations: string[],
): NugetNupkg["dependency_groups"] => {
const container = firstNamed(metadata, "dependencies");
if (container === undefined) return [];
const groups = childrenNamed(container, "group");
const ungrouped = childrenNamed(container, "dependency");
// NuspecReader walks every <dependencies> element, with one decision about
// groups across all of them.
const containers = childrenNamed(metadata, "dependencies");
const groups = containers.flatMap((container) =>
childrenNamed(container, "group"),
);
const ungrouped = containers.flatMap((container) =>
childrenNamed(container, "dependency"),
);
const readDependency = (
element: Element,
): NugetNupkg["dependency_groups"][number]["dependencies"][number] => ({
@@ -302,18 +310,15 @@ const parseDependencies = (
target_framework: attribute(element, "targetFramework"),
dependencies: childrenNamed(element, "dependency").map(readDependency),
});
if (groups.length > 0)
return [
...groups.map(fromGroup),
...(ungrouped.length === 0
? []
: [
{
target_framework: null,
dependencies: ungrouped.map(readDependency),
},
]),
];
// NuGet's NuspecReader reads ungrouped dependencies only when no group is
// present, so those beside groups never reach restore.
if (groups.length > 0) {
if (ungrouped.length > 0)
limitations.push(
NUPKG_LIMITATIONS.ungroupedBesideGroups(ungrouped.length),
);
return groups.map(fromGroup);
}
if (ungrouped.length === 0) return [];
return [
{ target_framework: null, dependencies: ungrouped.map(readDependency) },
@@ -393,7 +398,7 @@ const parseNuspecDocument = (
commit: attribute(repositoryElement, "commit"),
branch: attribute(repositoryElement, "branch"),
},
dependency_groups: parseDependencies(metadata),
dependency_groups: parseDependencies(metadata, limitations),
};
};