gci icon indicating copy to clipboard operation
gci copied to clipboard

Trailing whitespace in generated files causes gci to fail in golangci-lint

Open cnvergence opened this issue 8 months ago • 1 comments

What version of GCI are you using?

 [v0.13.6] 

Reproduce Steps

 
1. Use `gci` as part of `golangci-lint` v2 in a project. 
2. Include a generated file with trailing whitespace in the imports section. 
3. Run `golangci-lint run` with gci enabled. 

What did you expect to see?

gci should gracefully handle generated files, even if they contain trailing whitespace, and sort imports without errors. This issue is caused by trailing whitespace in the imports section of generated files. It would be helpful if gci could handle such cases more robustly or provide a clearer error message.

What did you see instead?

 
The following error occurs: 
WARN [runner] Can't run linter goanalysis_metalinter: gci: error while running gci: 27:59: expected ';', found "github.com/kcp-dev/logicalcluster/v3" (and 2 more errors) 
ERRO Running error: can't run linter goanalysis_metalinter gci: error while running gci: 27:59: expected ';', found "github.com/kcp-dev/logicalcluster/v3" (and 2 more errors) make: *** [imports] Error 3 

cnvergence avatar May 06 '25 09:05 cnvergence

To be clear, a file with

import (
    "pkg.dev/something"_
)

will cause gci to fail if there is a trailing whitespace after the something" part (where I inserted the _ in this example). And this is simply gci "crashing" (in big quotes), which golangci-lint then treats as "linter broken", not as "file bad", and so the problem bubbles up to the user.

The origin of this issue is that golangci-lint doesn't expose the --skip-generated flag of gci anymore and relies on its own filtering/exclusion mechanism. But this mechanism filters found issues, not input files. And since gci is not causing an issue to be created, but simply "crashes", the exclusion mechanism doesn't work anymore and users of golangci-lint now run into more issues with gci because suddenly gci is ran on more files than before.

Even if the integration in golangci-lint would be different, gci should still not simply fail and explode on a file with misplaced whitespace, which is why we decided to open a ticket here :)

xrstf avatar May 06 '25 11:05 xrstf