Skip to content

Commit

Permalink
refactor: remove deep-exit in lint/linter.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Dec 11, 2024
1 parent 1fe025e commit 84fd22d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}()

Expand Down
11 changes: 11 additions & 0 deletions revivelib/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package revivelib

import (
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 84fd22d

Please sign in to comment.