golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
15.22k stars 1.36k forks source link

Could not Import C (cgo preprocessing failed) (typecheck) #3289

Closed csmithv closed 1 year ago

csmithv commented 1 year ago

Welcome

Description of the problem

Hello getting the following error on my go project anytime I try to run golanci-lint on it. There was a very similar bug posted on this repo, but I think that this might be getting caused by a different issue. For background, this was correctly running yesterday, I did not change anything in the code, and my make command started failing with this issue. I know that's difficult to believe, but I even reverted to the last github push that I was confident ran "make" correctly and it started failing.

Some common errors I have checked: gcc is installed with version 9.4.0

Version of golangci-lint

```console $ golangci-lint --version golangci-lint has version 1.50.0 built from 704109c6 on 2022-10-10T01:29:44Z ```

Configuration file

```console $ cat .golangci.yml run: timeout: 5m modules-download-mode: readonly linters-settings: gofmt: simplify: true goimports: local-prefixes: git-cluster.virtu.com/projects/SYS/repos/mattermost_plugins/compliance-plugin golint: min-confidence: 0 govet: check-shadowing: true enable-all: true disable: - fieldalignment misspell: locale: US linters: disable-all: true enable: - bodyclose #- deadcode - errcheck - gocritic - gofmt - goimports #- golint - revive - gosec - gosimple - govet - ineffassign - misspell - nakedret - staticcheck #- structcheck - stylecheck - typecheck - unconvert - unused #- varcheck - whitespace issues: exclude-rules: - path: server/manifest.go linters: - unused - path: server/configuration.go linters: - unused - path: _test\.go linters: - bodyclose - scopelint # https://github.com/kyoh86/scopelint/issues/4 ```

Go environment

```console $ go version && go env go version go1.19.1 linux/amd64 GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/csmith/.cache/go-build" GOENV="/home/csmith/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/csmith/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/csmith/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19.1" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/home/csmith/Code/goProjects/mattermost_plugins/com.compliance-plugin/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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build899707641=/tmp/go-build -gno-record-gcc-switches" ```

Verbose output of running

```console $ golangci-lint cache clean $ golangci-lint run -v INFO [config_reader] Config search paths: [./ /home/csmith/Code/goProjects/mattermost_plugins/com.compliance-plugin /home/csmith/Code/goProjects/mattermost_plugins /home/csmith/Code/goProjects /home/csmith/Code /home/csmith /home / /home/csmith/snap/golangci-lint/99] INFO [config_reader] Used config file .golangci.yml INFO [lintersdb] Active 18 linters: [bodyclose errcheck gocritic gofmt goimports gosec gosimple govet ineffassign misspell nakedret revive staticcheck stylecheck typecheck unconvert unused whitespace] INFO [loader] Go packages loading at mode 575 (exports_file|types_sizes|files|imports|name|compiled_files|deps) took 474.589304ms INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 1.681516ms INFO [linters_context/goanalysis] analyzers took 15.260912949s with top 10 stages: buildir: 11.056493377s, inspect: 951.68857ms, findcall: 781.12351ms, fact_deprecated: 621.707111ms, ctrlflow: 568.395668ms, printf: 498.239616ms, nilness: 227.431331ms, fact_purity: 220.532607ms, SA5012: 203.311228ms, typedness: 131.108218ms INFO [runner] Issues before processing: 10, after processing: 1 INFO [runner] Processors filtering stat (out/in): autogenerated_exclude: 10/10, exclude-rules: 10/10, path_prettifier: 10/10, skip_files: 10/10, identifier_marker: 10/10, filename_unadjuster: 10/10, skip_dirs: 10/10, severity-rules: 1/1, path_prefixer: 1/1, sort_results: 1/1, source_code: 1/1, path_shortener: 1/1, nolint: 10/10, uniq_by_line: 1/10, diff: 1/1, max_per_file_from_linter: 1/1, max_same_issues: 1/1, max_from_linter: 1/1, cgo: 10/10, exclude: 10/10 INFO [runner] processing took 619.364µs with stages: exclude-rules: 269.28µs, identifier_marker: 177.508µs, nolint: 54.749µs, path_prettifier: 48.073µs, skip_dirs: 40.124µs, source_code: 14.34µs, uniq_by_line: 3.479µs, cgo: 2.119µs, autogenerated_exclude: 1.66µs, filename_unadjuster: 1.609µs, max_same_issues: 1.54µs, max_per_file_from_linter: 927ns, max_from_linter: 922ns, path_shortener: 863ns, severity-rules: 538ns, sort_results: 515ns, skip_files: 373ns, diff: 372ns, exclude: 254ns, path_prefixer: 119ns INFO [runner] linters took 3.6568462s with stages: goanalysis_metalinter: 3.656135589s ../../../../../../snap/golangci-lint/99/src/runtime/cgo/cgo.go:34:8: could not import C (cgo preprocessing failed) (typecheck) import "C" ^ INFO File cache stats: 1 entries of total size 862B INFO Memory: 43 samples, avg is 354.5MB, max is 647.7MB INFO Execution took 4.140228964s ```

Code example or link to a public repository

```MAKEFILE GO ?= $(shell command -v go 2> /dev/null) NPM ?= $(shell command -v npm 2> /dev/null) CURL ?= $(shell command -v curl 2> /dev/null) MM_DEBUG ?= MANIFEST_FILE ?= plugin.json GOPATH ?= $(shell go env GOPATH) GO_TEST_FLAGS ?= -race GO_BUILD_FLAGS ?= MM_UTILITIES_DIR ?= ../mattermost-utilities DLV_DEBUG_PORT := 2346 DEFAULT_GOOS := $(shell go env GOOS) DEFAULT_GOARCH := $(shell go env GOARCH) export GO111MODULE=on # You can include assets this directory into the bundle. This can be e.g. used to include profile pictures. ASSETS_DIR ?= assets ## Define the default target (make all) .PHONY: default default: all # Verify environment, and define PLUGIN_ID, PLUGIN_VERSION, HAS_SERVER and HAS_WEBAPP as needed. include build/setup.mk include build/legacy.mk BUNDLE_NAME ?= $(PLUGIN_ID)-$(PLUGIN_VERSION).tar.gz # Include custom makefile, if present ifneq ($(wildcard build/custom.mk),) include build/custom.mk endif ## Checks the code style, tests, builds and bundles the plugin. .PHONY: all all: check-style test dist ## Runs eslint and golangci-lint .PHONY: check-style check-style: webapp/node_modules @echo Checking for style guide compliance ifneq ($(HAS_WEBAPP),) cd webapp && npm run lint cd webapp && npm run check-types endif ifneq ($(HAS_SERVER),) @if ! [ -x "$$(command -v golangci-lint)" ]; then \ echo "golangci-lint is not installed. Please see https://github.com/golangci/golangci-lint#install for installation instructions."; \ exit 1; \ fi; \ @echo Running golangci-lint golangci-lint run ./... endif ## Builds the server, if it exists, for all supported architectures, unless MM_SERVICESETTINGS_ENABLEDEVELOPER is set. .PHONY: server server: ifneq ($(HAS_SERVER),) mkdir -p server/dist; ifeq ($(MM_DEBUG),) ifneq ($(MM_SERVICESETTINGS_ENABLEDEVELOPER),) @echo Building plugin only for $(DEFAULT_GOOS)-$(DEFAULT_GOARCH) because MM_SERVICESETTINGS_ENABLEDEVELOPER is enabled cd server && $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-$(DEFAULT_GOOS)-$(DEFAULT_GOARCH); else cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-linux-amd64; cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-darwin-amd64; cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -trimpath -o dist/plugin-windows-amd64.exe; endif else $(info DEBUG mode is on; to disable, unset MM_DEBUG) ifneq ($(MM_SERVICESETTINGS_ENABLEDEVELOPER),) @echo Building plugin only for $(DEFAULT_GOOS)-$(DEFAULT_GOARCH) because MM_SERVICESETTINGS_ENABLEDEVELOPER is enabled cd server && $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -trimpath -o dist/plugin-$(DEFAULT_GOOS)-$(DEFAULT_GOARCH); else cd server && env GOOS=linux GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -trimpath -o dist/plugin-linux-amd64; cd server && env GOOS=darwin GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -trimpath -o dist/plugin-darwin-amd64; cd server && env GOOS=windows GOARCH=amd64 $(GO) build $(GO_BUILD_FLAGS) -gcflags "all=-N -l" -trimpath -o dist/plugin-windows-amd64.exe; endif endif endif ## Ensures NPM dependencies are installed without having to run this all the time. webapp/node_modules: $(wildcard webapp/package.json) ifneq ($(HAS_WEBAPP),) cd webapp && $(NPM) install touch $@ endif ## Builds the webapp, if it exists. .PHONY: webapp webapp: webapp/node_modules ifneq ($(HAS_WEBAPP),) ifeq ($(MM_DEBUG),) cd webapp && $(NPM) run build; else cd webapp && $(NPM) run debug; endif endif ## Generates a tar bundle of the plugin for install. .PHONY: bundle bundle: rm -rf dist/ mkdir -p dist/$(PLUGIN_ID) cp $(MANIFEST_FILE) dist/$(PLUGIN_ID)/ ifneq ($(wildcard $(ASSETS_DIR)/.),) cp -r $(ASSETS_DIR) dist/$(PLUGIN_ID)/ endif ifneq ($(HAS_PUBLIC),) cp -r public dist/$(PLUGIN_ID)/ endif ifneq ($(HAS_SERVER),) mkdir -p dist/$(PLUGIN_ID)/server cp -r server/dist dist/$(PLUGIN_ID)/server/ endif ifneq ($(HAS_WEBAPP),) mkdir -p dist/$(PLUGIN_ID)/webapp cp -r webapp/dist dist/$(PLUGIN_ID)/webapp/ endif cd dist && tar -cvzf $(BUNDLE_NAME) $(PLUGIN_ID) @echo plugin built at: dist/$(BUNDLE_NAME) ## Builds and bundles the plugin. .PHONY: dist dist: server webapp bundle ## Builds and installs the plugin to a server. .PHONY: deploy deploy: dist ./build/bin/pluginctl deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME) ## Builds and installs the plugin to a server, updating the webapp automatically when changed. .PHONY: watch watch: server bundle ifeq ($(MM_DEBUG),) cd webapp && $(NPM) run build:watch else cd webapp && $(NPM) run debug:watch endif ## Installs a previous built plugin with updated webpack assets to a server. .PHONY: deploy-from-watch deploy-from-watch: bundle ./build/bin/pluginctl deploy $(PLUGIN_ID) dist/$(BUNDLE_NAME) ## Setup dlv for attaching, identifying the plugin PID for other targets. .PHONY: setup-attach setup-attach: $(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}')) $(eval NUM_PID := $(shell echo -n ${PLUGIN_PID} | wc -w)) @if [ ${NUM_PID} -gt 2 ]; then \ echo "** There is more than 1 plugin process running. Run 'make kill reset' to restart just one."; \ exit 1; \ fi ## Check if setup-attach succeeded. .PHONY: check-attach check-attach: @if [ -z ${PLUGIN_PID} ]; then \ echo "Could not find plugin PID; the plugin is not running. Exiting."; \ exit 1; \ else \ echo "Located Plugin running with PID: ${PLUGIN_PID}"; \ fi ## Attach dlv to an existing plugin instance. .PHONY: attach attach: setup-attach check-attach dlv attach ${PLUGIN_PID} ## Attach dlv to an existing plugin instance, exposing a headless instance on $DLV_DEBUG_PORT. .PHONY: attach-headless attach-headless: setup-attach check-attach dlv attach ${PLUGIN_PID} --listen :$(DLV_DEBUG_PORT) --headless=true --api-version=2 --accept-multiclient ## Detach dlv from an existing plugin instance, if previously attached. .PHONY: detach detach: setup-attach @DELVE_PID=$(shell ps aux | grep "dlv attach ${PLUGIN_PID}" | grep -v "grep" | awk -F " " '{print $$2}') && \ if [ "$$DELVE_PID" -gt 0 ] > /dev/null 2>&1 ; then \ echo "Located existing delve process running with PID: $$DELVE_PID. Killing." ; \ kill -9 $$DELVE_PID ; \ fi ## Runs any lints and unit tests defined for the server and webapp, if they exist. .PHONY: test test: webapp/node_modules ifneq ($(HAS_SERVER),) $(GO) test -v $(GO_TEST_FLAGS) ./server/... endif ifneq ($(HAS_WEBAPP),) cd webapp && $(NPM) run test; endif ifneq ($(wildcard ./build/sync/plan/.),) cd ./build/sync && $(GO) test -v $(GO_TEST_FLAGS) ./... endif ## Creates a coverage report for the server code. .PHONY: coverage coverage: webapp/node_modules ifneq ($(HAS_SERVER),) $(GO) test $(GO_TEST_FLAGS) -coverprofile=server/coverage.txt ./server/... $(GO) tool cover -html=server/coverage.txt endif ## Extract strings for translation from the source code. .PHONY: i18n-extract i18n-extract: ifneq ($(HAS_WEBAPP),) ifeq ($(HAS_MM_UTILITIES),) @echo "You must clone github.com/mattermost/mattermost-utilities repo in .. to use this command" else cd $(MM_UTILITIES_DIR) && npm install && npm run babel && node mmjstool/build/index.js i18n extract-webapp --webapp-dir $(PWD)/webapp endif endif ## Disable the plugin. .PHONY: disable disable: detach ./build/bin/pluginctl disable $(PLUGIN_ID) ## Enable the plugin. .PHONY: enable enable: ./build/bin/pluginctl enable $(PLUGIN_ID) ## Reset the plugin, effectively disabling and re-enabling it on the server. .PHONY: reset reset: detach ./build/bin/pluginctl reset $(PLUGIN_ID) ## Kill all instances of the plugin, detaching any existing dlv instance. .PHONY: kill kill: detach $(eval PLUGIN_PID := $(shell ps aux | grep "plugins/${PLUGIN_ID}" | grep -v "grep" | awk -F " " '{print $$2}')) @for PID in ${PLUGIN_PID}; do \ echo "Killing plugin pid $$PID"; \ kill -9 $$PID; \ done; \ ## Clean removes all build artifacts. .PHONY: clean clean: rm -fr dist/ ifneq ($(HAS_SERVER),) rm -fr server/coverage.txt rm -fr server/dist endif ifneq ($(HAS_WEBAPP),) rm -fr webapp/junit.xml rm -fr webapp/dist rm -fr webapp/node_modules endif rm -fr build/bin/ ## Sync directory with a starter template sync: ifndef STARTERTEMPLATE_PATH @echo STARTERTEMPLATE_PATH is not set. @echo Set STARTERTEMPLATE_PATH to a local clone of https://github.com/mattermost/mattermost-plugin-starter-template and retry. @exit 1 endif cd ${STARTERTEMPLATE_PATH} && go run ./build/sync/main.go ./build/sync/plan.yml $(PWD) # Help documentation à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html help: @cat Makefile build/*.mk | grep -v '\.PHONY' | grep -v '\help:' | grep -B1 -E '^[a-zA-Z0-9_.-]+:.*' | sed -e "s/:.*//" | sed -e "s/^## //" | grep -v '\-\-' | sed '1!G;h;$$!d' | awk 'NR%2{printf "\033[36m%-30s\033[0m",$$0;next;}1' | sort ```
boring-cyborg[bot] commented 1 year ago

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

bombsimon commented 1 year ago

What is the Go code you're linting? Your Make file only does golangci-lint run ./... so what's interesting is a minimal reproducible example with Go/C code.

A heads up is that typecheck is not a real linter but a way to detect compilation issues. So one thing that would be nice to try and attach would be the result of go build ./.... If that does not compile it's not an issue with golangci-lint.

logan-bobo commented 1 year ago

I am also seeing the same issue when using an alpine image getting the following error. On the latest build of golangci-lint and Go


golangci-lint run
../../../../usr/local/go/src/runtime/cgo/cgo.go:33:8: could not import C (cgo preprocessing failed) (typecheck)
import "C"
       ^
logan-bobo commented 1 year ago

Okay so my issue here is I needed to install the build-base package on alpine by running apk add build-base @csmithv you may need to resolve some OS level dependency.

l0rem1psum commented 1 year ago

Encountered the same issue on Ubuntu, solved by running apt install build-essential.

roskee commented 1 year ago

Encountered this same issue yesterday. I do have build-essential and gcc installed. but the error still exists. the exact message is as follows. It also prints incorrect errors before the cgo error.

...
...:37:6: undeclared name: `redis` (typecheck)
    DB *redis.Client
        ^
...: "github.com/go-redis/redis/v8" imported but not used (typecheck)
    "github.com/go-redis/redis/v8"

../../../../../snap/golangci-lint/104/src/runtime/cgo/cgo.go:34:8: could not import C (cgo preprocessing failed) (typecheck)
import "C"
roskee commented 1 year ago

Apparently the reason was snap. I removed my snap installation of golangci-lint and installed the binary and it worked

emanuelef commented 1 year ago

Had the same issue when using it in a Github action and fixed with apt install build-essential. What was weird is that the go build ./... was working even without installing build essential and only golangci-lint run ./... was failing.

AlaaRdwan94 commented 1 year ago

Encountered the same issue on Mac M1 , solved after update brew and reinstall it brew update brew install golangci-lint

cytown commented 1 year ago

Still the same issue under alpine linux.

I use lint in docker jenkins/jenkins:lts-alpine.

+ golangci-lint --version
golangci-lint has version v1.50.1 built from (unknown, mod sum: "h1:C829clMcZXEORakZlwpk7M4iDw2XiwxxKaG504SZ9zY=") on (unknown)
+ make lint
# run golangci-lint
golangci-lint run
app/shentong/aci.go:4:8: could not import C (cgo preprocessing failed) (typecheck)
import "C"
       ^
make: *** [Makefile:78: lint] Error 1
Build step 'Execute shell' marked build as failure
[SSH] executing post build script:
apprehensions commented 1 year ago

image