[英]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.我在 Golang 项目中做了一些更改,然后运行
make test
来处理 linting、格式化和单元测试。 But when it run linter.sh, it throws following error但是当它运行 linter.sh 时,它会抛出以下错误
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"
Just doing normal Sort imports
will probably not work.仅仅进行正常的
Sort imports
可能不起作用。 I think you have goimports
linting with local-prefixes
enabled, which is why the error File is not 'goimports'-ed with -local ...
我认为你已经启用了
local-prefixes
的goimports
,这就是为什么错误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.通常
goimports
以某种方式对导入的库进行排序,以便标准 pkg 和其他库位于一个单独的组中。 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.但是,当您启用本地前缀时,linting 需要标准 pkg、第 3 方 pkg 和具有指定本地前缀的 pkg(在您的情况下
github.com/GoogleContainerTools/skaffold
,也就是您自己的项目 pkg),这 3 种类型在单独的组。 (ref: https://github.com/golangci/golangci-lint/issues/209 ) (参考: 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.
这些不必分在 3 个组中,您可以有 3 个以上的组。 Just make sure that above 3 types (or 2) are not in the same one.
只要确保以上 3 种(或 2 种)不在同一个中即可。
Fix使固定
When you run goimports
make sure you run it with -local
flag.当您运行
goimports
时,请确保您使用-local
标志运行它。 I think you can configure your IDE as well to do that.我认为你也可以配置你的 IDE 来做到这一点。 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
-w标志,以便将更改写回
.
. (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: goimports
有一个问题来排序这样的事情:
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
为了使其格式化和 linter 可接受,您可以尝试这个第三方来修复包导入顺序: https ://github.com/incu6us/goimports-reviser
Example:例子:
import (
"log"
"github.com/incu6us/goimports-reviser/testdata/innderpkg"
"bytes"
"github.com/pkg/errors"
)
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).您必须对文件应用
goimports
命令才能正确排序导入(或者如果您使用 Goland,则可以使用 IDE 工具完成)。
Info about Goland integration: https://www.jetbrains.com/help/go/integration-with-go-tools.html有关 Goland 集成的信息: https ://www.jetbrains.com/help/go/integration-with-go-tools.html
I hit the same error.我遇到了同样的错误。 The lint was
golangci-lint
.棉绒是
golangci-lint
。
I sorted imports ( using GoLand ), tidied dependencies ( go mod tidy
) and removed whitespace ( gofmt -w app.go
).我对导入进行了排序(使用 GoLand),整理了依赖项(
go mod tidy
)并删除了空格( 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.当您的 IDE 和 linter 有不同的设置来对导入进行排序时,通常会出现此问题。 I was using GoLand IDE and golangci-lint as linter.
我使用GoLand IDE 和golangci-lint作为 linter。 I changed IDE settings to use goimports in place of gofmt as per the below picture and it solved these errors for me.
根据下图,我更改了 IDE 设置以使用goimports代替gofmt ,它为我解决了这些错误。
Simply follow the 2 steps, it will work fine:-只需按照2个步骤,它会正常工作: -
goimports
locally, to this run:-goimports
,运行:-go install golang.org/x/tools/cmd/goimports@latest
go 安装 golang.org/x/tools/cmd/goimports@latest
goimports -w file_name.go
goimports -w 文件名.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:
例如,这导致了我拥有的一个文件中的问题,而goimports -w...没有解决任何问题:
// 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()
// ...
// }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.