From 758b8a185178c07ed1c626b2c193bedfe3538129 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 11 Apr 2024 10:11:50 -0700 Subject: [PATCH] feat: remove golangci-lint (#207) In preparation for 1.0, we want all linters to work correctly. In the discussion for #129 we decided that is a fatal bug, and no one is able to fix it in the short term. We're happy to add Go linting back to rules_lint when someone has a correct implementation that handles transitive srcs. The code will still be here in the git history when it's time to revive it. Closes #129 --- MODULE.bazel | 3 - README.md | 3 +- docs/BUILD.bazel | 5 -- docs/golangci-lint.md | 63 ------------------- example/.aspect/cli/config.yaml | 1 - example/.golangci.yaml | 21 ------- example/BUILD.bazel | 1 - example/lint.sh | 2 +- example/test/lint_test.bats | 3 - example/tools/lint/linters.bzl | 8 --- lint/BUILD.bazel | 10 --- lint/golangci-lint.bzl | 104 -------------------------------- lint/multitool.lock.json | 36 ----------- 13 files changed, 2 insertions(+), 258 deletions(-) delete mode 100644 docs/golangci-lint.md delete mode 100644 example/.golangci.yaml delete mode 100644 lint/golangci-lint.bzl diff --git a/MODULE.bazel b/MODULE.bazel index ad72913d..ddc73221 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -24,9 +24,6 @@ bazel_dep(name = "rules_proto", version = "6.0.0-rc2") bazel_dep(name = "rules_buf", version = "0.1.1") bazel_dep(name = "toolchains_protoc", version = "0.2.1") -# Needed to get the 'go_context' symbol, to invoke golangci-lint -bazel_dep(name = "rules_go", version = "0.39.1", repo_name = "io_bazel_rules_go") - multitool = use_extension("@rules_multitool//multitool:extension.bzl", "multitool") multitool.hub(lockfile = "//format:multitool.lock.json") multitool.hub(lockfile = "//lint:multitool.lock.json") diff --git a/README.md b/README.md index 5cb0cfae..e9662af7 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ New tools are being added frequently, so check this page again! | ---------------------- | --------------------- | ---------------- | | C / C++ | [clang-format] | ([#112]) | | CSS | [Prettier] | | -| Go | [gofmt] or [gofumpt] | [golangci-lint] | +| Go | [gofmt] or [gofumpt] | | | HCL (Hashicorp Config) | [terraform] fmt | | | HTML | [Prettier] | | | JSON | [Prettier] | | @@ -63,7 +63,6 @@ New tools are being added frequently, so check this page again! [ruff]: https://docs.astral.sh/ruff/ [shellcheck]: https://www.shellcheck.net/ [shfmt]: https://github.com/mvdan/sh -[golangci-lint]: https://github.com/golangci/golangci-lint [clang-format]: https://clang.llvm.org/docs/ClangFormat.html [#112]: https://github.com/aspect-build/rules_lint/issues/112 [vale]: https://vale.sh/ diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index efbd7450..18382c3c 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -37,11 +37,6 @@ stardoc_with_diff_test( bzl_library_target = "//lint:ruff", ) -stardoc_with_diff_test( - name = "golangci-lint", - bzl_library_target = "//lint:golangci-lint", -) - stardoc_with_diff_test( name = "shellcheck", bzl_library_target = "//lint:shellcheck", diff --git a/docs/golangci-lint.md b/docs/golangci-lint.md deleted file mode 100644 index e1dbd729..00000000 --- a/docs/golangci-lint.md +++ /dev/null @@ -1,63 +0,0 @@ - - -API for declaring a golangci-lint lint aspect that visits go_library, go_test, and go_binary rules. - -``` -load("@aspect_rules_lint//lint:golangci-lint.bzl", "golangci_lint_aspect") - -golangci_lint = golangci_lint_aspect( - binary = "@multitool//tools/golangci-lint", - config = "@@//:.golangci.yaml", -) -``` - - - - -## golangci_lint_action - -
-golangci_lint_action(ctx, executable, srcs, config, report, use_exit_code)
-
- -Run golangci-lint as an action under Bazel. - -Based on https://github.com/golangci/golangci-lint - - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| ctx | Bazel Rule or Aspect evaluation context | none | -| executable | label of the the golangci-lint program | none | -| srcs | golang files to be linted | none | -| config | label of the .golangci.yaml file | none | -| report | output file to generate | none | -| use_exit_code | whether to fail the build when a lint violation is reported | False | - - - - -## lint_golangci_aspect - -
-lint_golangci_aspect(binary, config)
-
- -A factory function to create a linter aspect. - -Attrs: - binary: a golangci-lint executable. - config: the .golangci.yaml file - -**PARAMETERS** - - -| Name | Description | Default Value | -| :------------- | :------------- | :------------- | -| binary |

-

| none | -| config |

-

| none | - - diff --git a/example/.aspect/cli/config.yaml b/example/.aspect/cli/config.yaml index 9f913206..50a0fdcf 100644 --- a/example/.aspect/cli/config.yaml +++ b/example/.aspect/cli/config.yaml @@ -3,7 +3,6 @@ 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/.golangci.yaml b/example/.golangci.yaml deleted file mode 100644 index 8ee6d83b..00000000 --- a/example/.golangci.yaml +++ /dev/null @@ -1,21 +0,0 @@ -# Refer to golangci-lint's example config file for more options and information: -# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml - -run: - timeout: 5m - # Prevent non-hermetic behavior. Per the documentation: - # > If invoked with -mod=readonly, the go command is disallowed from the implicit - # > automatic updating of go.mod described above. Instead, it fails when any changes - # > to go.mod are needed. This setting is most useful to check that go.mod does - # > not need updates, such as in a continuous integration and testing system. - modules-download-mode: readonly -linters: - enable: - # Default - - errcheck - - gosimple - - govet - - ineffassign - - staticcheck - - typecheck - - unused diff --git a/example/BUILD.bazel b/example/BUILD.bazel index 988666d8..1f4e66e4 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -19,7 +19,6 @@ exports_files( ".ruff.toml", ".shellcheckrc", ".scalafmt.conf", - ".golangci.yaml", ".vale.ini", ], visibility = ["//visibility:public"], diff --git a/example/lint.sh b/example/lint.sh index 137ca952..a339b272 100755 --- a/example/lint.sh +++ b/example/lint.sh @@ -24,7 +24,7 @@ filter='.namedSetOfFiles | values | .files[] | ((.pathPrefix | join("/")) + "/" # NB: perhaps --remote_download_toplevel is needed as well with remote execution? args=( - "--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,pmd,ruff,shellcheck,golangci_lint,vale} | tr ' ' ',')" + "--aspects=$(echo //tools/lint:linters.bzl%{buf,eslint,flake8,pmd,ruff,shellcheck,vale} | tr ' ' ',')" "--build_event_json_file=$buildevents" ) report_args=( diff --git a/example/test/lint_test.bats b/example/test/lint_test.bats index 81dbabd3..924944d0 100644 --- a/example/test/lint_test.bats +++ b/example/test/lint_test.bats @@ -28,9 +28,6 @@ EOF # Buf assert_output --partial 'src/file.proto:1:1:Import "src/unused.proto" is unused.' - # Golangci-lint - assert_output --partial 'src/hello.go:13:2: SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)' - # Vale assert_output --partial "src/README.md:3:47:Google.We:Try to avoid using first-person plural like 'We'." } diff --git a/example/tools/lint/linters.bzl b/example/tools/lint/linters.bzl index b095f4a5..7debca02 100644 --- a/example/tools/lint/linters.bzl +++ b/example/tools/lint/linters.bzl @@ -3,7 +3,6 @@ load("@aspect_rules_lint//lint:buf.bzl", "lint_buf_aspect") load("@aspect_rules_lint//lint:eslint.bzl", "lint_eslint_aspect") load("@aspect_rules_lint//lint:flake8.bzl", "lint_flake8_aspect") -load("@aspect_rules_lint//lint:golangci-lint.bzl", "lint_golangci_aspect") load("@aspect_rules_lint//lint:lint_test.bzl", "lint_test") load("@aspect_rules_lint//lint:pmd.bzl", "lint_pmd_aspect") load("@aspect_rules_lint//lint:ruff.bzl", "lint_ruff_aspect") @@ -57,13 +56,6 @@ shellcheck = lint_shellcheck_aspect( shellcheck_test = lint_test(aspect = shellcheck) -golangci_lint = lint_golangci_aspect( - binary = "@multitool//tools/golangci-lint", - config = "@@//:.golangci.yaml", -) - -golangci_lint_test = lint_test(aspect = golangci_lint) - vale = lint_vale_aspect( binary = "@@//tools/lint:vale", config = "@@//:.vale.ini", diff --git a/lint/BUILD.bazel b/lint/BUILD.bazel index 7e07b643..171f0103 100644 --- a/lint/BUILD.bazel +++ b/lint/BUILD.bazel @@ -118,16 +118,6 @@ bzl_library( ], ) -bzl_library( - name = "golangci-lint", - srcs = ["golangci-lint.bzl"], - visibility = ["//visibility:public"], - deps = [ - "//lint/private:lint_aspect", - "@io_bazel_rules_go//go:def", - ], -) - bzl_library( name = "shellcheck", srcs = ["shellcheck.bzl"], diff --git a/lint/golangci-lint.bzl b/lint/golangci-lint.bzl deleted file mode 100644 index a92f5564..00000000 --- a/lint/golangci-lint.bzl +++ /dev/null @@ -1,104 +0,0 @@ -"""API for declaring a golangci-lint lint aspect that visits go_library, go_test, and go_binary rules. - -``` -load("@aspect_rules_lint//lint:golangci-lint.bzl", "golangci_lint_aspect") - -golangci_lint = golangci_lint_aspect( - binary = "@multitool//tools/golangci-lint", - config = "@@//:.golangci.yaml", -) -``` -""" - -load("@io_bazel_rules_go//go:def.bzl", "go_context") -load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "filter_srcs", "report_file") - -_MNEMONIC = "golangcilint" - -def golangci_lint_action(ctx, executable, srcs, config, report, use_exit_code = False): - """Run golangci-lint as an action under Bazel. - - Based on https://github.com/golangci/golangci-lint - - Args: - ctx: Bazel Rule or Aspect evaluation context - executable: label of the the golangci-lint program - srcs: golang files to be linted - config: label of the .golangci.yaml file - report: output file to generate - use_exit_code: whether to fail the build when a lint violation is reported - """ - - # golangci-lint calls out to Go, so we need the go context - go = go_context(ctx) - inputs = srcs + [config] + go.sdk_files - args = ctx.actions.args() - args.add_all(srcs) - - command = """#!/usr/bin/env bash - export GOROOT=$(cd "$(dirname {go_tool})/.."; pwd) - export GOPATH=$GOROOT - export GOCACHE="$(mktemp -d)" - export PATH="$GOPATH/bin:$PATH" - GOLANGCI_LINT_CACHE=$(pwd)/.cache {golangci_lint} run --config={config} $@""" - - if use_exit_code: - command += " && touch {report}" - else: - command += " >{report} || true" - - ctx.actions.run_shell( - inputs = inputs, - outputs = [report], - command = command.format( - 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, - arguments = [args], - mnemonic = _MNEMONIC, - tools = [executable], - ) - -# buildifier: disable=function-docstring -def _golangci_lint_aspect_impl(target, ctx): - if ctx.rule.kind not in ["go_binary", "go_library", "go_test"]: - return [] - - report, info = report_file(_MNEMONIC, target, ctx) - - srcs = filter_srcs(ctx.rule) - if not srcs: - return [] - - golangci_lint_action(ctx, ctx.executable._golangci_lint, srcs, ctx.file._config_file, report, ctx.attr._options[LintOptionsInfo].fail_on_violation) - return [info] - -def lint_golangci_aspect(binary, config): - """A factory function to create a linter aspect. - - Attrs: - binary: a golangci-lint executable. - config: the .golangci.yaml file - """ - return aspect( - implementation = _golangci_lint_aspect_impl, - attrs = { - "_options": attr.label( - default = "//lint:fail_on_violation", - providers = [LintOptionsInfo], - ), - "_golangci_lint": attr.label( - default = binary, - executable = True, - cfg = "exec", - ), - "_config_file": attr.label( - default = config, - allow_single_file = True, - ), - }, - toolchains = ["@io_bazel_rules_go//go:toolchain"], - ) diff --git a/lint/multitool.lock.json b/lint/multitool.lock.json index 0939b5d7..24552728 100644 --- a/lint/multitool.lock.json +++ b/lint/multitool.lock.json @@ -1,41 +1,5 @@ { "$schema": "https://raw.githubusercontent.com/theoremlp/rules_multitool/main/lockfile.schema.json", - "golangci-lint": { - "binaries": [ - { - "os": "linux", - "cpu": "x86_64", - "kind": "archive", - "url": "https://github.com/golangci/golangci-lint/releases/download/v1.55.2/golangci-lint-1.55.2-linux-amd64.tar.gz", - "sha256": "ca21c961a33be3bc15e4292dc40c98c8dcc5463a7b6768a3afc123761630c09c", - "file": "golangci-lint-1.55.2-linux-amd64/golangci-lint" - }, - { - "os": "linux", - "cpu": "arm64", - "kind": "archive", - "url": "https://github.com/golangci/golangci-lint/releases/download/v1.55.2/golangci-lint-1.55.2-linux-arm64.tar.gz", - "sha256": "8eb0cee9b1dbf0eaa49871798c7f8a5b35f2960c52d776a5f31eb7d886b92746", - "file": "golangci-lint-1.55.2-linux-arm64/golangci-lint" - }, - { - "os": "macos", - "cpu": "x86_64", - "kind": "archive", - "url": "https://github.com/golangci/golangci-lint/releases/download/v1.55.2/golangci-lint-1.55.2-darwin-amd64.tar.gz", - "sha256": "632e96e6d5294fbbe7b2c410a49c8fa01c60712a0af85a567de85bcc1623ea21", - "file": "golangci-lint-1.55.2-darwin-amd64/golangci-lint" - }, - { - "os": "macos", - "cpu": "arm64", - "kind": "archive", - "url": "https://github.com/golangci/golangci-lint/releases/download/v1.55.2/golangci-lint-1.55.2-darwin-arm64.tar.gz", - "sha256": "234463f059249f82045824afdcdd5db5682d0593052f58f6a3039a0a1c3899f6", - "file": "golangci-lint-1.55.2-darwin-arm64/golangci-lint" - } - ] - }, "shellcheck": { "binaries": [ {