Skip to content

Commit

Permalink
Validate empty package before analysing
Browse files Browse the repository at this point in the history
  • Loading branch information
daemon1024 committed Oct 6, 2021
1 parent 331d232 commit fe26f9b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions hack/tools/logcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"fmt"
"go/ast"
"go/token"
"os"
"strings"

"golang.org/x/exp/utf8string"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/singlechecker"
"golang.org/x/tools/go/packages"
)

type config struct {
Expand All @@ -34,6 +36,24 @@ type config struct {
}

func main() {
pkgs, _ := packages.Load(nil, os.Args[1:]...)
// count of "no Go files" errors
goFileErrorsCount := 0
packages.Visit(pkgs, nil, func(pkg *packages.Package) {

for _, err := range pkg.Errors {
fmt.Fprintln(os.Stderr, err)
if strings.Contains(err.Msg, "no Go files") {
goFileErrorsCount++
continue
}
os.Exit(1)
}
})

if goFileErrorsCount > 0 { // if there are "no Go files" errors, don't exec analyzer
return
}
singlechecker.Main(analyser())
}

Expand Down

0 comments on commit fe26f9b

Please sign in to comment.