From 6c633efba73d6a45be4dd7cd73e40b05a681bed9 Mon Sep 17 00:00:00 2001 From: "Sean R. Abraham" Date: Fri, 9 Feb 2024 13:05:23 -0700 Subject: [PATCH 1/2] chore: demonstrate golangci-lint not working with deps I get an error like below when I run bazel lint src:hello_go as a result of the go_binary depending on another go target this is a demo of https://github.com/aspect-build/rules_lint/issues/129 INFO: Analyzed target //src:hello_go (0 packages loaded, 0 targets configured). INFO: From golangcilint src/golangcilint.hello_go.aspect_rules_lint.report: level=warning msg="[runner] Can't run linter goanalysis_metalinter: buildir: failed to load package : could not load export data: no export data for \"gopher\"" level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: buildir: failed to load package : could not load export data: no export data for \"gopher\"\n\n" --- example/.aspect/cli/config.yaml | 1 + example/src/BUILD.bazel | 1 + example/src/gopher/BUILD.bazel | 8 ++++++++ example/src/gopher/gopher.go | 5 +++++ example/src/hello.go | 17 +++++++++++++---- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 example/src/gopher/BUILD.bazel create mode 100644 example/src/gopher/gopher.go diff --git a/example/.aspect/cli/config.yaml b/example/.aspect/cli/config.yaml index 50a0fdcf..9f913206 100644 --- a/example/.aspect/cli/config.yaml +++ b/example/.aspect/cli/config.yaml @@ -3,6 +3,7 @@ lint: - //tools/lint:linters.bzl%eslint - //tools/lint:linters.bzl%buf - //tools/lint:linters.bzl%flake8 + - //tools/lint:linters.bzl%golangci_lint - //tools/lint:linters.bzl%pmd - //tools/lint:linters.bzl%ruff - //tools/lint:linters.bzl%vale diff --git a/example/src/BUILD.bazel b/example/src/BUILD.bazel index 01d65db0..394e774e 100644 --- a/example/src/BUILD.bazel +++ b/example/src/BUILD.bazel @@ -46,6 +46,7 @@ sh_library( go_binary( name = "hello_go", srcs = ["hello.go"], + deps = ["//src/gopher"], ) cc_binary( diff --git a/example/src/gopher/BUILD.bazel b/example/src/gopher/BUILD.bazel new file mode 100644 index 00000000..a9ba4e0e --- /dev/null +++ b/example/src/gopher/BUILD.bazel @@ -0,0 +1,8 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "gopher", + srcs = ["gopher.go"], + importpath = "gopher", + visibility = ["//visibility:public"], +) diff --git a/example/src/gopher/gopher.go b/example/src/gopher/gopher.go new file mode 100644 index 00000000..dedce7cd --- /dev/null +++ b/example/src/gopher/gopher.go @@ -0,0 +1,5 @@ +package gopher + +func Name() string { + return "Gopher!" +} diff --git a/example/src/hello.go b/example/src/hello.go index f64c44f7..da816a85 100644 --- a/example/src/hello.go +++ b/example/src/hello.go @@ -2,13 +2,22 @@ package main import ( "fmt" + "gopher" + "log" ) -const ( - w = "world" -) +// staticcheck won't like this +var notUsed string func main() { - hello := fmt.Sprintf("Hello %s\n", w) + s := []string{"a"} + // staticcheck also won't like this + if s != nil { + for _, v := range s { + log.Println(v) + } + } + hello := fmt.Sprintf("Hello %s\n", gopher.Name()) fmt.Printf(hello) + _ = s } From d8992a930f97853115687dfdeba487524f9b94d2 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Sun, 7 Apr 2024 11:04:13 -0700 Subject: [PATCH 2/2] chore: read providers from go_library dependencies However this isn't sufficient yet for the Go tool to locate these files --- lint/golangci-lint.bzl | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lint/golangci-lint.bzl b/lint/golangci-lint.bzl index 44fe863d..b6b1d7b5 100644 --- a/lint/golangci-lint.bzl +++ b/lint/golangci-lint.bzl @@ -11,7 +11,7 @@ golangci_lint = golangci_lint_aspect( """ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@io_bazel_rules_go//go:def.bzl", "go_context") +load("@io_bazel_rules_go//go:def.bzl", "GoLibrary", "GoSource", "go_context") load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "filter_srcs", "report_file") _MNEMONIC = "golangcilint" @@ -31,14 +31,26 @@ def golangci_lint_action(ctx, executable, srcs, config, report, use_exit_code = """ # golangci-lint calls out to Go, so we need the go context - go = go_context(ctx) - inputs = srcs + [config] + go.sdk_files + go_ctx = go_context(ctx) + inputs = srcs + [config] + go_ctx.sdk_files + importmaps = [] + for dep in ctx.rule.attr.deps: + inputs.extend(dep[GoSource].srcs) + importmaps.append(dep[GoLibrary].importmap) + + # For the Go tool to locate sources from other packages, it expects their imports to be on the GOPATH. + # It seems like rules_go does something different from what the ecosystem expects, that libraries are installed + # into the GOROOT folder. + # https://github.com/bazelbuild/rules_go/blob/c0ef535977f9fd2d9a67243552cd04da285ab629/extras/gomock.bzl#L37-L55 + # suggests that we have to copy files around? + gopath = go_ctx.sdk.root_file.dirname + args = ctx.actions.args() args.add_all(srcs) command = """#!/usr/bin/env bash - export GOROOT=$(cd "$(dirname {go_tool})/.."; pwd) - export GOPATH=$GOROOT + export GOROOT=$(pwd)/{goroot} + export GOPATH=$(pwd)/{gopath} export GOCACHE="$(mktemp -d)" export PATH="$GOPATH/bin:$PATH" GOLANGCI_LINT_CACHE=$(pwd)/.cache {golangci_lint} run --config={config} $@""" @@ -52,12 +64,14 @@ def golangci_lint_action(ctx, executable, srcs, config, report, use_exit_code = inputs = inputs, outputs = [report], command = command.format( + goroot = go_ctx.sdk.root_file.dirname, + gopath = gopath, golangci_lint = executable.path, report = report.path, config = config.path, go_tool = ctx.toolchains["@io_bazel_rules_go//go:toolchain"].sdk.go.path, ), - env = go.env, + env = go_ctx.env, arguments = [args], mnemonic = _MNEMONIC, tools = [executable],