简体   繁体   中英

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 .

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

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

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

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

在此处输入图像描述

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

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 worked apart from:

golangci-lint run --fix

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 编辑器设置

Simply follow the 2 steps, it will work fine:-

  1. Install goimports locally, to this run:-

go install golang.org/x/tools/cmd/goimports@latest

  1. Then for each file run:-

goimports -w file_name.go

Sometimes removing (portions of) comment blocks is the only fix. Eg this was causing the problem in one file I had, and goimports -w... didn't fix anything about it:

// Example usage:
//
//    const tracePrefix = "storage"
//
//    func FuncName(ctx context.Context, ...) {
//    fSpan, ctx := opentracing.StartSpanFromContext(ctx, CurrentFuncName(tracePrefix))
//    fSpan.SetTag("company_uuid", jwt.CompanyUUID)
//    defer fSpan.Finish()
//    ...
//    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM