Closed ArjunHariharan closed 2 years ago
Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.
Hello,
I take your example code with your configuration, and I can't reproduce it.
You can try to use the following command to apply the changes from gofumpt:
golangci-lint run --fix
Also, I recommend using v1.45.2
I have the same problem
dima@dima-mibook:badlint$ golangci-lint version
golangci-lint has version 1.46.1 built from 044f0a17 on 2022-05-12T09:23:45Z
And it is reporoduced with example above on my machine.
I put example code at some dir (not under GOPATH), then added cmd/cmdopts/cmdopts.go
file and run go mod init demo
.
When I run golangci-lint run
dima@dima-mibook:badlint$ golangci-lint run
cmd/cmdopts/opts.go:14: File is not `gofumpt`-ed (gofumpt)
main.go:3: File is not `gofumpt`-ed (gofumpt)
import (
main.go:6: File is not `gofumpt`-ed (gofumpt)
"demo/cmd/cmdopts"
If I run with -fix
flag it reorders imports in strange way
import (
"demo/cmd/cmdopts"
"flag"
"fmt"
)
If I run gofumpt -d main.go
it shows that everything is fine, and no changes needed. Gofumpt is v0.3.1
.
I have the same problem
same here
root@icx34:~/zhenhui/polaris-security# gofumpt -w -l .
bootstrap/bootstrap.go
cmd/root.go
server/ca_server.go
root@icx34:~/zhenhui/polaris-security# golangci-lint run -c .golangci.yaml
server/ca_server.go:11: File is not `gofumpt`-ed (gofumpt)
"net/http"
server/ca_server.go:13: File is not `gofumpt`-ed (gofumpt)
"polaris-security/util"
bootstrap/bootstrap.go:9: File is not `gofumpt`-ed (gofumpt)
cmd/root.go:5: File is not `gofumpt`-ed (gofumpt)
looks like golangci-lint
and gofumpt
are having conflict decision in formatting imports?
@ldez I also have the same issue and I've tested versions 1.45.2 as you recommended, as well as 1.46 and the latest 1.47.2. The issue is still there.
The issue has already been outlined by other people in this thread. I.e. golangci-lint
sorts imports differently than gofumpt
yet complains if the file is gofumpt
-ed directly.
If I run golangci-lint run --fix
my main.go file has the imports sorted as follows:
import (
"adrian/pkg/sql"
...
"adrian/pkg/redis"
"context"
...
"time"
However, if I do gofumpt
, the imports will be sorted as follows:
import (
"context"
...
"time"
"adrian/pkg/sql"
...
"adrian/pkg/redis"
The behavior is consistent throughout the different versions of golangci-lint
I've outlined, while gofumpt
is on v0.3.1
.
This particular rule in gofumpt
that golangci-lint
seems to ignore seems to be std imports must be in a separate group at the top
with an example in the gofumpt repo here.
After investigating a bit more, it seems gofumpt
might be the culprit, as the format_test.go
that tests the function that golangci-lint
is using does the same thing that golangci-lint
does; suggesting there's a discrepancy between the gofumpt
binary and the format
package although the formatting rules are specified to be part of the format
package.
I'll open an issue over there as well to see if we can get more eyes on this issue.
Made some progress with the help of the gofumpt
maintainer.
By adding //gofumpt:diagnose
to my files, I realized that golangci-lint
was running without the modpath
being set.
Gofumpt
by default detects the modpath, so this was the reason gofumpt
was running different to golangci-lint
.
I've left //gofumpt:diagnose
in my file, run gofumpt -l -w main.go
so it would give me some details, including the modpath
, and then set the modpath
in .golangci.yml
as per the documentation. After this, golangci-lint
was doing gofumpt the same way gofumpt
was.
I guess it would be nice to have the same modpath
detection inside golangci-lint as gofumpt
has, but till then, hope this helps anyone else in solving their File is not gofumpt-ed
issues.
Edit: based on https://github.com/mvdan/gofumpt/issues/238#issuecomment-1196739009 maybe it would be helpful for golangci-lint
to use a default similar to what gofumpt
uses instead of an empty default ?
For the sake of completeness, this is how the gofumpt tool sets its formatting defaults:
https://github.com/mvdan/gofumpt/blob/8dda8068d9f339047fc1777b688afb66a0a0db17/gofmt.go#L292-L307
In other words, -lang
defaults to the go 1.X
version set in the current go.mod
, and -modpath
defaults to the module some/path
set in the current go.mod
.
and then set the modpath in .golangci.yml as per the documentation
Where did you read this? I can't find anything about it. Where does this setting need to be added?
and then set the modpath in .golangci.yml as per the documentation
Where did you read this? I can't find anything about it. Where does this setting need to be added?
lang-version:
is now handled globally by run.go:
https://golangci-lint.run/usage/configuration/#run-configuration
use Go version from the go.mod file, fallback on the env var
GOVERSION
, fallback on 1.18
@ldez Can this be reopened? Not solved yet, and it clashes with vscode, that uses vanilla gofumpt setting the modpath in .golangci.yaml dictates that each module needs a .golangci.yaml file, which is not very DRY
For me the problem is fixed: the option has been added.
I suggest to open a new issue with a reproducible example if the use of run.go
doesn't fix your problem.
For me the problem is fixed: the option has been added. I suggest to open a new issue with a reproducible example if the use of
run.go
doesn't fix your problem.
The option of what - selecting go version? The problem that is at the core of this issue is that golangci-lint does not automatically detect the modpath, and that has still not been solved. The question of how the go version is resolved was secondary. This issue has valuable research on the problem, so it seems more appropriate to reopen this one.
please open a new issue https://github.com/golangci/golangci-lint/issues/new/choose
FYI golangci-lint automatically detects go version inside the go.mod
.
https://golangci-lint.run/usage/configuration/#run-configuration
for lazy people
linters-settings:
gofumpt:
module-path: <your module path>
Welcome
Description of the problem
golangci-lint throws an error
File is not
gofumpt-ed
on properly formatted code.Running gofumpt on the same file doesn't throw an error.
Output of gofumpt linter
Output of golangci-lint
Version of golangci-lint
Configuration file
Go environment
Verbose output of running
Code example or link to a public repository