Skip to content

Adding import "C" makes warnings for some linters to go away #2910

Closed
@kolyshkin

Description

@kolyshkin

Welcome

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've included all information below (version, config, etc).
  • Yes, I've tried with the standalone linter if available. (https://golangci-lint.run/usage/linters/)

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.

[kir@kir-rhat golangci-lint]$ pwd; git status; git show | head -2
/home/kir/go/src/github.com/golangci/golangci-lint
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
commit c531fc2ad559624436ff66ca77aa306f47c47442
Author: Marat Reymers <[email protected]>

[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable gofmt test/testdata/cgo/main.go 
[kir@kir-rhat golangci-lint]$ echo $?
0

[kir@kir-rhat golangci-lint]$ echo >> test/testdata/cgo/main.go
[kir@kir-rhat golangci-lint]$ git diff
diff --git a/test/testdata/cgo/main.go b/test/testdata/cgo/main.go
index cbb692fe..4df7d95d 100644
--- a/test/testdata/cgo/main.go
+++ b/test/testdata/cgo/main.go
@@ -19,3 +19,4 @@ func Example() {
        C.myprint(cs)
        C.free(unsafe.Pointer(cs))
 }
+

[kir@kir-rhat golangci-lint]$ gofmt -d test/testdata/cgo/main.go
diff -u test/testdata/cgo/main.go.orig test/testdata/cgo/main.go
--- test/testdata/cgo/main.go.orig	2022-06-06 19:18:55.848930364 -0700
+++ test/testdata/cgo/main.go	2022-06-06 19:18:55.848930364 -0700
@@ -19,4 +19,3 @@
 	C.myprint(cs)
 	C.free(unsafe.Pointer(cs))
 }
-

[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable gofmt test/testdata/cgo/main.go 
[kir@kir-rhat golangci-lint]$ echo $?
0

If we try doing the same thing with the file without import "C", it all works as expected:

[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable gofmt test/testdata/godot.go 
[kir@kir-rhat golangci-lint]$ echo $?
0
[kir@kir-rhat golangci-lint]$ echo >> test/testdata/godot.go
[kir@kir-rhat golangci-lint]$ golangci-lint run --no-config --disable-all --enable gofmt test/testdata/godot.go 
test/testdata/godot.go:8: File is not `gofmt`-ed with `-s` (gofmt)

Now, let's add import "C":

[kir@kir-rhat golangci-lint]$ git reset --hard HEAD
HEAD is now at c531fc2a gosec: allow `global` config (#2880)
kir@kir-rhat golangci-lint]$ vim test/testdata/godot.go
[kir@kir-rhat golangci-lint]$ git diff
diff --git a/test/testdata/godot.go b/test/testdata/godot.go
index 819ce941..73e888e1 100644
--- a/test/testdata/godot.go
+++ b/test/testdata/godot.go
@@ -1,6 +1,8 @@
 //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 gofmt test/testdata/godot.go 
[kir@kir-rhat golangci-lint]$ echo >> test/testdata/godot.go
[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 gofmt test/testdata/godot.go 
[kir@kir-rhat golangci-lint]$ echo $?
0

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

Standard .golangci.yml from this repo. Unrelated, since I have used `--no-config` in the above repro.

Go environment

$ go version && go env
go version go1.18.1 linux/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/kir/.cache/go-build"
GOENV="/home/kir/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/kir/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/kir/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/kir/sdk/go1.18.1"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/kir/sdk/go1.18.1/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/kir/go/src/github.com/golangci/golangci-lint/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1837761077=/tmp/go-build -gno-record-gcc-switches"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
# paste output here

Cleaning the cache does not change anything.

Code example or link to a public repository

See the repro above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: cgoRelated to CGO or line directivesbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions