golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

confusing error message "File is not goimports-ed (goimports)"

Open briantkennedy opened this issue 7 years ago • 29 comments

A file not being "goimports-ed" is a little confusing. Can we adjust this error message to something a little clearer like "File has not been formatted (goimports)"

briantkennedy avatar Jun 29 '18 21:06 briantkennedy

hi! it's the good idea, but now it's too late to change it: some users have already added this text to exclude list.

jirfag avatar Jul 14 '18 20:07 jirfag

now it's too late to change it: some users have already added this text to exclude list.

This way we can never improve error message 😇

pjvds avatar Nov 09 '18 19:11 pjvds

You are right, I got an idea to mark it as v2: in the next version (not in the roadmap yet) we can break compatibility and implement id.

jirfag avatar Nov 10 '18 07:11 jirfag

Is this using some custom version of goimports? I recently had it complain about one import even though the file was formatted by goimports. Turned out it wanted me to move it to another group of imports (even though regular goimports doesn't move imports between groups).

qfel avatar Dec 21 '18 00:12 qfel

This has started to happen to me since 1.24+. Running GolangCI with goimports suggests files aren't properly formatted even though running both gofmt and goimports manually on the files golangci complains about results in no changes and both tools exiting with a status code of 0.

daenney avatar Dec 23 '18 11:12 daenney

Same here (but I'm a new user). It complains for imports containing the - character, such as:

  • github.com/opentracing/opentracing-go
  • github.com/getsentry/raven-go
  • github.com/michaelklishin/rabbit-hole
  • github.com/oschwald/geoip2-golang

pierrre avatar Dec 26 '18 08:12 pierrre

@qfel @daenney @pierrre do you use updated goimports? Did you run go get -u golang.org/x/tools/cmd/goimports?

also, let's continue in https://github.com/golangci/golangci-lint/issues/347, this issue about message text

jirfag avatar Jan 20 '19 17:01 jirfag

I just ran into this problem too. A better message would have gotten me to a fix quicker.

bcomnes avatar Feb 06 '19 23:02 bcomnes

+1; Human readable message is expected and "File is not goimportes-ed" confused me

detailyang avatar May 20 '19 02:05 detailyang

  1. What message do you want to see? Please, run goimports tool for this file?
  2. Have you tried --fix option?

vtopc avatar Jun 12 '19 13:06 vtopc

The message should explain what the problem is (e.g. missing an import statement for package x.) It could also maybe propose a solution like, add the import statement or run goimports to add missing import statements. It should not just print what the solution is, because it may not be clear what the problem is for those unfamiliar with the suggested tool.

bcomnes avatar Jun 12 '19 13:06 bcomnes

@bcomnes, import statement != goimports, looks like the tool should be renamed, not a message.

In addition to fixing imports, goimports also formats your code in the same style as gofmt

vtopc avatar Jun 12 '19 13:06 vtopc

i have the same error, but run goimports -w on file does not bring any differences.

vtolstov avatar Dec 04 '19 10:12 vtolstov

Is there any update on this? Maybe it makes sense to at least add documentation to this check?

Right now, it reports two lines in my project and I have no idea what it means and how to resolve them (go fmt ./... doesn't change anything and the imports are obviously needed).

Shpota avatar Mar 27 '20 18:03 Shpota

@qfel @daenney @pierrre do you use updated goimports? Did you run go get -u golang.org/x/tools/cmd/goimports?

also, let's continue in #347, this issue about message text

I came across the same issue. It was being fixed by the following steps:

  1. go get -u golang.org/x/tools/cmd/goimports
  2. go mod tidy
  3. go mod vendor
  4. go fmt //==> formatting code
  5. goimports -w ==> clean imports
  6. gofmt -s -w ==> simplify code

samurnin avatar Mar 27 '20 19:03 samurnin

@samurnin, the goimports -w is not just managing imports, it's also doing the same formating as regular gofmt -w. P.S. thanks for -s flag.

vtopc avatar Mar 30 '20 08:03 vtopc

Built-in and non-built-in packages should be separated and the built-in packages placed first 内置包和非内置包应该要分开,内置包放在前面,自己的外部包放后面,例如

import (
	"fmt"
	"net/http"

	"github.com/containers/podman/v2/libpod/define"
	"github.com/containers/podman/v2/pkg/domain/entities"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
)

zhangguanzhang avatar Aug 02 '20 14:08 zhangguanzhang

+1 for @samurnin for the details steps. goimports -w filename.go fixed it for me. In the end it was what @zhangguanzhang suggested.

Built-in and non-built-in packages should be separated and the built-in packages placed first

an00bhav avatar Aug 10 '20 11:08 an00bhav

"File is not goimports-ed (goimports)" is a really bad message, confusing, wasting time trying to understand what it means.

gillesdouaire avatar Sep 11 '20 16:09 gillesdouaire

Echoing the "what? my vim config runs gofmt automatically, and it doesn't care, so why the hell does golangci?" sentiment. The error

now it's too late to change it: some users have already added this text to exclude list.

@jirfag да блин, кто они? наверно, если эти юзери, ну «перегрузить» бы свои exclude lists, многие другие юзери улыбнулись бы 🤣 ))))))

