Skip to content

Commit 696f2ae

Browse files
fix: Golang version parsing from binaries w/GOEXPERIMENT (#6696)
1 parent 9d26ae8 commit 696f2ae

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

pkg/dependency/parser/golang/binary/parse.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
5252
return nil, nil, convertError(err)
5353
}
5454

55+
// Ex: "go1.22.3 X:boringcrypto"
56+
stdlibVersion := strings.TrimPrefix(info.GoVersion, "go")
57+
stdlibVersion, _, _ = strings.Cut(stdlibVersion, " ")
58+
5559
ldflags := p.ldFlags(info.Settings)
5660
pkgs := make(ftypes.Packages, 0, len(info.Deps)+2)
5761
pkgs = append(pkgs, []ftypes.Package{
@@ -69,7 +73,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
6973
{
7074
// Add the Go version used to build this binary.
7175
Name: "stdlib",
72-
Version: strings.TrimPrefix(info.GoVersion, "go"),
76+
Version: stdlibVersion,
7377
Relationship: ftypes.RelationshipDirect, // Considered a direct dependency as the main module depends on the standard packages.
7478
},
7579
}...)

pkg/dependency/parser/golang/binary/parse_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ func TestParse(t *testing.T) {
114114
},
115115
},
116116
},
117+
{
118+
name: "goexperiment",
119+
inputFile: "testdata/goexperiment",
120+
want: []ftypes.Package{
121+
{
122+
Name: "",
123+
Version: "",
124+
Relationship: ftypes.RelationshipRoot,
125+
},
126+
{
127+
Name: "stdlib",
128+
Version: "1.22.1",
129+
Relationship: ftypes.RelationshipDirect,
130+
},
131+
},
132+
},
117133
{
118134
name: "sad path",
119135
inputFile: "testdata/dummy",
Binary file not shown.

0 commit comments

Comments
 (0)