Search code examples
go

File is not `goimports`-ed with -local somePath


I made some changes in Golang project and later ran make test which takes care of linting, formatting and unit testing. But when it run linter.sh, it throws following error

pkg/skaffold/kubernetes/wait.go:23: File is not `goimports`-ed with -local github.com/GoogleContainerTools/skaffold (goimports)
        "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"

Here is the link to Code.


Solution

  • Just doing normal Sort imports will probably not work. I think you have goimports linting with local-prefixes enabled, which is why the error File is not 'goimports'-ed with -local ...

    Normally goimports sorts the imported libraries in a way so that standard pkg and others are in a separated group. However when you have local-prefixes enable, linting expects standard pkg, 3rd party pkg, and the pkg with the specified local-prefixes (in your case github.com/GoogleContainerTools/skaffold, aka your own project pkg), these 3 types in separate group. (ref: https://github.com/golangci/golangci-lint/issues/209)

    import (
      // stdlib
    
      // third-party
    
      // other packages of that project
    )
    

    These doesn't have to be in 3 groups, you can have more that 3 groups. Just make sure that above 3 types (or 2) are not in the same one.

    Fix

    When you run goimports make sure you run it with -local flag. I think you can configure your IDE as well to do that. In your case it should look something like this:

    goimports -local "github.com/GoogleContainerTools/skaffold" -w .
    

    -w flag so that it writes the changes back

    . (dot) for all the files or you can specify just one file