Skip to content

fix(dotnet): resolve packages.lock dependencies deterministically - #5210

Merged
wagoodman merged 8 commits into
mainfrom
dotnet-packages-lock-per-tfm-deps
Oct 1, 2026
Merged

wagoodman merged 8 commits into
mainfrom
dotnet-packages-lock-per-tfm-deps

Conversation

@spiffcs

@spiffcs spiffcs commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

packages.lock.json can group packages by target framework.

The parser flattened those groups when it ranged over Go maps.

For multi-targeted projects the SBOM depended on map iteration order causing document instability.

Changes

  • The lockfile is normalized into target frameworks sorted by name, each with its packages sorted
    by name. Packages and relationships are both derived from it.
  • When frameworks disagree on a package's type, a fixed precedence decides:
    Project > Direct > CentralTransitive > Transitive (unknown types last).
  • Edges resolve within their declaring framework (case-insensitive, since NuGet IDs are).
    Runtime-specific sections (net8.0/win-x64) fall back to their base framework. Unresolvable
    edges are dropped with a debug log instead of guessed from another framework.
  • Edges repeated across frameworks are deduplicated.

Not in scope (remains open in #5211)

  • requested is not captured.
  • A package Direct in any framework is reported only as Direct; per-framework types are lost.

Testing

  • TestParseDotnetPackagesLock_multipleTargetFrameworks: per-framework version resolution,
    Direct-wins, and cross-framework edge dedup
  • Test_findDependencyPkg: resolution precedence and the lowest-version fallback

Refs #5211. Follow-up to #5143.

…ramework

Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
… target frameworks

Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
@spiffcs
spiffcs force-pushed the dotnet-packages-lock-per-tfm-deps branch from ac8570f to c60cb64 Compare August 21, 2026 20:21

@CAOShurong CAOShurong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

End-to-end verification on the current head (c60cb64), Windows, syft built from the exact head with the issue's multi-target scenario (Newtonsoft.Json Direct under net8.0 / Transitive under netstandard2.0, Serilog pulled transitively via MyLogAdapter):

  • determinism, 100 scans of the same fixture: main flips (88 Direct / 12 Transitive), this head is 100/100 "Direct" — the reported nondeterminism is gone and Direct-wins is stable;
  • relationships resolve per-framework correctly and dedupe across frameworks: the fixture yields exactly one "Serilog dependency-of MyLogAdapter" edge, and Serilog itself is reported Transitive as every framework sees it;
  • package order and relationship order are stable across runs.

One scope note (matches what the PR already implies): metadata.requested from the lockfile is still not surfaced (metadata exposes name/version/type/contentHash only), so the second observation in #5211 remains open beyond this PR — worth a tracking note so it isn't lost when this merges.

@CAOShurong

Copy link
Copy Markdown
Contributor

Independent end-to-end verification of #5210 (maintainer fix for #5211), run 2026-08-24 ~02:20+08:00.

Setup

  • Repro from the issue: a packages.lock.json where Newtonsoft.Json 13.0.3 is Direct under net8.0 and Transitive under netstandard2.0, scanned with syft scan dir:. --select-catalogers dotnet -o syft-json.
  • Baseline = local build of main @ bf82010f (Windows/amd64, go1.26.6). Candidate = local build of PR head c60cb640. Both built clean; the parser file is untouched between them, so the baseline reflects upstream behavior.
  • Parser unit tests were run in a throwaway worktree at the PR head; it has been removed (the repo working tree is back on our own branch, clean).

Baseline (main): nondeterminism confirmed on Windows too

60 scans of the byte-identical input:

  • 50 runs → "type": "Direct"
  • 10 runs → "type": "Transitive"

This reproduces the issue's flip-rate (~17% here vs ~16% in the issue) outside Linux, so the randomized-map-order root cause is platform-independent. Also confirmed on main: metadata.requested is dropped entirely — the output metadata carries only name/version/contentHash/type.

Candidate (#5210 head c60cb64): deterministic

Same loop, same machine: 60/60 runs → "type": "Direct", zero flips across repeated invocations of separate processes (each run is a fresh process, so map iteration order differs per run).

Tests

  • go test ./syft/pkg/cataloger/dotnet/ -run 'TestParseDotnetPackagesLock' including the new TestParseDotnetPackagesLock_multipleTargetFrameworks: PASS, also PASS at -count=20.
  • The three failures elsewhere in that package (TestCataloger_Globs, TestCataloger, TestDotnetDepsCataloger_regressions/libman_support) are environment-only: all three fail identically on unmodified main because they shell out to docker, which isn't installed here. They are unrelated to this change.

One observation, not a blocker

The fix resolves the determinism and Direct-preference halves of the issue cleanly. The lossiness half remains open by design: after stabilization the SBOM reports only "type": "Direct" for Newtonsoft.Json even though one target framework lists it as Transitive, and requested still isn't captured. The issue itself frames this as acceptable ("we can say syft doesn't support the transitive entries for this cataloger yet"), so this looks intentional for now — flagging only so the tradeoff is on record from an independent run.

Nice-to-have if you want belt-and-suspenders coverage: a test asserting that two parses of the multi-framework fixture produce identical package metadata ordering, which would catch any future regression into map-order dependence even before golden-file comparison.

Verified as an independent third party; no code changes proposed — the PR looks correct and ready from this side.

spiffcs and others added 6 commits September 30, 2026 15:23
* main: (91 commits)
  feat: catalog CPAN distributions installed by perl clients (#5131)
  fix(binary): correct traefik version detection on big-endian arches (#5281)
  Fix hang when scanning a Java resource adapter (.rar) file (#5348)
  chore(deps): update SPDX license list (#5315)
  fix: match opensource.org license URLs across SPDX URL forms (#5361)
  Speed up unit tests in CI (#5360)
  Fix missing file hashes when running on windows system (#5341)
  Stop reading the mount table for every single-file source (#5257)
  fix: conan expat CPE (#5259)
  fix(spdx): write a name for the root package when the source has none (#5349)
  fix(python): link uv.lock dependents to the locked version they name (#5351)
  fix: suppress misleading upstream CPE for PHP extension binaries (#5102)
  fix: report scanner errors and raise the line cap in metadata parsers (#5353)
  Harden cataloger parsers against truncated input (do not panic) (#5355)
  fix(cpe): bound CPE candidate generation for pathological package metadata (#5356)
  test(golang): make the UPX fixture exercise loader padding (#5354)
  fix(golang): account for UPX's loader padding when unpacking Go binaries (#5347)
  test(snap): bound the kernel changelog bomb fixture by ratio, not a fixed 1MB (#5352)
  test(debian): bound the bomb fixture by ratio, not a fixed 1MB (#5350)
  fix(ai): handle malformed GGUF headers without panicking (#5345)
  ...
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
NuGet IDs are case-insensitive, and edge lookup already treated them that way, but package identity used the exact-case name. Two spellings of one ID and version across target frameworks became two packages, and type precedence never ran between them.

- packages merge on the lowercased name + version, display name comes from the winning entry
- a package listing itself (under any casing) no longer produces a self edge

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
@wagoodman
wagoodman enabled auto-merge (squash) October 1, 2026 21:08
@wagoodman
wagoodman merged commit c33fa32 into main Oct 1, 2026
17 checks passed
@wagoodman
wagoodman deleted the dotnet-packages-lock-per-tfm-deps branch October 1, 2026 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants