From 2251e1ec46ddfd41d9def7649c705c699cf2658a Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 23 Nov 2026 11:23:33 -0600 Subject: [PATCH] feat(lint-baseline): fail on new dead code in the action --- README.md | 10 ++++++++++ lint-baseline/action.yml | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/README.md b/README.md index e358806..b360112 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,16 @@ nested module, run the action with `working-directory` set to that module. It accepts unimported `testdata` files. The Linux lint job does not lint files that only load on Windows or macOS. +The full-tree dead-code check runs last. It fails on any `unused` or +`unparam` finding, and the directive check rejects new suppressions for +both linters. Set `dead-code: "false"` to turn off both checks. To check +for dead code on pushes to `main`, run: + +```sh +golangci-lint run --config .golangci.yaml --enable-only unused,unparam \ + --allow-parallel-runners --issues-exit-code=1 --path-prefix=./ ./... +``` + Set `working-directory` for a Go module outside the repo root. Set `config` and `baseline` to paths relative to that directory to use different names. Use `golangci-lint-version` to override `v2.15.0` or `guarded-linters` to diff --git a/lint-baseline/action.yml b/lint-baseline/action.yml index 58009e5..0a16c40 100644 --- a/lint-baseline/action.yml +++ b/lint-baseline/action.yml @@ -28,6 +28,9 @@ inputs: allow-new-modules: description: Allow new go.mod and go.work files. default: "false" + dead-code: + description: Check the whole Go module for unused code and parameters. + default: "true" runs: using: composite steps: @@ -135,6 +138,7 @@ runs: ALLOW_GENERATED: ${{ inputs.allow-generated }} ALLOW_NEW_MODULES: ${{ inputs.allow-new-modules }} MODULE_DIR: ${{ inputs.working-directory }} + DEAD_CODE: ${{ inputs.dead-code }} run: | tags=$("$RUNNER_TEMP/lint-baseline" config-check --config "$CONFIG" --linters "$BASELINE_LINTERS") bash "$GITHUB_ACTION_PATH/with-lint-env.sh" bash -ec ' @@ -145,6 +149,7 @@ runs: GOOS=$os CGO_ENABLED=1 go list -e -json="$fields" -deps -test "${args[@]}" ./... done > "$RUNNER_TEMP/golist.json" ' _ "$tags" + [[ $DEAD_CODE != true ]] || GUARDED_LINTERS+=,unused,unparam GIT_ATTR_SOURCE=$PR_MERGE_BASE git -c core.quotePath=true -C "$GITHUB_WORKSPACE" diff \ --no-ext-diff --no-textconv --no-color --no-relative --no-renames --text \ --src-prefix=a/ --dst-prefix=b/ --unified=0 --inter-hunk-context=0 \ @@ -190,3 +195,14 @@ runs: bash "$GITHUB_ACTION_PATH/lint-json.sh" "$CONFIG" "$RUNNER_TEMP/lint.json" "$RUNNER_TEMP/lint-baseline" check --lint-json "$RUNNER_TEMP/lint.json" \ --baseline "$BASELINE" "${base_args[@]}" --root . + - name: Check full-tree dead code + if: inputs.dead-code == 'true' + shell: bash + working-directory: ${{ inputs.working-directory }} + env: + CONFIG: ${{ inputs.config }} + run: | + bash "$GITHUB_ACTION_PATH/with-lint-env.sh" bash -o pipefail -c ' + golangci-lint run --config "$CONFIG" --enable-only unused,unparam --allow-parallel-runners \ + --issues-exit-code=1 --path-prefix=./ ./... 2>&1 | tr "\r" " " | sed "s/^/lint:dead: /" + '