Open howardjohn opened 4 years ago
Input:
import (
"fmt"
"os"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/framework/components/prometheus"
)
Output:
import (
"fmt"
"os"
"istio.io/istio/pkg/test/framework/components/prometheus"
)
Also can only reproduce through golangci-lint, not direct calls on goimports -local istio.io -w main.go; gci -local istio.io -w main.go
I tried with different cases:
import (
"io/ioutil"
"github.com/golangci/sandbox/pkg/render"
"github.com/ghodss/yaml"
"log"
"encoding/json"
"github.com/golangci/sandbox/pkg/router"
)
import (
"github.com/golangci/sandbox/pkg/render"
"github.com/golangci/sandbox/pkg/router"
"encoding/json"
"io/ioutil"
"log"
"github.com/ghodss/yaml"
)
and all the linters that handle the imports (gofmt
, goimports
, gci
, gofumpt
).
Results:
linters | results |
---|---|
gofmt + goimports | FAIL |
gofmt + gci | FAIL |
goimports + gci | FAIL |
goimports + gofumpt | FAIL |
gofmt + gci + gofumpt | FAIL |
gofmt + goimports + gci + gofumpt | FAIL |
gci + gofumpt | OK |
gofmt + gofumpt | OK |
I think that the problem is not related to a specific linter but to how the fixes are applied.
I'm also seeing this problem. I'll add that with a larger set of imports and more randomization, I'm able to reproduce the failure with the gci + gofumpt
and gofmt + gofumpt
combinations as well.
I've started debugging this issue. I'll try to post more as I have time to dig into the results.
So far, I'm pretty sure the issue is related to one or all of these code paths:
extractIssuesFromPatch
, which analyzes the diff from the linter output to determine what the affected lines are and what action is necessary on those linesmergeLineIssues
(fixer call, func), where the fixer tries to merge all issues from all linters together for a single filefindNotIntersectingIssues
(fixer call, func), where the fixer tries to break fixes up into multiple passes? not 100% sure on that yet@danielhochman, trying to understand:
This is in fixer.go
, would erroneous behavior in those three functions trigger if I weren't using --fix
?
Ignore this comment, something is weirder, I have to do more research
the fixer is only used when --fix
is called.
edit: I removed my previous comment because I misread your comment.
the fixer is only used when
--fix
is called.edit: I removed my previous comment because I misread your comment.
In which case, as all functions you mentioned are in fixer.go
I am not sure the behaviour is related.
I have to do some more research, but I did notice some very strange behaviour when I started doing A/B tests.
I'll have a nice little spreadsheet for you tomorrow. :-)
Ok so I think it's off-topic, you can continue in https://github.com/golangci/golangci-lint/discussions/1875
I recently ran into this as well, and finally found this issue. Thought I was conflicting with other tooling but I've confirmed this behavior is occurring directly when invoked. I tried running golangci-lint run --fix --enable gci,gofumpt --fast --allow-parallel-runners=false
to see if it would help with duplicates but no results. Will have to revert to manually invoking commands.
Hi @ldez isnt this fixed by PR #2653?
After merging PR #2653 both linters should be compatible.
no #2653 will not fix this issue. Since v1.45.0, the support of the autofix with gci has been dropped. #2653 just restore the previous behavior and then the bug.
While this is being fixed, my workaround to remove this error is to put the following in my .golangci.yaml
:
linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/ossf/scorecard) # Replace with your module name
I found this fix here: https://github.com/ossf/scorecard/pull/1664/files.
I'm using mvdan.cc/gofumpt@v0.3.1
and golangci-lint@1.46.2
.
The error I'm getting without the fix above:
gofumpt -l -w ./main.go ./pkg/hello/hello_test.go ./pkg/hello/hello.go
golangci-lint run ./...
pkg/hello/hello_test.go:7:1: Expected '\t', Found '\n' at pkg/hello/hello_test.go[line 7,col 1] (gci)
I'm not sure if this will help you all, but I added the mentioned flags to the VSCode settings and the warning has gone away.
"go.formatTool": "gofumpt",
"go.formatFlags": ["-s"],
"go.lintTool": "golangci-lint",
"go.lintFlags": ["--skip-generated", "-s standard,default"],
P.S. I used gci
and gofumpt
Same for me, but i don't have any small example app
gci + gofumpt OK
I'm getting conflicts between these two in version 1.53.3
golangci-lint run --disable-all --enable gci,gofumpt --fix
in:
import (
"github.com/Crocmagnon/snippetbox/ui"
"net/http"
)
out:
import (
"net/http"
"net/http"
"github.com/Crocmagnon/snippetbox/ui"
)
Running into this issue as well 😞. It seems like the correct thing to do here is to enforce an ordering of linters, applying fixes before running the next linter. gci
recommends running goimports
before gci
.
Unfortunately, it doesn't seem like there's a way to enforce linter ordering with golangci-lint
.
Workaround:
golangci-lint v1.57.0 introduced the --enable-only
flag (https://github.com/golangci/golangci-lint/pull/4438).
After fixing only the imports first (e.g. using gci
), the next run with the usual config works.
golangci-lint run --enable-only gci --fix
golangci-lint run --fix
What I do is disable both of those and use gofumpt only as it includes import sort already try that and maybe you'll no longer have a competing format.
I'm still having this issue with golangci-lint v1.61.0
. Is there any workaround or fix?
I'm still having this issue with
golangci-lint v1.61.0
. Is there any workaround or fix?
Yeah the issue is still open so still active. A workaround is mentioned in https://github.com/golangci/golangci-lint/issues/1490#issuecomment-2079080064
I seem to have fixed it. It looks like it was caused by having a wrong prefix in goimports.local-prefixes
: I copied my config from another project and didn't fix all the configurations. Hope it helps
Config:
Input:
Output:
Output seems nondeterministic
cc @daixiang0