From 84fd22db57cf7cf440a236fdfffbaa791fb4103d Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:30:37 +0100 Subject: [PATCH] refactor: remove deep-exit in lint/linter.go --- lint/linter.go | 21 ++++++++++++--------- revivelib/core.go | 11 +++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lint/linter.go b/lint/linter.go index 6ecaa8da1..e038c5155 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -10,10 +10,10 @@ import ( "regexp" "strconv" "strings" - "sync" goversion "github.com/hashicorp/go-version" "golang.org/x/mod/modfile" + "golang.org/x/sync/errgroup" ) // ReadFile defines an abstraction for reading files. @@ -101,20 +101,23 @@ func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-cha perPkgVersions[n] = v } - var wg sync.WaitGroup - wg.Add(len(packages)) + var wg errgroup.Group for n := range packages { - go func(pkg []string, gover *goversion.Version) { + wg.Go(func() error { + pkg := packages[n] + gover := perPkgVersions[n] if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil { - fmt.Fprintln(os.Stderr, "error during linting: "+err.Error()) - os.Exit(1) + return fmt.Errorf("error during linting: %w", err) } - wg.Done() - }(packages[n], perPkgVersions[n]) + return nil + }) } go func() { - wg.Wait() + err := wg.Wait() + if err != nil { + failures <- NewInternalFailure(err.Error()) + } close(failures) }() diff --git a/revivelib/core.go b/revivelib/core.go index 31463fe64..f81e348fe 100644 --- a/revivelib/core.go +++ b/revivelib/core.go @@ -2,6 +2,7 @@ package revivelib import ( + "errors" "fmt" "log" "os" @@ -137,7 +138,13 @@ func (r *Revive) Format( exitCode := 0 + var errInternal error for failure := range failuresChan { + if failure.IsInternal() { + errInternal = errors.New(failure.Failure) + break + } + if failure.Confidence < conf.Confidence { continue } @@ -160,6 +167,10 @@ func (r *Revive) Format( close(formatChan) <-exitChan + if errInternal != nil { + return "", exitCode, fmt.Errorf("internal error: %w", errInternal) + } + if formatErr != nil { return "", exitCode, fmt.Errorf("formatting: %w", err) }