Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AppendError to check for build.NoGoError #1273

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package gosec

import (
"errors"
"fmt"
"go/ast"
"go/build"
Expand Down Expand Up @@ -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) {
ccojocar marked this conversation as resolved.
Show resolved Hide resolved
return
}
errors := make([]Error, 0)
Expand Down
6 changes: 5 additions & 1 deletion analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gosec_test

import (
"errors"
"go/build"
"log"
"regexp"
"strings"
Expand Down Expand Up @@ -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())
})
Expand Down
Loading