From 36c81ed69b2279f562bbd511a3bdec49ee488816 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Thu, 19 Dec 2024 12:55:12 +0200 Subject: [PATCH] Refactor AppendError to check for build.NoGoError (#1273) --- analyzer.go | 4 +++- analyzer_test.go | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/analyzer.go b/analyzer.go index ba2cb6fdb9..186cc3c258 100644 --- a/analyzer.go +++ b/analyzer.go @@ -16,6 +16,7 @@ package gosec import ( + "errors" "fmt" "go/ast" "go/build" @@ -543,7 +544,8 @@ func (gosec *Analyzer) ParseErrors(pkg *packages.Package) error { // AppendError appends an error to the file errors func (gosec *Analyzer) AppendError(file string, err error) { // Do not report the error for empty packages (e.g. files excluded from build with a tag) - if strings.Contains(err.Error(), "no buildable Go source files in") { + var noGoErr *build.NoGoError + if errors.As(err, &noGoErr) { return } errors := make([]Error, 0) diff --git a/analyzer_test.go b/analyzer_test.go index 153bc27525..00c7cc4e7d 100644 --- a/analyzer_test.go +++ b/analyzer_test.go @@ -16,6 +16,7 @@ package gosec_test import ( "errors" + "go/build" "log" "regexp" "strings" @@ -1311,7 +1312,10 @@ var _ = Describe("Analyzer", func() { Context("when appending errors", func() { It("should skip error for non-buildable packages", func() { - analyzer.AppendError("test", errors.New(`loading file from package "pkg/test": no buildable Go source files in pkg/test`)) + err := &build.NoGoError{ + Dir: "pkg/test", + } + analyzer.AppendError("test", err) _, _, errors := analyzer.Report() Expect(errors).To(BeEmpty()) })