fix(nuget): preserve declared dependencies beside foreign groups (#12500)

* fix(nuget): preserve declared dependencies beside foreign groups

* fix(nuget): share namespace admission with typed manifest fields

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
RRFZandmorluto authored and GitHub committed 2026-11-19 20:17:57 +00:00
1 parent 7ad140b5a1
commit 34b7356fd2
3 files changed
+92 -17

No files matched your search

+18
View File
@@ -790,3 +790,21 @@ it("keeps one member when every manifest target resolves to it", () => {
expect(distinctTargetMatches(targets, match)).toEqual([member]);
expect(match).toHaveBeenCalledTimes(1_000);
});
it("keeps a foreign group from suppressing ungrouped NuGet dependencies", async () => {
const bytes = await nugetNupkgFixture({
nuspec: `<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd" xmlns:ext="urn:example">
<metadata>
<id>Demo.Package</id><version>1.0.0</version>
<dependencies>
<ext:group targetFramework="net8.0"><ext:dependency id="Extension" /></ext:group>
<dependency id="Declared" version="1.0" />
</dependencies>
</metadata>
</package>`,
});
const result = await parseNugetNupkg(bytes, artifactOf(bytes), defaults);
expect(result.dependency_groups).toMatchObject([
{ target_framework: null, dependencies: [{ id: "Declared" }] },
]);
});
+40 -15
View File
@@ -69,11 +69,22 @@ const childElements = (element: Element): Element[] => {
return children;
};
const childrenNamed = (element: Element, local: string): Element[] =>
childElements(element).filter((child) => child.localName === local);
const childrenNamed = (
element: Element,
local: string,
namespace?: string,
): Element[] =>
childElements(element).filter(
(child) =>
child.localName === local &&
(namespace === undefined || (child.namespaceURI ?? "") === namespace),
);
const firstNamed = (element: Element, local: string): Element | undefined =>
childrenNamed(element, local)[0];
const firstNamed = (
element: Element,
local: string,
namespace?: string,
): Element | undefined => childrenNamed(element, local, namespace)[0];
const nonempty = (value: string | null | undefined): string | null => {
if (value === null || value === undefined) return null;
@@ -293,16 +304,17 @@ const parseBooleanAttribute = (value: string | null): boolean | null => {
const parseDependencies = (
metadata: Element,
namespace: string,
limitations: string[],
): NugetNupkg["dependency_groups"] => {
// NuspecReader walks every <dependencies> element, with one decision about
// groups across all of them.
const containers = childrenNamed(metadata, "dependencies");
const containers = childrenNamed(metadata, "dependencies", namespace);
const groups = containers.flatMap((container) =>
childrenNamed(container, "group"),
childrenNamed(container, "group", namespace),
);
const ungrouped = containers.flatMap((container) =>
childrenNamed(container, "dependency"),
childrenNamed(container, "dependency", namespace),
);
const readDependency = (
element: Element,
@@ -319,7 +331,9 @@ const parseDependencies = (
element: Element,
): NugetNupkg["dependency_groups"][number] => ({
target_framework: attribute(element, "targetFramework"),
dependencies: childrenNamed(element, "dependency").map(readDependency),
dependencies: childrenNamed(element, "dependency", namespace).map(
readDependency,
),
});
// NuGet's NuspecReader reads ungrouped dependencies only when no group is
// present, so those beside groups never reach restore.
@@ -376,17 +390,24 @@ const parseNuspecDocument = (
limitations.push("The selected .nuspec has no metadata element.");
return empty;
}
const metadataText = (local: string): string | null => {
const element = firstNamed(metadata, local);
// NuGet's typed getters select children in the default namespace in scope.
// Authors and description come from its local-name metadata index.
const metadataNamespace = metadata.lookupNamespaceURI("") ?? "";
const metadataText = (local: string, namespace?: string): string | null => {
const element = firstNamed(metadata, local, namespace);
return element === undefined ? null : elementText(element);
};
const licenseElement = firstNamed(metadata, "license");
const licenseElement = firstNamed(metadata, "license", metadataNamespace);
const licenseType = licenseElement ? attribute(licenseElement, "type") : null;
const repositoryElement = firstNamed(metadata, "repository");
const repositoryElement = firstNamed(
metadata,
"repository",
metadataNamespace,
);
return {
identity: {
id: metadataText("id"),
version: metadataText("version"),
id: metadataText("id", metadataNamespace),
version: metadataText("version", metadataNamespace),
authors: metadataText("authors"),
description: metadataText("description"),
},
@@ -409,7 +430,11 @@ const parseNuspecDocument = (
commit: attribute(repositoryElement, "commit"),
branch: attribute(repositoryElement, "branch"),
},
dependency_groups: parseDependencies(metadata, limitations),
dependency_groups: parseDependencies(
metadata,
metadataNamespace,
limitations,
),
};
};
+34 -2
View File
@@ -129,11 +129,30 @@ cliTest(
);
cliTest(
"matches CLI and MCP observations for the same .nupkg",
"matches NuGet's field namespace rules through CLI and MCP",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-nupkg-cli-mcp-");
const path = join(root, "Demo.Package.nupkg");
await writeFile(path, await nugetNupkgFixture());
await writeFile(
path,
await nugetNupkgFixture({
nuspec: `<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd" xmlns:ext="urn:example">
<ext:metadata>
<ext:id>Extension</ext:id><id>Demo.Package</id>
<ext:version>9.0.0</ext:version><version>1.2.3</version>
<ext:license type="expression">ISC</ext:license><license type="expression">MIT</license>
<ext:repository type="git" url="https://example.invalid/extension" />
<repository type="git" url="https://example.invalid/declared" />
<ext:authors>Alice</ext:authors><authors>Second author</authors>
<ext:description>First description.</ext:description><description>Second description.</description>
<dependencies>
<ext:group targetFramework="net8.0"><ext:dependency id="Extension" /></ext:group>
<dependency id="Declared" version="1.0" />
</dependencies>
</ext:metadata>
</package>`,
}),
);
const output = await cli.run({
arguments: ["inspect-nuget-nupkg", path, "--json"],
});
@@ -148,6 +167,19 @@ cliTest(
completeNupkgEvidenceSchema.parse(reply.structuredContent)
.normalized_result,
).toEqual(report);
expect(report).toMatchObject({
identity: {
id: "Demo.Package",
version: "1.2.3",
authors: "Alice",
description: "First description.",
},
license: { type: "expression", value: "MIT" },
repository: { url: "https://example.invalid/declared" },
dependency_groups: [
{ target_framework: null, dependencies: [{ id: "Declared" }] },
],
});
},
);