'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.

go


Solution 1:[1]

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

Solution 2:[2]

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"
)

Solution 3:[3]

goimports has an issue to sort something like this:

import (
    "context"

    "github.com/gofrs/uuid"

    "github.com/pkg/errors"

    "github.com/shopspring/decimal"

    "io"
)

It will live as it is. To make it formatted and linter-acceptable you could try this third-party to fix package imports order: https://github.com/incu6us/goimports-reviser

Example:

  • before usage:
import (
    "log"

    "github.com/incu6us/goimports-reviser/testdata/innderpkg"

    "bytes"

    "github.com/pkg/errors"
)
  • after usage:
import (
    "bytes"
    "log"

    "github.com/pkg/errors"

    "github.com/incu6us/goimports-reviser/testdata/innderpkg"
)

Solution 4:[4]

I looked through your code and obviously the problem is your imports. You have to apply goimports command to your files to sort imports properly (or if you use Goland it can be done with IDE tools).

enter image description here

Info about Goland integration: https://www.jetbrains.com/help/go/integration-with-go-tools.html

Solution 5:[5]

I hit the same error. The lint was golangci-lint.

I sorted imports ( using GoLand ), tidied dependencies ( go mod tidy ) and removed whitespace ( gofmt -w app.go ).

Nothing work apart from:

golangci-lint run --fix

Solution 6:[6]

This problem usually occurs when your IDE and your linter have different settings to sort imports. I was using GoLand IDE and golangci-lint as linter. I changed IDE settings to use goimports in place of gofmt as per the below picture and it solved these errors for me.

Goland Editor Settings

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Tadej
Solution 3
Solution 4 rustyMagnet
Solution 5 rustyMagnet
Solution 6 Rahul Garg