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

Update golangci-lint/goimports configuration #4945

Merged
merged 8 commits into from
Oct 25, 2021

Conversation

naemono
Copy link
Contributor

@naemono naemono commented Oct 12, 2021

Update goimports settings to consider github.com/elastic/cloud-on-k8s a local prefix in imports section.

This standardizes having

  1. stdlib imports at top of import section
  2. 3rd party imports as the next section
  3. local imports as the final section

@botelastic botelastic bot added the triage label Oct 12, 2021
Copy link
Contributor

@david-kow david-kow left a comment

Choose a reason for hiding this comment

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

I've read through all changes and all looks good. I was able to find few more files that have more than three groups though:

pkg/controller/association/reconciler.go
pkg/controller/association/conf.go
pkg/controller/association/resources_test.go
pkg/controller/association/controller/beat_es.go
pkg/controller/enterprisesearch/pod_test.go
pkg/controller/kibana/pod_test.go
pkg/controller/kibana/driver_test.go
pkg/controller/common/license/verifier_test.go
pkg/controller/common/keystore/resources_test.go
pkg/controller/elasticsearch/remotecluster/elasticsearch_test.go
pkg/controller/elasticsearch/user/associated_test.go
pkg/controller/elasticsearch/user/user_provided_test.go
pkg/controller/elasticsearch/sset/list_test.go
pkg/controller/remoteca/watches.go
pkg/dev/portforward/pod_forwarder_test.go

I found them using the following invocation and while I have little confidence that it's the best way, maybe we should follow up with including this into make lint? With that and goimports we should cover all cases and keep all the imports neat from now on.

$ find . -name "*\.go" | xargs -I 'file' bash -c 'echo -n "file - " && sed -E -n "/import \(/,/\)/P" file | grep -c "^$"' | grep -v '\(0\|1\|2\)' | cut -f 1 -d " "

@pebrc
Copy link
Collaborator

pebrc commented Oct 13, 2021

If we go for this new three section import block we should also update the contributing guideline accordingly.

@naemono naemono force-pushed the update-goimports-configuration branch from 5dcc33d to 6c21708 Compare October 13, 2021 15:31
@naemono
Copy link
Contributor Author

naemono commented Oct 13, 2021

Ok, I've updated the contribution document, as well as fixed the additional files. How would yall like to handle detecting imports that are not valid? Add something to the makefile such as the find command @david-kow is suggesting @pebrc ?

Copy link
Contributor

@david-kow david-kow left a comment

Choose a reason for hiding this comment

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

LGTM! As for adding the check I'd be fine both ways, curious what others think too.

@david-kow david-kow added :ci Things related to Continuous Integration, automation and releases >enhancement Enhancement of existing functionality labels Oct 14, 2021
@botelastic botelastic bot removed the triage label Oct 14, 2021
@botelastic botelastic bot removed the triage label Oct 14, 2021
@pebrc
Copy link
Collaborator

pebrc commented Oct 14, 2021

Why do we need another check? Or put differently why did goimports not catch all cases?

@david-kow
Copy link
Contributor

Why do we need another check? Or put differently why did goimports not catch all cases?

Because when more groups are present it thinks that it's on purpose and fixes them only partially.

@naemono
Copy link
Contributor Author

naemono commented Oct 14, 2021

From doing some investigation, goimports doesn't handle things perfectly as we'd want it to. See golang/go#20818
Along with this https://go-review.googlesource.com/c/tools/+/116795/
which seems to have been reverted, from what I can tell.

Taking one of the files you noted earlier that wasn't "fixed", from the master branch, and note it isn't shown that it would be "fixed" by goimports (that is, the 2x 3rd party groupings aren't combined.

# -d shows the diff, and note 0 output... 
❯ goimports -d -local github.com/elastic/cloud-on-k8s pkg/controller/association/reconciler.go
❯ 

But, as noted in the golang issue above, there is a fork of goimports that seems to handle this properly

❯ gosimports -d -local github.com/elastic/cloud-on-k8s pkg/controller/association/reconciler.go

diff -u pkg/controller/association/reconciler.go.orig pkg/controller/association/reconciler.go
--- pkg/controller/association/reconciler.go.orig	2021-10-14 09:06:10.000000000 -0500
+++ pkg/controller/association/reconciler.go	2021-10-14 09:06:10.000000000 -0500
@@ -10,6 +10,7 @@
 	"reflect"
 	"time"

+	"github.com/go-logr/logr"
 	"go.elastic.co/apm"
 	corev1 "k8s.io/api/core/v1"
 	apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -18,8 +19,6 @@
 	"sigs.k8s.io/controller-runtime/pkg/client"
 	"sigs.k8s.io/controller-runtime/pkg/reconcile"

-	"github.com/go-logr/logr"
-
 	commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
 	esv1 "github.com/elastic/cloud-on-k8s/pkg/apis/elasticsearch/v1"
 	"github.com/elastic/cloud-on-k8s/pkg/controller/common"

Is this fork a path we want to go down, or simply work under the assumption that PR reviews + golangci-lint will catch the vast majority of these improper imports?

@naemono naemono force-pushed the update-goimports-configuration branch from 6c21708 to f7e837f Compare October 14, 2021 16:02
@naemono
Copy link
Contributor Author

naemono commented Oct 18, 2021

@david-kow @pebrc any thoughts on the above question? Should we just leave as-is, and merge? lmk, thanks.

@david-kow
Copy link
Contributor

I'd be fine both with merging as is and with adding the script to verify group count.

@naemono naemono force-pushed the update-goimports-configuration branch from f7e837f to 7469552 Compare October 19, 2021 13:25
Add back linter hints
Add back more linter hints
Update additional files to group import groups properly
@naemono naemono force-pushed the update-goimports-configuration branch from 7469552 to 4743b16 Compare October 20, 2021 22:26
@pebrc
Copy link
Collaborator

pebrc commented Oct 21, 2021

I'd be fine both with merging as is

Same.

@naemono naemono merged commit 9be005d into elastic:master Oct 25, 2021
fantapsody pushed a commit to fantapsody/cloud-on-k8s that referenced this pull request Feb 7, 2023
* Update goimports settings to consider github.com/elastic/cloud-on-k8s a local prefix in imports

* remove comment in golangci.yml

* Fix imports
Add back linter hints

* Fix more imports
Add back more linter hints

* Update verbiage for ccontributing guidelines
Update additional files to group import groups properly

* Fix mis-aligned imports

* Fix additional bad import statements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:ci Things related to Continuous Integration, automation and releases >enhancement Enhancement of existing functionality >refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants