go-tools icon indicating copy to clipboard operation
go-tools copied to clipboard

import "C" not working after staticcheck upgrade. New loader regression?

Open dzbarsky opened this issue 4 years ago • 9 comments

  • The output of 'staticcheck -version' staticcheck_1.12_bin 2019.2.1
  • The output of 'staticcheck -debug.version' (it is fine if this command fails)
staticcheck_1.12_bin 2019.2.1

Compiled with Go version: go1.12.9
  • The output of 'go version' go version go1.12.9 linux/amd64
  • The output of 'go env'
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/zbarsky/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/zbarsky/src/server/go"
GOPROXY=""
GORACE=""
GOROOT="/home/zbarsky/src/server/bazel-server/external/go1_12_9_linux_amd64_tar_gz/go"
GOTMPDIR=""
GOTOOLDIR="/home/zbarsky/src/server/bazel-server/external/go1_12_9_linux_amd64_tar_gz/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS="-I/home/zbarsky/src/server -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/leveldb/include/ -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/org_linuxcontainers_lxc/src/ -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/percona_server/include/ -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/rdkafka/ -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/rocksdb/include/ -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/zookeeper_3_4_6/include/ -I/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/com_github_dropbox_libseccomp/include/"
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-build280212957=/tmp/go-build -gno-record-gcc-switches"
  • Exactly which command you ran ['/mnt/cache/home/zbarsky/.cache/bazel/_bazel_zbarsky/9beb5023491c0352a77da06871417523/execroot/__main__/bazel-out/k8-fastbuild/bin/tools/go-staticcheck.runfiles/__main__/go/src/honnef.co/go/tools/cmd/staticcheck/staticcheck_1.12', 'afs/afsd/...']
  • Output of the command and what's wrong with the output
-: could not analyze dependency afs/afsd [afs/afsd.test] of afs/afsd.test (compile)
-: could not analyze dependency util/store/rocksdb of afs/afsd [afs/afsd.test]:
	could not analyze dependency util/store/rocksdb/levigo_rocksdb of util/store/rocksdb:
	/home/zbarsky/src/server/go/src/util/store/rocksdb/levigo_rocksdb/batch.go:4:8: could not import C (Config.Importer.Import(C) returned nil but no error) (compile)
  • Where we can read the code you're running staticcheck on (GitHub repo, link to playground, code embedded in the issue, ...) Batch.go file from above:
package levigo_rocksdb

// #include "rocksdb/c.h"
import "C"

import (
        "unsafe"
)

type WriteBatch struct {
        wbatch *C.rocksdb_writebatch_t
}
<more code, not relevant>

Our setup is a bit weird due to use of bazel, so I'm hoping you can provide me some guidance to track this down but understand you may not be able to repro. How is "import C"/cgo supposed to work? staticcheck is using a custom importer:

importer := func(path string) (*types.Package, error) {
                if path == "unsafe" {
                        return types.Unsafe, nil
                }
                imp := pkg.Imports[path]
                if imp == nil {
                        return nil, nil
                }
                if len(imp.Errors) > 0 {
                        return nil, imp.Errors[0]
                }
                return imp.Types, nil
        }

With this code, pkg.Imports does not contain "C" so we return nil, is something else supposed to handle cgo? I believe the loading mechanism has changed, since this does work correctly in staticcheck from revision f68e85e63525b16caea5b5e587dad88f1646ddda. Any pointers?

dzbarsky avatar Aug 27 '19 15:08 dzbarsky