It's not immediately obvious what to do when this occurs (simply running goimports prints a fixed version to stdout, which neither fixes the issue nor makes it clear where it is). Perhaps:

<file> has improperly-formatted imports. Run `goimports -d <file>` to see the issue or `goimports -w <file>` to fix it in-place.

That offers both a command that shows the issue only, and a command that will just fix it.

rainest avatar Sep 12 '20 04:09 rainest

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 14 '21 07:09 stale[bot]

@rainest,

has improperly-formatted imports

it's not only about imports - https://github.com/golangci/golangci-lint/issues/138#issuecomment-501268312 and https://github.com/golangci/golangci-lint/issues/138#issuecomment-605858864

So it's probably the main confusion with this message when one trying to "fix" imports, but the problem is another place.

vtopc avatar Sep 14 '21 17:09 vtopc

we already had this discussion and decided that the message will be changed in v2

SVilgelm avatar Sep 15 '21 00:09 SVilgelm

So, how do I fix this error? Neither gofmt -w no goimports -w fixes it. This reads like gobbledygook to me. I try to permutate imports for now without using IDE to avoid it wrongly arranging imports.

konstmonst avatar Oct 26 '21 18:10 konstmonst

So, how do I fix this error? Neither gofmt -w no goimports -w fixes it. This reads like gobbledygook to me. I try to permutate imports for now without using IDE to avoid it wrongly arranging imports.

From https://github.com/golangci/golangci-lint/issues/1462#issuecomment-713840286

golangci-lint run --fix

bombsimon avatar Oct 27 '21 07:10 bombsimon

So, how do I fix this error? Neither gofmt -w no goimports -w fixes it. This reads like gobbledygook to me. I try to permutate imports for now without using IDE to avoid it wrongly arranging imports.

From #1462 (comment)

golangci-lint run --fix

Thanks for the answer, but this doesn't work. It sometimes changes the order of imports (but the error stays the same) or sometimes removes imports it doesn't like all together

konstmonst avatar Oct 27 '21 08:10 konstmonst

I could fix it with goimports -w -local github.com/proj-name . && gci -w -local github.com/proj-name . && golangci-lint run --allow-parallel-runners

You have to replace proj-name with your github project

konstmonst avatar Oct 27 '21 08:10 konstmonst

As gci and goimport are modifying the same section of the code, you have to disable one of these 2 linters, run golangci-lint then restore the linter.

Note: I will flag this discussion as offtopic.

ldez avatar Oct 27 '21 08:10 ldez

Faced similar issue , fix for this is golangci-lint run --fix

Kritikazepto avatar Mar 19 '24 06:03 Kritikazepto