Golangci-lint: Goimports "local" mode

Created on 31 Aug 2018  路  6Comments  路  Source: golangci/golangci-lint

Specifically, -local flag:

-local string
put imports beginning with this string after 3rd-party packages

That is very useful for projects that follow this grouping style:

import (
  // stdlib

  // third-party

  // other packages of that project
)

Most helpful comment

@thepwagner you are right, the bug was introduced in commit c02a6daa5c6d2b0faf5b4be37b65ef15a52a1f35.
I need to make a test for it, the fix will be to use golang.org/x/tools (I've switched to the fork because of the bug in x/tools, now it should be fixed)

All 6 comments

goimports shouldn't complain if the file is already formatted with -local - it's still correctly imported as goimports won't merge/sort newline-separated blocks of imports.

Yes, but I want it to complain if imports are not formatted with -local flag, but local setting is present in the configuration.

Hi!
Thank you for the idea,
I supported it in the master branch. I will include it into a next release

@jirfag I don't think this is working as intended yet.

We're setting https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/gofmt.go#L115 , which resolves to golang.org/x/tools/imports.LocalPrefix ( https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/gofmt.go#L9 )

Then we invoke https://github.com/golangci/golangci-lint/blob/master/vendor/github.com/golangci/gofmt/goimports/golangci.go#L11 , which in turn uses https://github.com/golangci/golangci-lint/blob/master/vendor/github.com/golangci/gofmt/goimports/golangci.go#L8 - golang.org/golangci/tools/imports.LocalPrefix

So when GoImports is invoked, it doesn't have the LocalPrefix that we attempted to set - we set another LocalPrefix that isn't used.

I won't PR this as it's pretty deep into dep hell - swapping https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/gofmt.go#L9 to github.com/golangci/tools/imports works for me - but there may be a better solution (or more cleanup: like picking one of the vendored goimports and using it).

thank you

@thepwagner you are right, the bug was introduced in commit c02a6daa5c6d2b0faf5b4be37b65ef15a52a1f35.
I need to make a test for it, the fix will be to use golang.org/x/tools (I've switched to the fork because of the bug in x/tools, now it should be fixed)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kipply picture kipply  路  4Comments

simonpasquier picture simonpasquier  路  4Comments

KeepMasterBranch picture KeepMasterBranch  路  3Comments

anuaimi picture anuaimi  路  4Comments

jirfag picture jirfag  路  3Comments