Skip to content

refactor: modifies linting machinery to use Failure as a mean to signal erros in rules application #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lint/failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ type Failure struct {
func (f *Failure) GetFilename() string {
return f.Position.Start.Filename
}

const internalFailure = "REVIVE_INTERNAL"

// IsInternal returns true if this failure is internal, false otherwise.
func (f *Failure) IsInternal() bool {
return f.Category == internalFailure
}

// NewInternalFailure yields an internal failure with the given message as failure message.
func NewInternalFailure(message string) Failure {
return Failure{
Category: internalFailure,
Failure: message,
}
}
7 changes: 6 additions & 1 deletion lint/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lint

import (
"bytes"
"errors"
"go/ast"
"go/parser"
"go/printer"
Expand Down Expand Up @@ -96,7 +97,7 @@ func (f *File) isMain() bool {

const directiveSpecifyDisableReason = "specify-disable-reason"

func (f *File) lint(rules []Rule, config Config, failures chan Failure) {
func (f *File) lint(rules []Rule, config Config, failures chan Failure) error {
rulesConfig := config.Rules
_, mustSpecifyDisableReason := config.Directives[directiveSpecifyDisableReason]
disabledIntervals := f.disabledIntervals(rules, mustSpecifyDisableReason, failures)
Expand All @@ -107,6 +108,9 @@ func (f *File) lint(rules []Rule, config Config, failures chan Failure) {
}
currentFailures := currentRule.Apply(f, ruleConfig.Arguments)
for idx, failure := range currentFailures {
if failure.IsInternal() {
return errors.New(failure.Failure)
}
if failure.RuleName == "" {
failure.RuleName = currentRule.Name()
}
Expand All @@ -122,6 +126,7 @@ func (f *File) lint(rules []Rule, config Config, failures chan Failure) {
}
}
}
return nil
}

type enableDisableConfig struct {
Expand Down
4 changes: 1 addition & 3 deletions lint/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS
return nil
}

pkg.lint(ruleSet, config, failures)

return nil
return pkg.lint(ruleSet, config, failures)
}

func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) {
Expand Down
25 changes: 21 additions & 4 deletions lint/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,34 @@ func (p *Package) scanSortable() {
}
}

