From 22e076507a22d3ae2f8e4e83d4869641b79422ea Mon Sep 17 00:00:00 2001 From: Weston Steimel Date: Wed, 22 Feb 2023 15:35:40 +0000 Subject: [PATCH] feat: retain go package info when no module declared Signed-off-by: Weston Steimel --- syft/pkg/cataloger/golang/parse_go_binary.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/syft/pkg/cataloger/golang/parse_go_binary.go b/syft/pkg/cataloger/golang/parse_go_binary.go index 80008bd3a10e..826e71c5bed0 100644 --- a/syft/pkg/cataloger/golang/parse_go_binary.go +++ b/syft/pkg/cataloger/golang/parse_go_binary.go @@ -51,6 +51,7 @@ func parseGoBinary(_ source.FileResolver, _ *generic.Environment, reader source. internal.CloseAndLogError(reader.ReadCloser, reader.RealPath) for i, mod := range mods { + log.Info(mod.Path) pkgs = append(pkgs, buildGoPkgInfo(reader.Location, mod, archs[i])...) } return pkgs, nil, nil @@ -179,12 +180,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 +207,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 }