From bcc0751a4027f77e5c4db1f1ce4a558727a1c981 Mon Sep 17 00:00:00 2001 From: Weston Steimel Date: Wed, 1 Mar 2023 14:26:44 +0000 Subject: [PATCH] feat: retain go package info when no module declared (#1632) Signed-off-by: Weston Steimel --- syft/pkg/cataloger/golang/parse_go_binary.go | 14 +++++-- .../cataloger/golang/parse_go_binary_test.go | 42 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/syft/pkg/cataloger/golang/parse_go_binary.go b/syft/pkg/cataloger/golang/parse_go_binary.go index 80008bd3a10..bc2f033ae27 100644 --- a/syft/pkg/cataloger/golang/parse_go_binary.go +++ b/syft/pkg/cataloger/golang/parse_go_binary.go @@ -179,12 +179,23 @@ func getBuildSettings(settings []debug.BuildSetting) map[string]string { return m } +func createMainModuleFromPath(path string) (mod debug.Module) { + mod.Path = path + mod.Version = devel + return +} + func buildGoPkgInfo(location source.Location, mod *debug.BuildInfo, arch string) []pkg.Package { var pkgs []pkg.Package if mod == nil { return pkgs } + var empty debug.Module + if mod.Main == empty && mod.Path != "" { + mod.Main = createMainModuleFromPath(mod.Path) + } + for _, dep := range mod.Deps { if dep == nil { continue @@ -195,9 +206,6 @@ func buildGoPkgInfo(location source.Location, mod *debug.BuildInfo, arch string) } } - // NOTE(jonasagx): this use happened originally while creating unit tests. It might never - // happen in the wild, but I kept it as a safeguard against empty modules. - var empty debug.Module if mod.Main == empty { return pkgs } diff --git a/syft/pkg/cataloger/golang/parse_go_binary_test.go b/syft/pkg/cataloger/golang/parse_go_binary_test.go index acfd435c59a..4cf7fb0fc5d 100644 --- a/syft/pkg/cataloger/golang/parse_go_binary_test.go +++ b/syft/pkg/cataloger/golang/parse_go_binary_test.go @@ -242,6 +242,48 @@ func TestBuildGoPkgInfo(t *testing.T) { }, }, }, + { + name: "parse a mod with path but no main module", + arch: archDetails, + mod: &debug.BuildInfo{ + GoVersion: goCompiledVersion, + Settings: []debug.BuildSetting{ + {Key: "GOARCH", Value: archDetails}, + {Key: "GOOS", Value: "darwin"}, + {Key: "GOAMD64", Value: "v1"}, + }, + Path: "github.com/a/b/c", + }, + expected: []pkg.Package{ + { + Name: "github.com/a/b/c", + Version: "(devel)", + PURL: "pkg:golang/github.com/a/b/c@(devel)", + Language: pkg.Go, + Type: pkg.GoModulePkg, + Locations: source.NewLocationSet( + source.Location{ + Coordinates: source.Coordinates{ + RealPath: "/a-path", + FileSystemID: "layer-id", + }, + }, + ), + MetadataType: pkg.GolangBinMetadataType, + Metadata: pkg.GolangBinMetadata{ + GoCompiledVersion: goCompiledVersion, + Architecture: archDetails, + H1Digest: "", + BuildSettings: map[string]string{ + "GOAMD64": "v1", + "GOARCH": "amd64", + "GOOS": "darwin", + }, + MainModule: "github.com/a/b/c", + }, + }, + }, + }, { name: "parse a mod without packages", arch: archDetails,