From 34b7356fd2e00c3577cedf5c26387e42d4b2ee38 Mon Sep 17 00:00:00 2001
From: RRFZ <247457381+Plastic-time@users.noreply.github.com>
Date: Thu, 19 Nov 2026 15:17:57 -0500
Subject: [PATCH] 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>
---
src/nuget/NugetNupkgParser.test.ts | 18 +++++++++
src/nuget/NugetNupkgParser.ts | 55 +++++++++++++++++++-------
tests/acceptance/nugetNupkgCli.test.ts | 36 ++++++++++++++++-
3 files changed, 92 insertions(+), 17 deletions(-)
diff --git a/src/nuget/NugetNupkgParser.test.ts b/src/nuget/NugetNupkgParser.test.ts
index b2a7ef34a1..67afda3be1 100644
--- a/src/nuget/NugetNupkgParser.test.ts
+++ b/src/nuget/NugetNupkgParser.test.ts
@@ -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: `
+
+ Demo.Package1.0.0
+
+
+
+
+
+ `,
+ });
+ const result = await parseNugetNupkg(bytes, artifactOf(bytes), defaults);
+ expect(result.dependency_groups).toMatchObject([
+ { target_framework: null, dependencies: [{ id: "Declared" }] },
+ ]);
+});
diff --git a/src/nuget/NugetNupkgParser.ts b/src/nuget/NugetNupkgParser.ts
index 42292de467..69539615b0 100644
--- a/src/nuget/NugetNupkgParser.ts
+++ b/src/nuget/NugetNupkgParser.ts
@@ -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 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,
+ ),
};
};
diff --git a/tests/acceptance/nugetNupkgCli.test.ts b/tests/acceptance/nugetNupkgCli.test.ts
index c4e53ab1a7..37ffc3fdd3 100644
--- a/tests/acceptance/nugetNupkgCli.test.ts
+++ b/tests/acceptance/nugetNupkgCli.test.ts
@@ -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: `
+
+ ExtensionDemo.Package
+ 9.0.01.2.3
+ ISCMIT
+
+
+ AliceSecond author
+ First description.Second description.
+
+
+
+
+
+ `,
+ }),
+ );
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" }] },
+ ],
+ });
},
);