Skip to content

Commit

Permalink
fix: segmentation fault panic when go modules info is missing (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobgy authored Apr 12, 2022
1 parent 0b61a44 commit 1f7f92b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions licenses/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ func Libraries(ctx context.Context, classifier Classifier, importPaths ...string

pkgs := map[string]*packages.Package{}
pkgsByLicense := make(map[string][]*packages.Package)
errorOccurred := false
pkgErrorOccurred := false
otherErrorOccurred := false
packages.Visit(rootPkgs, func(p *packages.Package) bool {
if len(p.Errors) > 0 {
errorOccurred = true
pkgErrorOccurred = true
return false
}
if isStdLib(p) {
Expand All @@ -97,6 +98,11 @@ func Libraries(ctx context.Context, classifier Classifier, importPaths ...string
// This package is empty - nothing to do.
return true
}
if p.Module == nil {
otherErrorOccurred = true
glog.Errorf("Package %s does not have module info. Non go modules projects are no longer supported. For feedback, refer to https://github.com/google/go-licenses/issues/128.", p.PkgPath)
return false
}
licensePath, err := Find(pkgDir, p.Module.Dir, classifier)
if err != nil {
glog.Errorf("Failed to find license for %s: %v", p.PkgPath, err)
Expand All @@ -105,11 +111,14 @@ func Libraries(ctx context.Context, classifier Classifier, importPaths ...string
pkgsByLicense[licensePath] = append(pkgsByLicense[licensePath], p)
return true
}, nil)
if errorOccurred {
if pkgErrorOccurred {
return nil, PackagesError{
pkgs: rootPkgs,
}
}
if otherErrorOccurred {
return nil, fmt.Errorf("some errors occurred when loading direct and transitive dependency packages")
}

var libraries []*Library
for licensePath, pkgs := range pkgsByLicense {
Expand Down

0 comments on commit 1f7f92b

Please sign in to comment.