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 custom go lint checker #3021

Closed
jpeach opened this issue Oct 13, 2020 · 25 comments
Closed

Add custom go lint checker #3021

jpeach opened this issue Oct 13, 2020 · 25 comments
Labels
area/tooling Issues or PRs related to project tools and automation. kind/feature Categorizes issue or PR as related to a new feature.

Comments

@jpeach
Copy link
Contributor

jpeach commented Oct 13, 2020

Please describe the problem you have

We have a few informal rules around style and interfaces

  • CLI help should be capitalized
  • log messages should not be capitalized
  • import aliases should be have a consistent pattern

We can add a Go lint tool to check these conventions, removing the burden from maintainers and contributors.

@jpeach jpeach added kind/feature Categorizes issue or PR as related to a new feature. area/tooling Issues or PRs related to project tools and automation. labels Oct 13, 2020
@jpeach jpeach added this to Unprioritized in Contour Project Board via automation Oct 13, 2020
@Glyphack
Copy link
Contributor

I think there is a feature in golangci-lint that you can add custom plugins to it for your own needs. also by creating a plugin every repository can add it on top of golangci-lint to run.

@jpeach
Copy link
Contributor Author

jpeach commented Oct 15, 2020

I think there is a feature in golangci-lint that you can add custom plugins to it for your own needs. also by creating a plugin every repository can add it on top of golangci-lint to run.

Yep, that would be my preference. Unfortunately it's a binary plugin interface, so distribution is a problem.

@Glyphack
Copy link
Contributor

Yep, that would be my preference. Unfortunately it's a binary plugin interface, so distribution is a problem.

Yes it's binary plugin but it's possible to download binary in Makefile when running lint command. I take a look at this to see if I can help.

@Glyphack
Copy link
Contributor

There are several examples of logs messages first letter capitalized. Are you sure this feature is needed?

@jpeach
Copy link
Contributor Author

jpeach commented Oct 16, 2020

https://github.com/jpeach/go-message-fmt

@Glyphack
Copy link
Contributor

Do you have an idea about import alias namimg pattern?

@jpeach
Copy link
Contributor Author

jpeach commented Oct 16, 2020

Do you have an idea about import alias namimg pattern?

The alias naming guidelines are here. It might be hard to programmatically get it right because the rule is a bit subjective, but I expect it to reasonably easy to ensure that a set of specific well-known imports are always consistent.

@Glyphack
Copy link
Contributor

Glyphack commented Oct 18, 2020

I think this rule is easy to implement and also effective:
consider import path: github.com/projectcontour/x/y/z/v*

  1. the alias name should be subset of x[optional]_y[optional]_z[optional]_v* where optional means it can be present or not. We can omit the last _ because It's already used like this

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

corev1 "k8s.io/api/core/v1"

  1. one of x or y or z must be present in alias name. however this rule is not satisfied right now, for example:

    v1 "k8s.io/api/core/v1"

  2. If version exists in path, must be specified.

  3. words like apis should be api in import alias

@jpeach
Copy link
Contributor Author

jpeach commented Oct 18, 2020

The rule is newer than the code, so it's not (yet) followed consistently. A linter would be really useful to help us make the transition (especially if it emits a fixit suggestion) :)

In your examples, I think it would be fine to end up with meta_v1 and core_v1 in all cases. It's less conventional, but more consistent.

@Glyphack
Copy link
Contributor

Can I start working on implementation for import alias?

@jpeach
Copy link
Contributor Author

jpeach commented Oct 19, 2020

Can I start working on implementation for import alias?

Yes, please do!

@jpeach
Copy link
Contributor Author

jpeach commented Oct 23, 2020 via email

@Glyphack
Copy link
Contributor

Glyphack commented Oct 26, 2020

@jpeach I finished the implementation of the linter. here's the PR, Can you take a look at it? also I attach the result of running this linter on the repo, seemed fine to me but double check is good.
res.txt
After this we can move on to the fixit feature.

@jpeach
Copy link
Contributor Author

jpeach commented Oct 27, 2020

/home/glyphack/programming/contour/internal/certgen/certgen.go:25:2: used words in alias most be present in path path: k8s/io/apimachinery/pkg/api/errors alias: k8serrors

