Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: demonstrate golangci-lint not working with deps #145

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/.aspect/cli/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions example/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ sh_library(
go_binary(
name = "hello_go",
srcs = ["hello.go"],
deps = ["//src/gopher"],
)

cc_binary(
Expand Down
8 changes: 8 additions & 0 deletions example/src/gopher/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
5 changes: 5 additions & 0 deletions example/src/gopher/gopher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gopher

func Name() string {
return "Gopher!"
}
17 changes: 13 additions & 4 deletions example/src/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 20 additions & 6 deletions lint/golangci-lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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} $@"""
Expand All @@ -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],
Expand Down
Loading