Skip to content

Commit 4be4794

Browse files
committed
don't stop megacheck on main packages
even if the main package does not compile, analyze a program by megacheck
1 parent 014a924 commit 4be4794

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

pkg/golinters/megacheck.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,44 @@ func prettifyCompilationError(err packages.Error) error {
8282
return errors.New(errText)
8383
}
8484

85-
func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
86-
if len(lintCtx.NotCompilingPackages) != 0 {
87-
var errPkgs []string
88-
var errors []packages.Error
89-
for _, p := range lintCtx.NotCompilingPackages {
90-
errPkgs = append(errPkgs, p.String())
91-
errors = append(errors, libpackages.ExtractErrors(p, lintCtx.ASTCache)...)
85+
func (m Megacheck) canAnalyze(lintCtx *linter.Context) bool {
86+
if len(lintCtx.NotCompilingPackages) == 0 {
87+
return true
88+
}
89+
90+
var errPkgs []string
91+
var errors []packages.Error
92+
for _, p := range lintCtx.NotCompilingPackages {
93+
if p.Name == "main" {
94+
// megacheck crashes on not compiling packages but main packages
95+
// aren't reachable by megacheck: other packages can't depend on them.
96+
continue
9297
}
9398

94-
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s",
95-
errPkgs)
96-
if len(errors) != 0 {
97-
warnText += fmt.Sprintf(": %s", prettifyCompilationError(errors[0]))
98-
if len(errors) > 1 {
99-
const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
100-
warnText += fmt.Sprintf(" and %d more errors: run `%s` to see all errors", len(errors)-1, runCmd)
101-
}
99+
errPkgs = append(errPkgs, p.String())
100+
errors = append(errors, libpackages.ExtractErrors(p, lintCtx.ASTCache)...)
101+
}
102+
103+
if len(errPkgs) == 0 { // only main packages do not compile
104+
return true
105+
}
106+
107+
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s", errPkgs)
108+
if len(errors) != 0 {
109+
warnText += fmt.Sprintf(": %s", prettifyCompilationError(errors[0]))
110+
if len(errors) > 1 {
111+
const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
112+
warnText += fmt.Sprintf(" and %d more errors: run `%s` to see all errors", len(errors)-1, runCmd)
102113
}
103-
lintCtx.Log.Warnf("%s", warnText)
114+
}
115+
lintCtx.Log.Warnf("%s", warnText)
104116

105-
// megacheck crashes if there are not compiling packages
117+
// megacheck crashes if there are not compiling packages
118+
return false
119+
}
120+
121+
func (m Megacheck) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
122+
if !m.canAnalyze(lintCtx) {
106123
return nil, nil
107124
}
108125

0 commit comments

Comments
 (0)