From 1f7f92b8c480bfb10a03859cefcc39b875e63077 Mon Sep 17 00:00:00 2001 From: "Yuan (Bob) Gong" <4957653+Bobgy@users.noreply.github.com> Date: Tue, 12 Apr 2022 09:12:21 +0800 Subject: [PATCH] fix: segmentation fault panic when go modules info is missing (#129) --- licenses/library.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/licenses/library.go b/licenses/library.go index bbfd290..af792bc 100644 --- a/licenses/library.go +++ b/licenses/library.go @@ -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) { @@ -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) @@ -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 {