文件未使用-local somePath进行`goimports`-艾德

pobjuy32  于 12个月前  发布在  Go
关注(0)|答案(9)|浏览(237)

我在Golang项目中做了一些修改,后来运行了make test,它负责linting,格式化和单元测试。但是当它运行www.example.com时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"

这里是Code的链接。

vc9ivgsu

vc9ivgsu1#

仅仅执行普通的Sort imports可能无法工作。我认为你有goimports linting与local-prefixes启用,这就是为什么错误File is not 'goimports'-ed with -local ...
通常情况下,goimports会对导入的库进行排序,以便将标准pkg和其他库放在一个单独的组中。然而,当你启用了本地前缀时,linting需要标准的pkg,第三方的pkg,以及带有指定的本地前缀的pkg(在你的例子中是github.com/GoogleContainerTools/skaffold,也就是你自己的项目pkg),这三种类型在单独的组中。(ref:https://github.com/golangci/golangci-lint/issues/209

import (
  // stdlib

  // third-party

  // other packages of that project
)

这些不一定要在3组,你可以有更多的3组。只要确保以上3种类型(或2种)不在同一种类型中即可。

修复

当你运行goimports时,确保你运行它时带有-local标志。我想你也可以配置你的IDE来做到这一点。在您的情况下,它应该看起来像这样:

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

-w标志,以便将更改写回
**. (点) 表示所有文件,也可以只指定一个文件

gg0vcinb

gg0vcinb2#

我犯了同样的错误。皮棉是golangci-lint
我对导入进行了排序(使用GoLand),整理了依赖项(go mod tidy)并删除了空白(gofmt -w app.go)。
除了以下几点之外,没有任何效果:
golangci-lint run --fix

dbf7pr2w

dbf7pr2w3#

在我的情况下,我必须改变这一点:

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

对此:

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

    "github.com/gin-gonic/gin"
)
ou6hu8tu

ou6hu8tu4#

当IDE和linter对导入进行排序的设置不同时,通常会发生此问题。我使用 GoLand IDE和 golangci-lint 作为linter。我更改了IDE设置,使用***goimports***代替***gofmt***,如下图所示,它为我解决了这些错误。

nkhmeac6

nkhmeac65#

goimports有一个问题来排序这样的东西:

import (
    "context"

    "github.com/gofrs/uuid"

    "github.com/pkg/errors"

    "github.com/shopspring/decimal"

    "io"
)

它会像它一样生活。要使其格式化并可接受,您可以尝试此第三方来修复包导入顺序:https://github.com/incu6us/goimports-reviser
范例:

  • 使用前:
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"
)
xtfmy6hx

xtfmy6hx6#

我看了你的代码,很明显问题出在你的导入上。你必须对你的文件应用goimports命令来正确地对导入进行排序(或者如果你使用Goland,可以用IDE工具来完成)。

关于Goland集成的信息:https://www.jetbrains.com/help/go/integration-with-go-tools.html

gojuced7

gojuced77#

只要遵循两个步骤,它就会很好地工作:
1.在本地安装goimports,运行如下:-
去安装golang.org/x/tools/cmd/goimports@latest
1.然后,对于每个文件运行:-
goimports -w file_name.go

ufj5ltwl

ufj5ltwl8#

有时候,删除(部分)注解块是唯一的解决办法。例如,这导致了我的一个文件中的问题,并且**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()
//    ...
//    }
46scxncf

46scxncf9#

我在注解中添加JSON示例时也遇到了同样的问题

// newMap = {
//    "integration-algorithm" : [
//        {"container": 5},
//        {"alert-manager": 4}
//    ]
// }

这导致我的go-lint失败。当我从评论消息中删除空格(//和第一个字母之间的空格)时,问题解决了。

// newMap = {
 // "integration-algorithm" : [
 // {"container": 5},
 // {"alert-manager": 4}
 // ]
 // }

这是一个很好的黑客删除这些linting错误。

相关问题