Skip to content

Commit 8bf5661

Browse files
committed
support multi module project
1 parent 1285eb7 commit 8bf5661

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

analyzer.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,25 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
333333
return []*packages.Package{}, fmt.Errorf("importing dir %q: %w", pkgPath, err)
334334
}
335335

336-
var packageFiles []string
337-
for _, filename := range basePackage.GoFiles {
338-
packageFiles = append(packageFiles, path.Join(pkgPath, filename))
336+
var goModFile string
337+
goModDir := abspath[0 : len(abspath)-len(pkgPath)] // get root dir
338+
if modPkgs, err := packages.Load(&packages.Config{Mode: packages.NeedModule, Dir: abspath}, abspath); err == nil && len(modPkgs) == 1 {
339+
goModFile = modPkgs[0].Module.GoMod
340+
goModDir = path.Dir(goModFile)
339341
}
340-
for _, filename := range basePackage.CgoFiles {
341-
packageFiles = append(packageFiles, path.Join(pkgPath, filename))
342+
343+
var packageFiles []string
344+
for _, filename := range append(basePackage.GoFiles, basePackage.CgoFiles...) {
345+
if goModDir == "" {
346+
packageFiles = append(packageFiles, path.Join(pkgPath, filename))
347+
} else {
348+
filePath := path.Join(abspath, filename)
349+
relPath, err := filepath.Rel(goModDir, filePath)
350+
if err != nil {
351+
return []*packages.Package{}, fmt.Errorf("get relative path between %q and %q: %w", goModDir, filePath, err)
352+
}
353+
packageFiles = append(packageFiles, relPath)
354+
}
342355
}
343356

344357
if gosec.tests {
@@ -354,6 +367,7 @@ func (gosec *Analyzer) load(pkgPath string, conf *packages.Config) ([]*packages.
354367
gosec.mu.Lock()
355368
conf.BuildFlags = nil
356369
defer gosec.mu.Unlock()
370+
conf.Dir = goModDir
357371
pkgs, err := packages.Load(conf, packageFiles...)
358372
if err != nil {
359373
return []*packages.Package{}, fmt.Errorf("loading files from package %q: %w", pkgPath, err)

0 commit comments

Comments
 (0)