Skip to content
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

refactor: replace panic with error in rules #1126

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

mfederowicz
Copy link
Contributor

@mfederowicz mfederowicz commented Nov 15, 2024

related with PR #1107

changes in areas:

  • refactor rules to return error in r.configure func
  • make changes in almost all rules
  • add return error for Apply func to handle error in upper level (bacause there were panic outisde configure)
  • and other places where panic was used
  • add comment for package rule :)

I am open to suggestion what to change in next steps @chavacava @denisvmedia

@chavacava
Copy link
Collaborator

chavacava commented Nov 16, 2024

As is, the implementation of rules in this branch does not handle errors but put them "under the carpet"
Almost the single situation where rules might err is when reading arguments, therefore rule.Apply implementations shall handle errors returned by calls to r.configure

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

It wasn't fun to review 😅 27 minutes

lint/package.go Outdated Show resolved Hide resolved
lint/package.go Outdated Show resolved Hide resolved
rule/argument_limit.go Outdated Show resolved Hide resolved
rule/enforce_map_style.go Outdated Show resolved Hide resolved
rule/add_constant.go Outdated Show resolved Hide resolved
rule/unchecked_type_assertion.go Outdated Show resolved Hide resolved
rule/unhandled_error.go Outdated Show resolved Hide resolved
rule/unhandled_error.go Outdated Show resolved Hide resolved
rule/unused_param.go Outdated Show resolved Hide resolved
rule/unused_receiver.go Outdated Show resolved Hide resolved
lint/package.go Outdated Show resolved Hide resolved
rule/add_constant.go Outdated Show resolved Hide resolved
# Conflicts:
#	rule/add_constant.go
#	rule/argument_limit.go
#	rule/banned_characters.go
#	rule/cognitive_complexity.go
#	rule/comment_spacings.go
#	rule/comments_density.go
#	rule/context_as_argument.go
#	rule/cyclomatic.go
#	rule/defer.go
#	rule/dot_imports.go
#	rule/enforce_map_style.go
#	rule/enforce_repeated_arg_type_style.go
#	rule/enforce_slice_style.go
#	rule/error_strings.go
#	rule/exported.go
#	rule/file_header.go
#	rule/file_length_limit.go
#	rule/filename_format.go
#	rule/function_length.go
#	rule/function_result_limit.go
#	rule/import_alias_naming.go
#	rule/imports_blocklist.go
#	rule/line_length_limit.go
#	rule/max_control_nesting.go
#	rule/max_public_structs.go
#	rule/receiver_naming.go
#	rule/struct_tag.go
#	rule/unchecked_type_assertion.go
#	rule/unhandled_error.go
#	rule/unused_param.go
#	rule/unused_receiver.go
#	rule/var_naming.go
rule/add_constant.go Outdated Show resolved Hide resolved
rule/early_return.go Outdated Show resolved Hide resolved
@@ -14,7 +14,7 @@ type DisabledInterval struct {
// Rule defines an abstract rule interface
type Rule interface {
Name() string
Apply(*File, Arguments) []Failure
Apply(*File, Arguments) ([]Failure, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't simply add a new return argument to the Apply function due to the backward compatibility. Someone who uses revivelib may depend on this interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok @alexandear any idea how to handle errors without of course excuting panic(), we can use in each Apply:

if err != nil {                                                                                                                                                
   fmt.Fprintln(os.Stderr, err)                                                                         
   os.Exit(1)                                                                                          
   }

but it is not elegant way :(

Copy link
Collaborator

@denisvmedia denisvmedia Nov 19, 2024

Choose a reason for hiding this comment

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

To maintain backwards compatibility we would have to add backwards compatible wrappers around the newly included changes. And one thing that came to my mind, how do these changes fit the use of Revive in golangci-lint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok got it @denisvmedia you want something like that: https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/revive/revive.go#L53 ? but is it possible to do that in separate PR, or it should be done in that PR?

rule/add_constant.go Outdated Show resolved Hide resolved
rule/add_constant.go Outdated Show resolved Hide resolved
lint/linter.go Outdated
@@ -152,7 +152,11 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS
return nil
}

pkg.lint(ruleSet, config, failures)
err := pkg.lint(ruleSet, config, failures)

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I'd remove this line (it will be more consistent stylistically).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@denisvmedia maybe something like this: return pkg.lint(ruleSet, config, failures)?

lint/package.go Outdated Show resolved Hide resolved
Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

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

Please take a look at

#1126 (comment)

For me, it's blocking

@@ -153,7 +153,7 @@ func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleS
}

pkg.lint(ruleSet, config, failures)

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

Comment on lines +44 to +47
check := sync.OnceValue(func() error {return r.configure(arguments)})
if err := check(); err != nil {
return nil, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't work as you expected. The configure will be called multiple times.

You can check this by adding the statement fmt.Println("configure add-constant") inside the func() error. After running revive with enabled add-constant on a real project with multiple packages, there are a lot of lines configure add-constant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants