Skip to content

Commit 36c81ed

Browse files
authoredDec 19, 2024··
Refactor AppendError to check for build.NoGoError (#1273)
1 parent 9a2d74f commit 36c81ed

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed
 

‎analyzer.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package gosec
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"go/ast"
2122
"go/build"
@@ -543,7 +544,8 @@ func (gosec *Analyzer) ParseErrors(pkg *packages.Package) error {
543544
// AppendError appends an error to the file errors
544545
func (gosec *Analyzer) AppendError(file string, err error) {
545546
// Do not report the error for empty packages (e.g. files excluded from build with a tag)
546-
if strings.Contains(err.Error(), "no buildable Go source files in") {
547+
var noGoErr *build.NoGoError
548+
if errors.As(err, &noGoErr) {
547549
return
548550
}
549551
errors := make([]Error, 0)

‎analyzer_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package gosec_test
1616

1717
import (
1818
"errors"
19+
"go/build"
1920
"log"
2021
"regexp"
2122
"strings"
@@ -1311,7 +1312,10 @@ var _ = Describe("Analyzer", func() {
13111312

13121313
Context("when appending errors", func() {
13131314
It("should skip error for non-buildable packages", func() {
1314-
analyzer.AppendError("test", errors.New(`loading file from package "pkg/test": no buildable Go source files in pkg/test`))
1315+
err := &build.NoGoError{
1316+
Dir: "pkg/test",
1317+
}
1318+
analyzer.AppendError("test", err)
13151319
_, _, errors := analyzer.Report()
13161320
Expect(errors).To(BeEmpty())
13171321
})

0 commit comments

Comments
 (0)
Please sign in to comment.