简体   繁体   English

文件未使用 -local somePath 进行“goimports”编辑

[英]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"

Here is the link to Code .这是代码的链接。

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-prefixesgoimports ,这就是为什么错误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:例子:

  • 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).您必须对文件应用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 ,它为我解决了这些错误。

Goland 编辑器设置

Simply follow the 2 steps, it will work fine:-只需按照2个步骤,它会正常工作: -

  1. Install goimports locally, to this run:-在本地安装goimports ,运行:-

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

  1. Then for each file run:-然后对于每个文件运行: -

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.

相关问题 goimports 需要忽略供应商 package - goimports needs to ignore vendor package 如何将本地视频文件上传到 Google Cloud - How to upload local video file to Google Cloud GCP 将文件从我的本地机器复制到 GCP Vm - GCP Copying File from my local Machine to GCP Vm Google App Engine:将文件下载到用户的本地下载文件夹 - Google App Engine: Download a file to user's local Download folder Firebase 函数保存 JSON 到本地文件 - Firebase functions saving JSON to local file Flutter:如何将 Json 文件下载到本地然后访问它 - Flutter: How to download Json file to local then access it 如何向本地 apache 光束提供谷歌凭证 json 文件? - How to provide google credentials json file to local apache beam? Gitlab API 从本地文件系统上传整个项目 - Gitlab API to upload whole project from local file system Airflow DAG 找不到要在 s3 上上传的本地文件 - Airflow DAG can't find local file to upload on s3 运行 Cloud Function 将 Firebase 经过身份验证的用户数据存储到 Firestore - Run Cloud Function to store Firebase Auth'ed user data to Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM