Skip to content

Commit

Permalink
feat: retain go package info when no module declared
Browse files Browse the repository at this point in the history
Signed-off-by: Weston Steimel <[email protected]>
  • Loading branch information
westonsteimel committed Feb 28, 2023
1 parent 98e737f commit 22e0765
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions syft/pkg/cataloger/golang/parse_go_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 22e0765

Please sign in to comment.