From 00edd3cc11fef8703609d3157a6513984e06f511 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 8 Apr 2024 16:14:28 -0700 Subject: [PATCH] hack: add gopls analyzers Signed-off-by: Tonis Tiigi --- docker-bake.hcl | 3 +- hack/dockerfiles/lint.Dockerfile | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docker-bake.hcl b/docker-bake.hcl index 265595004e883..48803c7379c10 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -131,7 +131,7 @@ target "lint" { TARGETNAME = buildtags.name BUILDTAGS = buildtags.tags } - platforms = buildtags.target == "golangci-lint" && GOLANGCI_LINT_MULTIPLATFORM != null ? [ + platforms = ( buildtags.target == "golangci-lint" || buildtags.target == "gopls" ) && GOLANGCI_LINT_MULTIPLATFORM != null ? [ "freebsd/amd64", "linux/amd64", "linux/arm64", @@ -148,6 +148,7 @@ target "lint" { { name = "nydus", tags = "nydus", target = "golangci-lint" }, { name = "yaml", tags = "", target = "yamllint" }, { name = "proto", tags = "", target = "protolint" }, + { name = "gopls", tags = "", target = "gopls-analyze" } ] } } diff --git a/hack/dockerfiles/lint.Dockerfile b/hack/dockerfiles/lint.Dockerfile index b5aa717c3a91b..ff5d8fedc66d7 100644 --- a/hack/dockerfiles/lint.Dockerfile +++ b/hack/dockerfiles/lint.Dockerfile @@ -5,6 +5,9 @@ ARG ALPINE_VERSION=3.19 ARG XX_VERSION=1.4.0 ARG PROTOLINT_VERSION=0.45.0 ARG GOLANGCI_LINT_VERSION=1.57.1 +ARG GOPLS_VERSION=v0.20.0 +# disabled: deprecated unusedvariable simplifyrange +ARG GOPLS_ANALYZERS="embeddirective fillreturns infertypeargs nonewvars noresultvalues simplifycompositelit simplifyslice stubmethods undeclaredname unusedparams useany" FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS golang-base FROM --platform=$BUILDPLATFORM yoheimuta/protolint:${PROTOLINT_VERSION} AS protolint-base @@ -39,6 +42,51 @@ RUN --mount=target=/go/src/github.com/moby/buildkit \ protolint lint . && \ touch /protolint.done +FROM golang-base AS gopls +RUN apk add --no-cache git +ARG GOPLS_VERSION +WORKDIR /src +RUN git clone https://github.com/golang/tools.git && \ + cd tools && git checkout ${GOPLS_VERSION} +WORKDIR tools/gopls +ARG GOPLS_ANALYZERS +RUN <<'EOF' + set -ex + mkdir -p /out + for analyzer in ${GOPLS_ANALYZERS}; do + mkdir -p internal/cmd/$analyzer + cat < internal/cmd/$analyzer/main.go +package main + +import ( + "golang.org/x/tools/go/analysis/singlechecker" + analyzer "golang.org/x/tools/gopls/internal/analysis/$analyzer" +) + +func main() { singlechecker.Main(analyzer.Analyzer) } +eot + echo "Analyzing with ${analyzer}..." + go build -o /out/$analyzer ./internal/cmd/$analyzer + done +EOF + +FROM golang-base AS gopls-analyze +COPY --link --from=xx / / +ARG GOPLS_ANALYZERS +ARG TARGETNAME +ARG TARGETPLATFORM +WORKDIR /go/src/github.com/moby/buildkit +RUN --mount=target=. \ + --mount=target=/root/.cache,type=cache,id=lint-cache-${TARGETNAME}-${TARGETPLATFORM} \ + --mount=target=/gopls-analyzers,from=gopls,source=/out <