golangci-lint
golangci-lint copied to clipboard
confusing error message "File is not goimports-ed (goimports)"
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)"
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.
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 😇
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.
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).
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.
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
@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
I just ran into this problem too. A better message would have gotten me to a fix quicker.
+1; Human readable message is expected and "File is not goimportes-ed" confused me
- What message do you want to see?
Please, run goimports tool for this file? - Have you tried
--fixoption?
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, 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
i have the same error, but run goimports -w on file does not bring any differences.
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).
@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:
go get -u golang.org/x/tools/cmd/goimports- go mod tidy
- go mod vendor
- go fmt //==> formatting code
- goimports -w ==> clean imports
- gofmt -s -w ==> simplify code
@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.
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"
)
+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
"File is not goimports-ed (goimports)" is a really bad message, confusing, wasting time trying to understand what it means.
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.
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.
@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.
we already had this discussion and decided that the message will be changed in v2
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.
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
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
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
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.
Faced similar issue , fix for this is golangci-lint run --fix