Skip to content

Commit c6cb3e1

Browse files
committed
fix(dotnet): select deps.json root project deterministically
collectPackages ranged over the libraries map and kept the first library with type "project" as the root, so Go map iteration order decided which assembly was reported when multiple project libraries exist. Iterate sorted library keys so the selection is stable across runs.
1 parent f065203 commit c6cb3e1

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

pkg/dependency/parser/dotnet/core_deps/parse.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core_deps
22

33
import (
44
"context"
5+
"maps"
56
"slices"
67
"sort"
78
"strings"
@@ -104,7 +105,8 @@ func (p *Parser) collectPackages(depsFile dotNetDependencies, targetLibs map[str
104105
var projectNameVer string
105106
pkgs := make(map[string]ftypes.Package, len(depsFile.Libraries))
106107

107-
for nameVer, lib := range depsFile.Libraries {
108+
for _, nameVer := range slices.Sorted(maps.Keys(depsFile.Libraries)) {
109+
lib := depsFile.Libraries[nameVer]
108110
name, version, ok := strings.Cut(nameVer, "/")
109111
if !ok {
110112
// Invalid name

pkg/dependency/parser/dotnet/core_deps/parse_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,20 @@ func TestParse(t *testing.T) {
241241
})
242242
}
243243
}
244+
245+
func TestCollectPackagesDeterministicRoot(t *testing.T) {
246+
depsFile := dotNetDependencies{
247+
Libraries: map[string]dotNetLibrary{
248+
"Zebra/1.0.0": {Type: "project"},
249+
"Mango/1.0.0": {Type: "project"},
250+
"Alpha/1.0.0": {Type: "project"},
251+
},
252+
}
253+
254+
// The lexicographically first project library must win regardless of
255+
// map iteration order.
256+
pkgs, root := NewParser().collectPackages(depsFile, nil, false)
257+
258+
assert.Equal(t, "Alpha/1.0.0", root)
259+
assert.Equal(t, ftypes.RelationshipRoot, pkgs[root].Relationship)
260+
}

0 commit comments

Comments
 (0)