Unclear to me what the rule means for this import, maybe apimachinery_errors?

/home/glyphack/.cache/go-build/a3/a393284caaf6ccc1fa84f4413907252cfce43c3a6a389abdda771c0789515a66-d:13:2: used words in alias most be present in path path: github/com/projectcontour/contour/internal/contour alias: _test

Hmm, what is this telling me? Maybe an artifact of the testing mechanism?

/home/glyphack/programming/contour/internal/featuretests/envoy.go:29:2: version not specified in alias. path: github/com/envoyproxy/gocontrolplane/envoy/config/filter/network/tcp/proxy/v2 alias: envoy_config_v2_tcpproxy version v2

👍

Overall, it looks like the linter is doing a pretty reasonable job :)

@Glyphack
Copy link
Contributor

Glyphack commented Oct 27, 2020

Unclear to me what the rule means for this import, maybe apimachinery_errors?

Yes that's meant to be correct, maybe error message needs more clarification.

Hmm, what is this telling me? Maybe an artifact of the testing mechanism?

I suspect this is only happening on my machine could not find anything about it.

@Glyphack
Copy link
Contributor

Glyphack commented Nov 6, 2020

I implemented fixit option for linter(Glyphack/go-import-alias#2). and used it on contour main branch to see how it works(#3108). As I can see It's doing a reasonable job, Do you have any comments on it?

@Glyphack
Copy link
Contributor

Glyphack commented Nov 9, 2020

log messages should not be capitalized

a quick question @jpeach. what should happen to log messages like this?

log.Warnf("error listing HTTPProxies: %v", err)
} else {

you meant only the first letter should not be capitalized or the whole messge?

@Glyphack
Copy link
Contributor

Glyphack commented Nov 21, 2020

If we make a decision on how to integrate linter with project I can make progress on (#3108).
I guess there are two options:

  1. using golangci-lint: the downside is that we have to use binary plugins.
  2. using go-vet: downside is we have two source of linters(golangci-lint and go vet custom linters).

@jpeach @stevesloka

@jpeach
Copy link
Contributor Author

jpeach commented Nov 21, 2020

@Glyphack WDYT about making a new repository for linters under projectcontour? Once we do that, we could look at the binary plugins, or we may be able to add it to golangci-lint directly?

@Glyphack
Copy link
Contributor

@jpeach It's a good idea keeping list of all linters we use somewhere, Are you thinking about a single repo with all linters in it and a multicheker or separate ones? I'm OK with both.

Once we do that, we could look at the binary plugins, or we may be able to add it to golangci-lint directly?

I make an issue in golangci-lint about our linters, If they accept it to be part of golangci-lint it would be great, If not we can use binary plugins.

@jpeach
Copy link
Contributor Author

jpeach commented Nov 22, 2020

@jpeach It's a good idea keeping list of all linters we use somewhere, Are you thinking about a single repo with all linters in it and a multicheker or separate ones? I'm OK with both.

yeh I was thinking of a single repo

@Glyphack
Copy link
Contributor

Glyphack commented Dec 5, 2020

I created this issue in golangci-lint repo 11 days ago and did not get any response from them. shall we create the linters repository and proceed with binary plugins?

@Glyphack
Copy link
Contributor

Glyphack commented Jan 6, 2021

I was trying to test linter using a generated plugin from github.com/contour/lint and got an error about cannot opening the plugin. searched and found this issue on golangci-lint:
golangci/golangci-lint#1276
seems like we can't use plugins with release versions of golangci-lint. any idea of how to proceed? we have two options here:

  • using go vet
  • building golangci-lint binary with CGO_ENABLED=1 and then run it.

@jpeach
Copy link
Contributor Author

jpeach commented Jan 6, 2021

If we release this as a standalone binary, we can run in it in a separate Contour CI job, WDTY?

@sunjayBhatia
Copy link
Member

closing for now, I think we're happing with our existing linting

Contour Project Board automation moved this from Unprioritized to 1.21 release (candidates) Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/tooling Issues or PRs related to project tools and automation. kind/feature Categorizes issue or PR as related to a new feature.
Projects
No open projects
Contour Project Board
  
1.21 release (candidates)
Development

Successfully merging a pull request may close this issue.

3 participants