Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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.

like image 615
prashant Avatar asked Jan 29 '20 04:01

prashant


2 Answers

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

like image 72
leninhasda Avatar answered Sep 21 '22 13:09

leninhasda


In my case, I had to change this:

import (
    "fmt"
    "github.com/gin-gonic/gin"
    "net/http"
    "strconv"
)

To this:

import (
    "fmt"
    "net/http"
    "strconv"

    "github.com/gin-gonic/gin"
)
like image 22
Tadej Avatar answered Sep 17 '22 13:09

Tadej