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

Enable linter goimports to tidy imports #1037

Merged
merged 1 commit into from
Aug 6, 2020
Merged

Conversation

tnqn
Copy link
Member

@tnqn tnqn commented Aug 5, 2020

goimports makes imports deterministic, putting them in 3 sections:
builtin, 3rd-party, local packages. It saves reviewers's effort to
comment the order of imports.

It doesn't allow imports named "v1" unless the alias name is specified
as "v1", this patch adds the parent packages of v1 packages as the
prefix of the import names.

Fixes #1038

goimports makes imports deterministic, putting them in 3 sections:
builtin, 3rd-party, local packages. It saves reviewers's effort to
comment the order of imports.

It doesn't allow imports named "v1" unless the alias name is specified
as "v1", this patch adds the parent packages of v1 packages as the
prefix of the import names.
@antrea-bot
Copy link
Collaborator

Thanks for your PR.
Unit tests and code linters are run automatically every time the PR is updated.
E2e, conformance and network policy tests can only be triggered by a member of the vmware-tanzu organization. Regular contributors to the project should join the org.

The following commands are available:

  • /test-e2e: to trigger e2e tests.
  • /skip-e2e: to skip e2e tests.
  • /test-conformance: to trigger conformance tests.
  • /skip-conformance: to skip conformance tests.
  • /test-whole-conformance: to trigger all conformance tests on linux.
  • /skip-whole-conformance: to skip all conformance tests on linux.
  • /test-networkpolicy: to trigger networkpolicy tests.
  • /skip-networkpolicy: to skip networkpolicy tests.
  • /test-windows-conformance: to trigger windows conformance tests.
  • /skip-windows-conformance: to skip windows conformance tests.
  • /test-windows-networkpolicy: to trigger windows networkpolicy tests.
  • /skip-windows-networkpolicy: to skip windows networkpolicy tests.
  • /test-hw-offload: to trigger ovs hardware offload test.
  • /skip-hw-offload: to skip ovs hardware offload test.
  • /test-all: to trigger all tests (except whole conformance).
  • /skip-all: to skip all tests (except whole conformance).

These commands can only be run by members of the vmware-tanzu organization.

@tnqn
Copy link
Member Author

tnqn commented Aug 5, 2020

/test-all

@tnqn
Copy link
Member Author

tnqn commented Aug 5, 2020

/test-windows-conformance

2 similar comments
@tnqn
Copy link
Member Author

tnqn commented Aug 5, 2020

/test-windows-conformance

@tnqn
Copy link
Member Author

tnqn commented Aug 5, 2020

/test-windows-conformance

Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

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

LGTM. This will definitely be a big help so that we can focus on other things during review.

Copy link
Member

@srikartati srikartati left a comment

Choose a reason for hiding this comment

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

Thanks for the change @tnqn. Definitely a time saver. Just a couple of questions.

@@ -14,3 +18,4 @@ linters:
- deadcode
- staticcheck
- gosec
- goimports
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need to enable gofmt as goimports also formats the file?
Following is from https://godoc.org/golang.org/x/tools/imports:

func Process
func Process(filename string, src []byte, opt *Options) ([]byte, error)
Process formats and adjusts imports for the provided file. If opt is nil the defaults are used, and if src is nil the source is read from the filesystem.

Copy link
Contributor

Choose a reason for hiding this comment

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

I do agree that goimports does everything gofmt does. That being said, I don't think there is much harm in keeping it in the list. Makes it explicit that we are running this linters, even if it means we run it twice. Or we can remove it but add a comment to the yml file to indicate that goimports supersedes gofmt.

Copy link
Member Author

Choose a reason for hiding this comment

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

@srikartati thanks for pointing it out, I didn't know goimports supersedes gofmt. I also think there is no much harm to keep gofmt explicitly as it's more well known and widely adopted?

Copy link
Member

Choose a reason for hiding this comment

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

Sure.. format can be done two times. I was looking at the API and found out about it--thought I will check with you.

@@ -21,7 +21,7 @@ import (
"time"

"github.com/containernetworking/plugins/pkg/ip"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity.. is there a rule to determine the package name as prevDirectory+currentDirectory.

Copy link
Member Author

Choose a reason for hiding this comment

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

Adding this because goimports think "v1" is not a good import name because of lack of package information which I also agree. But the way it "fixes" it is adding "v1" as the import alias.

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

I'm not sure if this is a bug or a workaround that makes goimports ignore it. But I feel having a prefix can improve the readability as well as in compliance with goimports.
Both "corev1", "v1" are used in kubernetes:
https://github.com/kubernetes/kubernetes/blob/master/pkg/master/controller.go#L26
https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/kubelet.go#L42

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation. corev1 sounds good.

@@ -21,7 +21,7 @@ import (
"time"

"github.com/containernetworking/plugins/pkg/ip"
"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation. corev1 sounds good.

@@ -14,3 +18,4 @@ linters:
- deadcode
- staticcheck
- gosec
- goimports
Copy link
Member

Choose a reason for hiding this comment

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

Sure.. format can be done two times. I was looking at the API and found out about it--thought I will check with you.

@tnqn tnqn merged commit 74055d7 into antrea-io:master Aug 6, 2020
GraysonWu pushed a commit to GraysonWu/antrea that referenced this pull request Sep 22, 2020
goimports makes imports deterministic, putting them in 3 sections:
builtin, 3rd-party, local packages. It saves reviewers's effort to
comment the order of imports.

It doesn't allow imports named "v1" unless the alias name is specified
as "v1", this patch adds the parent packages of v1 packages as the
prefix of the import names.
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.

Imports order should be tidied automatically
5 participants