golangci / golangci-lint

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

could not import C: failure with confluent-kafka-go in docker alpine #1463

Closed fabriziomoscon closed 4 years ago

fabriziomoscon commented 4 years ago

Thank you for maintaining this great tool @bombsimon !

Please include the following information:

Version of golangci-lint ```console $ golangci-lint --version golangci-lint has version 1.31.0 ```
Config file ```console $ cat .golangci.yml # options for analysis running run: # default concurrency is a available CPU number concurrency: 4 # timeout for analysis, e.g. 30s, 5m, default is 1m deadline: 1m skip-files: - mock.go modules-download-mode: vendor # all available settings of specific linters linters-settings: errcheck: # report about not checking of errors in type assetions: `a := b.(MyStruct)`; check-type-assertions: true # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`; check-blank: true govet: # report about shadowed variables check-shadowing: true gocyclo: # minimal code complexity to report, 30 by default (but we recommend 10-20) min-complexity: 15 lll: # max line length, lines longer will be reported. Default is 120. # '\t' is counted as 1 character by default, and can be changed with the tab-width option line-length: 200 unused: # treat code as a program (not a library) and report unused exported identifiers check-exported: false unparam: # Inspect exported functions, default is false. Set to true if no external program/library imports your code. check-exported: true nakedret: # make an issue if func has more lines of code than this setting and it has naked returns max-func-lines: 60 prealloc: # Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. simple: true range-loops: true # Report preallocation suggestions on range loops for-loops: false # Report preallocation suggestions on for loops linters: disable-all: true enable: - govet - errcheck - gocyclo - structcheck - varcheck - ineffassign - deadcode - golint - interfacer - unconvert - goconst - gocyclo - staticcheck - unused - gosimple - dupl - gofmt - gosec - lll - megacheck - gocritic fast: false issues: # Independently from option `exclude` we use default exclude patterns, # it can be disabled by this option. To list all # excluded by default patterns execute `golangci-lint run --help`. # Default value for this option is true. exclude-use-default: true exclude: - "File is not `goimports`-ed" # TODO fix the following lint checks and remove them from exclude - "hugeParam" - "paramTypeCombine" - "importShadow" ```
Go environment ```console $ go version && go env go 1.14 GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/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" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/src/go.mod" 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-build039915949=/tmp/go-build -gno-record-gcc-switches" ```
Verbose output of running ```console $ golangci-lint cache clean $ golangci-lint run -v level=warning msg="[runner] Can't run linter goanalysis_metalinter: deadcode: analysis skipped: errors in package: [ /src/kafka/cfg.go:4:17: could not import github.com/confluentinc/confluent-kafka-go/kafka (/src/vendor/github.com/confluentinc/confluent-kafka-go/kafka/00version.go:48:8: could not import C (cgo preprocessing failed)) /src/kafka/consumer.go:17:2: \"github.com/davecgh/go-spew/spew\" imported but not used /src/kafka/consumer.go:8:2: \"strconv\" imported but not used /src/kafka/consumer_channel.go:10:2: \"github.com/prometheus/client_golang/prometheus\" imported but not used /src/kafka/consumer_channel.go:6:2: \"github.com/90poe/service-chassis/streaming/logs\" imported but not used ]" level=warning msg="[runner] Can't run linter unused: buildir: failed to load package kafka: could not load export data: no export data for \"github.com/confluentinc/confluent-kafka-go/kafka\"" level=error msg="Running error: buildir: failed to load package kafka: could not load export data: no export data for \"github.com/confluentinc/confluent-kafka-go/kafka\"" ```

I tried the suggestions for this issue https://github.com/golangci/golangci-lint/issues/1176 without success.

In docker the lint fails when I run on an alpine (golang:1.14.10-alpine3.12) base image, but succeeds with a Debian base image (golang:1.14.10-buster).

To build the repo under alpine we use go build -tags musl ./..., so I strongly suspect that the error is around must-dev, but I can't work out which file/setting is present in the Debian base image, absent in the Alpine base image.

The repo uses

github.com/confluentinc/confluent-kafka-go v1.4.2

The Dockerfile I use is

##################
### base stage ###
##################
FROM golang:1.14.10-alpine3.12 as base

ARG ALPINE_VERSION=v3.12
ARG LIBRESSL_VERSION=3.1
ARG LIBRDKAFKA_VERSION=1.4.2-r0

ARG GOLANGCI_VERSION=v1.30.0
ARG PROTOC_GEN_GO_VERSION=v1.4.2

# those bad guys are required to build C with Golang. Some of them may be redundant
RUN apk add --no-cache git \
    zstd \
    perl \
    openssl \
    curl \
    upx \
    openssh \
    bash \
    gcc \
    make \
    pkgconfig \
    gcc \
    g++ \
    musl-dev \
    yajl-dev \
    zlib-dev \
    cyrus-sasl-dev \
    lz4-dev \
    openssl-dev \
    libc-dev \
    bsd-compat-headers \
    build-base \
    coreutils \
    docker-compose \
    imagemagick \
    imagemagick-dev \
    ca-certificates \
    libressl${LIBRESSL_VERSION}-libcrypto libressl${LIBRESSL_VERSION}-libssl --update-cache --repository http://nl.alpinelinux.org/alpine/${ALPINE_VERSION}/main \
    librdkafka=${LIBRDKAFKA_VERSION} --update-cache --repository http://nl.alpinelinux.org/alpine/${ALPINE_VERSION}/community \
    librdkafka-dev=${LIBRDKAFKA_VERSION} --update-cache --repository http://nl.alpinelinux.org/alpine/${ALPINE_VERSION}/community

WORKDIR /src

COPY . ./

#######################
### lint-base stage ###
#######################
FROM golangci/golangci-lint:v1.31-alpine AS lint-base

##################
### lint stage ###
##################
FROM base AS lint
COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint
RUN golangci-lint run --allow-parallel-runners -c .golangci.yml

Makefile

go mod download
go mod vendor
docker build --force-rm --target=lint
boring-cyborg[bot] commented 4 years ago

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

fabriziomoscon commented 4 years ago

this is fixed by using --build-tags

golangci-lint run --build-tags=musl -c .golangci.yml ./...
jatinkapoor1313 commented 2 years ago

this is fixed by using --build-tags

golangci-lint run --build-tags=musl -c .golangci.yml ./...

this is not working - what this command is doing? I am not able to find .golangci.yml getting err - ERRO Can't read config: can't read viper config: open .golangci.yml: no such file or directory