-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding import "C" makes warnings for some linters to go away #2910
Comments
Here's an example of godot that stops working: [kir@kir-rhat golangci-lint]$ git diff
diff --git a/test/testdata/godot.go b/test/testdata/godot.go
index 819ce941..e258956b 100644
--- a/test/testdata/godot.go
+++ b/test/testdata/godot.go
@@ -1,7 +1,10 @@
//args: -Egodot
package testdata
+import "C"
+
// Godot checks top-level comments // ERROR "Comment should end in a period"
func Godot() {
// nothing to do here
}
+
[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable godot test/testdata/godot.go
[kir@kir-rhat golangci-lint]$ echo $?
0 |
OK, looks like some linters are affected, and some are not. Say, [kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable ineffassign test/testdata/ineffassign.go
test/testdata/ineffassign.go:10:3: ineffectual assignment to x (ineffassign)
x = 0 // ERROR "ineffectual assignment to x"
^
[kir@kir-rhat golangci-lint]$ vim test/testdata/ineffassign.go
[kir@kir-rhat golangci-lint]$ git diff test/testdata/ineffassign.go
diff --git a/test/testdata/ineffassign.go b/test/testdata/ineffassign.go
index 69d3cc81..6f4d93ff 100644
--- a/test/testdata/ineffassign.go
+++ b/test/testdata/ineffassign.go
@@ -1,6 +1,8 @@
//args: -Eineffassign
package testdata
+import "C"
+
import "math"
func _() {
[kir@kir-rhat golangci-lint]$ CGO_ENABLED=1 golangci-lint run --build-tags cgo --no-config --disable-all --enable ineffassign test/testdata/ineffassign.go
[kir@kir-rhat golangci-lint]$ echo $?
0 but [kir@kir-rhat golangci-lint]$ git diff test/testdata/deadcode.go
[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable deadcode test/testdata/deadcode.go
test/testdata/deadcode.go:6:5: `unused` is unused (deadcode)
var unused int // ERROR "`unused` is unused"
^
test/testdata/deadcode.go:11:6: `g` is unused (deadcode)
func g(x int) { // ERROR "`g` is unused"
^
[kir@kir-rhat golangci-lint]$ vim test/testdata/deadcode.go
[kir@kir-rhat golangci-lint]$ git diff test/testdata/deadcode.go
diff --git a/test/testdata/deadcode.go b/test/testdata/deadcode.go
index 82379efd..520a89db 100644
--- a/test/testdata/deadcode.go
+++ b/test/testdata/deadcode.go
@@ -1,6 +1,8 @@
//args: -Edeadcode
package testdata
+import "C"
+
var y int
var unused int // ERROR "`unused` is unused"
[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable deadcode test/testdata/deadcode.go
test/testdata/deadcode.go:8:5: `unused` is unused (deadcode)
var unused int // ERROR "`unused` is unused"
^
test/testdata/deadcode.go:13:6: `g` is unused (deadcode)
func g(x int) { // ERROR "`g` is unused"
^
[kir@kir-rhat golangci-lint]$
|
Linters that are affected:
Linters that are not affected:
|
To check which linters are affected: $ pwd
/home/kir/go/src/github.com/golangci/golangci-lint
$ cd test
$ # Add import "C" to every test file in testdata.
$ for f in testdata/*.go; do grep -q '^import "C"$' $f || sed -i -s 's/^package testdata$/\0\n\nimport "C"/' $f; done
$ go test -run TestSourcesFromTestdataWithIssuesDir (there are some false positives) |
I checked various versions and it seems that v1.26.0 is working as it should, and v1.27.0 does not. Next, I did a git-bisect using Indeed, reverting it (partially) helps: [kir@kir-rhat golangci-lint]$ git diff test/testdata/gofmt.go
diff --git a/test/testdata/gofmt.go b/test/testdata/gofmt.go
index 61c93d28..f4bb8b5c 100644
--- a/test/testdata/gofmt.go
+++ b/test/testdata/gofmt.go
@@ -1,6 +1,8 @@
//args: -Egofmt
package testdata
+import "C"
+
import "fmt"
func GofmtNotSimplified() {
[kir@kir-rhat golangci-lint]$ ./golangci-lint run --no-config --disable-all -Egofmt ./test/testdata/gofmt.go
[kir@kir-rhat golangci-lint]$ # ^^^ ignoring the issue
[kir@kir-rhat golangci-lint]$ patch -p1 < 1.diff
patching file pkg/golinters/gofmt.go
[kir@kir-rhat golangci-lint]$ git diff pkg test/testdata/gofmt.go
diff --git a/pkg/golinters/gofmt.go b/pkg/golinters/gofmt.go
index 1d50bfc5..03640f35 100644
--- a/pkg/golinters/gofmt.go
+++ b/pkg/golinters/gofmt.go
@@ -55,7 +55,7 @@ func NewGofmt(settings *config.GoFmtSettings) *goanalysis.Linter {
func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoFmtSettings) ([]goanalysis.Issue, error) {
var fileNames []string
for _, f := range pass.Files {
- pos := pass.Fset.PositionFor(f.Pos(), false)
+ pos := pass.Fset.Position(f.Pos())
fileNames = append(fileNames, pos.Filename)
}
diff --git a/test/testdata/gofmt.go b/test/testdata/gofmt.go
index 61c93d28..f4bb8b5c 100644
--- a/test/testdata/gofmt.go
+++ b/test/testdata/gofmt.go
@@ -1,6 +1,8 @@
//args: -Egofmt
package testdata
+import "C"
+
import "fmt"
func GofmtNotSimplified() {
[kir@kir-rhat golangci-lint]$ make build
go build -o golangci-lint ./cmd/golangci-lint
[kir@kir-rhat golangci-lint]$ ./golangci-lint run --no-config --disable-all -Egofmt ./test/testdata/gofmt.go
test/testdata/gofmt.go:10: File is not `gofmt`-ed with `-s` (gofmt)
fmt.Print(x[1:len(x)]) // ERROR "File is not `gofmt`-ed with `-s`"
[kir@kir-rhat golangci-lint]$ # ^^^ No longer ignoring the issue
[kir@kir-rhat golangci-lint]$ |
Since I don't have any idea of how all this works, I need help fixing this. @ksoichiro @ldez @idenx perhaps you can take a look 🙏🏻 |
@jirfag PTAL 🙏🏻 |
Great find @kolyshkin! This is in fact what is keeping me from using golangci-lint in the CI of our highly cgo-entangled binding-library. We use the standalone |
This change fixes a bug caused that many linters ignored all files that are using Cgo. It was introduced by PR golangci#1065, which assumed that all files referenced by //line directives are non-Go files, which is not true. In the case of Cgo they point to the original Go files which are used by Cgo as templates to generate other Go files. The fix replaces all calls of Pass.Fset.PositionFor with a function `getFileNames()` that first calls Pass.Fset.PositionFor with adjustment, and only if it results in a non-Go file it calls Pass.Fset.PositionFor again without adjustment. Fixes: golangci#2910 Signed-off-by: Sven Anderson <[email protected]>
This change fixes a bug caused that many linters ignored all files that are using Cgo. It was introduced by PR golangci#1065, which assumed that all files referenced by //line directives are non-Go files, which is not true. In the case of Cgo they point to the original Go files which are used by Cgo as templates to generate other Go files. The fix replaces all calls of Pass.Fset.PositionFor with a function getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only if it results in a non-Go file it calls Pass.Fset.PositionFor again without adjustment. Fixes: golangci#2910 Signed-off-by: Sven Anderson <[email protected]>
This change fixes a bug caused that many linters ignored all files that are using Cgo. It was introduced by PR golangci#1065, which assumed that all files referenced by //line directives are non-Go files, which is not true. In the case of Cgo they point to the original Go files which are used by Cgo as templates to generate other Go files. The fix replaces all calls of Pass.Fset.PositionFor with a function getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only if it results in a non-Go file it calls Pass.Fset.PositionFor again without adjustment. Fixes: golangci#2910 Signed-off-by: Sven Anderson <[email protected]>
Welcome
Description of the problem
This is a five star bug.
Essentially, it looks like having
import "C"
in the source file makes all the format linters (gofmt, gofumpt, goimports) to stop seeing formatting issues.Here's a quick repro using the golangci-lint repo.
If we try doing the same thing with the file without
import "C"
, it all works as expected:Now, let's add
import "C"
:Version of golangci-lint
$ golangci-lint --version
golangci-lint has version 1.46.2 built from a333689 on 2022-05-17T11:31:29Z
Configuration file
Go environment
Verbose output of running
Cleaning the cache does not change anything.
Code example or link to a public repository
The text was updated successfully, but these errors were encountered: