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

Merged
merged 44 commits into from
Dec 11, 2024

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

lint/file.go Outdated Show resolved Hide resolved
@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
lint/rule.go Outdated
@@ -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 Show resolved Hide resolved
lint/package.go Outdated Show resolved Hide resolved
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

@chavacava
Copy link
Collaborator

@mfederowicz thanks for the updates. Please run

revive --config revive.toml ./...

On the branch, you will see that the problem of error messages in uppercase is present in almost all configuration error messages

nit: we could add an utility function (in rules/utils.go ?) to avoid repeating []lint.Failure{lint.NewInternalFailure(configureErr.Error())} at each rule.

lint/file.go Outdated Show resolved Hide resolved
rule/dot_imports.go Outdated Show resolved Hide resolved
rule/dot_imports.go Outdated Show resolved Hide resolved
rule/dot_imports.go Outdated Show resolved Hide resolved
@mfederowicz
Copy link
Contributor Author

ok @chavacava i replaced almost all errors msg in rules, but there are some cases in testdata what about them?

rule/utils.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@chavacava chavacava left a comment

Choose a reason for hiding this comment

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

Some details to adjust but overall OK.
Thanks @mfederowicz !

lint/linter.go Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
@ccoVeille
Copy link
Contributor

ccoVeille commented Dec 9, 2024

You did great, you managed to fix the panic issue.

I will try to open a PR with ideas I have.

  • The errgroup in lint/linter.go (unsure it would work as I expect anyway) to avoid a deep-exit
  • the memorization on configureErr (I'm unsure we need it)

I cannot only suggest things, I have to code sometimes 🤭

Then I will be the one who will take feedbacks from you guys 😅

Copy link
Collaborator

@denisvmedia denisvmedia left a comment

Choose a reason for hiding this comment

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

LGTM

rule/add_constant.go Outdated Show resolved Hide resolved
Comment on lines +39 to +44
var configureErr error
r.configureOnce.Do(func() { configureErr = r.configure(arguments) })

if configureErr != nil {
return newInternalFailureError(configureErr)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I propose adding a test to check a situation where configureErr is not nil. Not for all rules but at least for a one.

rule/enforce_slice_style.go Outdated Show resolved Hide resolved
rule/file_header.go Outdated Show resolved Hide resolved
rule/file_length_limit.go Outdated Show resolved Hide resolved
rule/import_alias_naming.go Outdated Show resolved Hide resolved
rule/import_alias_naming.go Outdated Show resolved Hide resolved
rule/string_format.go Outdated Show resolved Hide resolved
rule/max_public_structs.go Outdated Show resolved Hide resolved
rule/struct_tag.go Outdated Show resolved Hide resolved
@@ -43,10 +47,9 @@ func (StringFormatRule) ParseArgumentsTest(arguments lint.Arguments) *string {
// Parse the arguments in a goroutine, defer a recover() call, return the error encountered (or nil if there was no error)
go func() {
defer func() {
err := recover()
err := w.parseArguments(arguments)
Copy link
Contributor

@alexandear alexandear Dec 9, 2024

Choose a reason for hiding this comment

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

Since we changed configure to return error instead of panicking, the ParseArgumentsTest became unnecessary. We can remove it.

@chavacava
Copy link
Collaborator

@mfederowicz I'll wait the OK from @alexandear to merge the PR. Thanks for the huge amount of work! And, of course, thanks for the reviews and discussions @ccoVeille @alexandear @denisvmedia

rule/file_header.go Outdated Show resolved Hide resolved
@chavacava chavacava merged commit 9b15f3f into mgechev:master Dec 11, 2024
5 checks passed
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