func (p *Package) lint(rules []Rule, config Config, failures chan Failure) {
func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error {
p.scanSortable()
var wg sync.WaitGroup
errChan := make(chan error)
doneChan := make(chan struct{})

wg.Add(len(p.files))
go func() { // This goroutine will signal when all files where linted
wg.Wait()
doneChan <- struct{}{}
}()

for _, file := range p.files {
wg.Add(1)
go (func(file *File) {
file.lint(rules, config, failures)
err := file.lint(rules, config, failures)
if err != nil {
errChan <- err // signal the error
}
wg.Done()
})(file)
}
wg.Wait()

select { // We block until...
case <-doneChan: //...all files were linted
return nil
case err := <-errChan: //...or there is an error
return err
}
Copy link
Contributor

@ccoVeille ccoVeille Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using

https://pkg.go.dev/golang.org/x/sync/errgroup

The problems I see with your code are:

    • the channels make it complicated
    • errgroup exists
    • all linters will be configured, no matter if one fail
    • but you will return on first error
    • so the go routines keeps being called for nothing

I would have said why not, if each errors was reported or returned

Of course, this feedback is based on my understanding of the code. I can be wrong 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback
(I've edited your comment to add numbers to the list of problems)
My meta-feedback:

  1. Fair (even if I personally prefer explicit code over "hidden magic")
  2. True (more on errorgroup below)
  3. Not necessary all but I get the point
  4. True
  5. On error, control exits the whole program thus gorutines will die.

On errgroup:

  1. as used in the PR refactor: replace panic with error in rules #1126, errgroup does not necessarily prevent for configuring "all linters, no matter if one fail"
revive -config defaults.toml ./...
>>>>> configure addconstant
>>>>> configure arguments limits
Error during linting:Invalid argument to the add-constant rule, expecting a k,v map. Got string
  1. as used in the PR refactor: replace panic with error in rules #1126, errgroup does not necessarily prevent for "goroutines keep being called"
    (each >>>> linting ... is a goroutine)
revive -config defaults.toml ./...
>>>>>> linting file  internal/typeparams/typeparams.go
>>>>>> linting file  internal/typeparams/typeparams_go118.go
>>>>> configure addconstant
>>>>>> linting file  config/config.go
>>>>>> linting file  config/config_test.go
>>>>>> linting file  internal/ifelse/branch.go
>>>>>> linting file  internal/ifelse/branch_kind.go
>>>>>> linting file  internal/ifelse/chain.go
>>>>>> linting file  internal/ifelse/doc.go
>>>>>> linting file  internal/ifelse/func.go
>>>>>> linting file  internal/ifelse/rule.go
>>>>>> linting file  internal/ifelse/target.go
>>>>>> linting file  internal/ifelse/args.go
>>>>>> linting file  main.go
>>>>> configure arguments limits
>>>>> configure banned chars
>>>>>> linting file  formatter/ndjson.go
>>>>>> linting file  formatter/plain.go
>>>>>> linting file  formatter/sarif.go
>>>>>> linting file  formatter/stylish.go
>>>>>> linting file  formatter/default.go
>>>>>> linting file  formatter/doc.go
>>>>>> linting file  formatter/json.go
>>>>>> linting file  formatter/severity.go
>>>>>> linting file  formatter/unix.go
>>>>>> linting file  formatter/checkstyle.go
>>>>>> linting file  formatter/friendly.go
>>>>>> linting file  internal/astutils/ast_utils.go
>>>>>> linting file  cli/main.go
>>>>>> linting file  cli/main_test.go
>>>>>> linting file  lint/doc.go
>>>>>> linting file  lint/file.go
>>>>>> linting file  lint/formatter.go
>>>>>> linting file  lint/package.go
>>>>>> linting file  lint/linter_test.go
>>>>>> linting file  lint/name_test.go
>>>>>> linting file  lint/config.go
>>>>>> linting file  lint/failure.go
>>>>>> linting file  lint/filefilter.go
>>>>>> linting file  lint/linter.go
>>>>>> linting file  lint/name.go
>>>>>> linting file  lint/rule.go
Error during linting:Invalid argument to the add-constant rule, expecting a k,v map. Got string

To resume, errgroup is (subjectively) simpler but still shares the same drawbacks of explicitly using channels. (and to be fair, these drawbacks are also present in the current -with panics- implementation of the linting machinery)

Anyway, the single significant point of my proposal is: use the already available failure mechanism instead of adding an error return value to rules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your detailed reply and feedback.

I agree with your conclusion

👍

}

// IsAtLeastGo121 returns true if the Go version for this package is 1.21 or higher, false otherwise
Expand Down
27 changes: 17 additions & 10 deletions rule/add_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ type AddConstantRule struct {

// Apply applies the rule to given file.
func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configureOnce.Do(func() { r.configure(arguments) })
var configureErr error
r.configureOnce.Do(func() { configureErr = r.configure(arguments) })
if configureErr != nil {
return []lint.Failure{lint.NewInternalFailure(configureErr.Error())}
}
Comment on lines +46 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This idea is great, but it doesn't address this

#1126 (comment)

But it could be addressed separately

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know. It is not my intention to address this problem right now.
IMO configureOnce is cumbersome (vs just lock/unlock) for dealing with errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree

Maybe linter could call configure if there is a Configure interface on the Rule

But, some tests will need to be rewritten

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've studied before the alternative of adding a Configure function to rules: it simplifies configuration of rules but it has the drawback of changing the interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was to add an interface for Configure only.

type Configure interface {
Configure() error
}

If detected, you call it after a type assertion.

But it's maybe part of what you considered

Copy link
Contributor

@mfederowicz mfederowicz Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok @chavacava nice concept, can you deploy that changes for lint/* files without add_constant.go rule, and in #1126 I do the rest of job (add []lint.Failure{lint.NewInternalFailure(configureErr.Error())} where it is needed) :P


var failures []lint.Failure

Expand Down Expand Up @@ -201,15 +205,16 @@ func (w *lintAddConstantRule) isStructTag(n *ast.BasicLit) bool {
return ok
}

func (r *AddConstantRule) configure(arguments lint.Arguments) {
func (r *AddConstantRule) configure(arguments lint.Arguments) error {
println(">>>> configuring")
r.strLitLimit = defaultStrLitLimit
r.allowList = newAllowList()
if len(arguments) == 0 {
return
return nil
}
args, ok := arguments[0].(map[string]any)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting a k,v map. Got %T", arguments[0]))
return fmt.Errorf("invalid argument to the add-constant rule, expecting a k,v map. Got %T", arguments[0])
}
for k, v := range args {
kind := ""
Expand All @@ -228,39 +233,41 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) {
}
list, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v))
fmt.Errorf("invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v)
}
r.allowList.add(kind, list)
case "maxLitCount":
sl, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v))
fmt.Errorf("invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v' (%T)", v, v)
}

limit, err := strconv.Atoi(sl)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v))
fmt.Errorf("invalid argument to the add-constant rule, expecting string representation of an integer. Got '%v'", v)
}
r.strLitLimit = limit
case "ignoreFuncs":
excludes, ok := v.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v))
fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule, string expected. Got '%v' (%T)", v, v)
}

for _, exclude := range strings.Split(excludes, ",") {
exclude = strings.Trim(exclude, " ")
if exclude == "" {
panic("Invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.")
fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule, expected regular expression must not be empty.")
}

exp, err := regexp.Compile(exclude)
if err != nil {
panic(fmt.Sprintf("Invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err))
fmt.Errorf("invalid argument to the ignoreFuncs parameter of add-constant rule: regexp %q does not compile: %v", exclude, err)
}

r.ignoreFunctions = append(r.ignoreFunctions, exp)
}
}
}

return nil
}
Loading