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

Add failing for certain methods in a given package (i.e: fmt.Errorf) #7

Closed
fatih opened this issue Mar 6, 2020 · 10 comments · Fixed by #8
Closed

Add failing for certain methods in a given package (i.e: fmt.Errorf) #7

fatih opened this issue Mar 6, 2020 · 10 comments · Fixed by #8
Labels
enhancement New feature or request

Comments

@fatih
Copy link
Owner

fatih commented Mar 6, 2020

Currently faillint only fails on package level (based on the import path). We should also make it possible to fail for a sub-set of exported functions, types or variables (i.e: ast.TypeSpec and ast.ValueSpec should be enough).

Why is this useful? Let's look at this example:

If a user opted to use github.com/pkg/errors not only want they prevent the usage of errors package, they probably also want to avoid using fmt.Errorf and start using errors.Errorf method from the github.com/pkg/errors package. There are many reasons for that. For example the github.com/pkg/errors package includes stack information in each error value. Hence they want to make sure all errors are created with the github.com/pkg/errors package.

We could still use the paths flags and could define the sub-set with a dot identifier. For example the following command could fail for all usages of errors and the usage of fmt.Errorf:

faillint --paths "errors,fmt.Errorf"

If anyone wants to work on this, please comment on the PR before starting on it. I'll work on that at some point otherwise.

@fatih fatih added the enhancement New feature or request label Mar 6, 2020
@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

Happy to take this.

Our main use case is that we need this to block prometheus.New... and promethues.MustRegister to force the use of new amazing promauto.With(..).New... addition that ensures that we never forget about registering metric again! However https://godoc.org/github.com/prometheus/client_golang/prometheus package has much more than just constructors, so we cannot block the whole package. (:

@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

Some additional work would be to make it less painful to configure multiple functions like

faillint -paths "github.com/foo/bar,github.com/foo/bar/foo.{A,B,D,G,Z}

But let's maybe attack this later on - also it would work weirdly with suggestions.

@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

This would be super easy to implement, but might be not very clear for users (:

faillint -paths "github.com/foo/bar,github.com/foo/bar/foo.A.B.D.G.Z

@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

Nice edge case:

faillint --paths "errors,gopkg.in/yaml.v2"

Should we block v2 function of yaml or just gopkg.in/yaml.v2 import? ;p I guess since you cannot in theory use unexported v2 function, it's the former (:

@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

Also package.ABC is invalid as package name but a valid repository name.. 🤦‍♂️

image

No way really to figure it out, so I guess we either assume no such package will be provided.. or we might need something different than . for function name separator.

@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

{} might do...

faillint -paths "github.com/foo/bar,github.com/foo/bar/foo.{A}

@pracucci
Copy link

pracucci commented Mar 6, 2020

{} might do...

Looks a feasible solution to me.

@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

Ack, playing with some nice regexp for this.

@fatih
Copy link
Owner Author

fatih commented Mar 6, 2020

Thanks for the comments. There are a lot of edge cases and it's great we capture them here.

So after looking at #2 and reading the comments here, I think we need a proper and scalable syntax for the config. Either we try to put everything inside the --paths flag and try to improve the syntax there OR we split certain things into separate flags.

If we go route 1, I think using a Regex style syntax would solve a lot of stuff. For example #2 would benefit from it:

faillint --paths "github.com/golang.org/x/net/**" 

Or for this issues it could be

faillint --pahts "errors,fmt.(Errorf|Foo)"

But regex is the last thing I want to use anywhere in this tool. faillint should be easy to use without any surprises. regex is the opposite. Also I don't want to use a config file, because storing and managing a file is a chore and burden.

Assuming we use this format (no regex):

faillint -paths "errors,fmt.{Errorf}"

How should we define the suggestion in this case? Currently it's like this:

faillint -paths "errors=github.com/pkg/errors"

So we need to come up with a solution to provide suggestion for methods as well. One idea:

faillint -paths "errors=github.com/pkg/errors,fmt.{Errorf}=github.com/pkg/errors.{Errorf}"

I'm still tinkering about this so if you have more ideas about the syntax or if you think it would make sense to introduce new flags let me know.

bwplotka added a commit to bwplotka/faillint that referenced this issue Mar 6, 2020
Fixes fatih#7

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
bwplotka added a commit to bwplotka/faillint that referenced this issue Mar 6, 2020
Fixes fatih#7

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
@bwplotka
Copy link
Contributor

bwplotka commented Mar 6, 2020

Suggestions for the function is not really a problem. It's bit tricky to add suggestions for grouped functions with {A,B,C} which I proposed here: #8 (:

I went for a simple solution, so just a single suggestion for a single group, so it's up to the user to make it work. The suggestion is kind of import-like arbitrary string you can put. I think that's easy enough for users.

Anyway, all should be fixed here: #8

bwplotka added a commit to bwplotka/faillint that referenced this issue Mar 6, 2020
Fixes fatih#7

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
bwplotka added a commit to bwplotka/faillint that referenced this issue Mar 6, 2020
Fixes fatih#7

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
@fatih fatih closed this as completed in #8 Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants