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 <weston.steimel@anchore.com>
  • Loading branch information
westonsteimel committed Feb 28, 2023
1 parent 98e737f commit 5e5f58e
Show file tree
Hide file tree
Showing 2 changed files with 14 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
2 changes: 2 additions & 0 deletions syft/pkg/cataloger/golang/scan_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func scanFile(reader unionreader.UnionReader, filename string) ([]*debug.BuildIn
builds = append(builds, bi)
}

log.Debug(filename)

archs := getArchs(readers, builds)

return builds, archs
Expand Down

0 comments on commit 5e5f58e

Please sign in to comment.