Skip to content

Commit 020c948

Browse files
committed
Analyze project even if fatal import dir error
Before that fix golangci-lint couldn't run analysis if in any dir there was go file with corrupted `package` or `import` statement. After that fix it will only warn about such files and continue analysis. But it will fail analysis after finding 10 packages with such errors to not being too noisy in case of internal error.
1 parent 5514c43 commit 020c948

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Diff for: pkg/packages/resolver.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type Resolver struct {
2121
skippedDirs []string
2222
log logutils.Log
2323

24-
wd string // working directory
24+
wd string // working directory
25+
importErrorsOccured int // count of errors because too bad files in packages
2526
}
2627

2728
func NewResolver(buildTags, excludeDirs []string, log logutils.Log) (*Resolver, error) {
@@ -79,7 +80,7 @@ func (r *Resolver) resolveRecursively(root string, prog *Program) error {
7980

8081
fis, err := ioutil.ReadDir(root)
8182
if err != nil {
82-
return fmt.Errorf("can't resolve dir %s: %s", root, err)
83+
return fmt.Errorf("can't read dir %s: %s", root, err)
8384
}
8485
// TODO: pass cached fis to build.Context
8586

@@ -113,7 +114,7 @@ func (r *Resolver) resolveRecursively(root string, prog *Program) error {
113114
return nil
114115
}
115116

116-
func (r Resolver) resolveDir(dir string, prog *Program) error {
117+
func (r *Resolver) resolveDir(dir string, prog *Program) error {
117118
// TODO: fork build.Import to reuse AST parsing
118119
bp, err := prog.bctx.ImportDir(dir, build.ImportComment|build.IgnoreVendor)
119120
if err != nil {
@@ -122,7 +123,14 @@ func (r Resolver) resolveDir(dir string, prog *Program) error {
122123
return nil
123124
}
124125

125-
return fmt.Errorf("can't resolve dir %s: %s", dir, err)
126+
err = fmt.Errorf("can't import dir %q: %s", dir, err)
127+
r.importErrorsOccured++
128+
if r.importErrorsOccured >= 10 {
129+
return err
130+
}
131+
132+
r.log.Warnf("Can't analyze dir %q: %s", dir, err)
133+
return nil
126134
}
127135

128136
pkg := Package{

0 commit comments

Comments
 (0)