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

feat: new linter exclusions system #5339

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

ldez
Copy link
Member

@ldez ldez commented Jan 20, 2025

This PR is a part of the v2: #5300


This PR deliberately omits documentation about the new options and section (but the JSONSchema is updated).
This is because I don't want to deprecate the previous section for now: I think it is better to handle that when all the v2 proposals are managed to avoid multiple migrations.

The main goal is to replace issue.exclude-xxx options with linters.exclusions section.


There is a new option linters.exclusions.warn-unused that emits a warning if a linters.exclusions.rules or linters.exclusions.paths is unused.

There is a new option run.relative-path-mode with 4 modes:

  • gomod: paths are relative to the go.mod
  • gitroot: paths are relative to the git root.
  • cfg: paths are relative to the configuration position.
  • wd (v1 default): paths are relative to the location where golangci-lint is run.

This option controls all the path-related options:

  • issues.exclude-rules[].path
  • issues.exclude-rules[].path-except
  • issues.exclude-dirs
  • issues.exclude-files
  • severity.rules[].path
  • severity.rules[].path-except
  • custom[].path
  • linters.exclusions.paths
  • linters.exclusions.rules[].path
  • linters.exclusions.rules[].path-except
  • linters

The linter configuration related to paths should use the placeholder ${base-path}:

  • gocritic (gocritic.ruleguard.rules)
  • depguard (rules[].files)
  • goheader (goheader.template-path)

errcheck: the option errcheck.exclude has been deprecated since golangci-lint v1.42 so I didn't add the support of the placeholder.

With the new option run.relative-path-mode, you can now run golangci-lint inside a package and the configuration related to file paths will work.

In v2, output.path-prefix will be used only for output and not for matching, and the default relative path mode will be gomod or cfg (not wd like today).


More details about the topic are inside the issues:


configuration overview
linters:
  # ...
  
  exclusions:
    default:
      - comments
      - stdErrorHandling
      - commonFalsePositives
      - legacy
    generated: strict
    warn-unused: true
    rules:
      - path: _test\.go
        linters:
          - gocyclo
          - errcheck
          - dupl
          - gosec
      - path-except: _test\.go
        linters:
          - forbidigo
      - path: internal/hmac/
        text: "weak cryptographic primitive"
        linters:
          - gosec
      - linters:
          - staticcheck
        text: "SA9003:"
      - linters:
          - lll
        source: "^//go:generate "
    paths:
      - src/external_libs
      - autogenerated_by_my_lib
      - ".*\\.my\\.go$"
      - lib/bad.go

Fixes #5297
Related to #5298
Fixes #3953
Fixes #3717
Fixes #1178
Closes #3748

@ldez ldez added area: config Related to .golangci.yml and/or cli options area: exclusions labels Jan 20, 2025
@ldez ldez requested a review from bombsimon January 20, 2025 16:36
@ldez ldez force-pushed the feat/linter-exclusions branch from aed5c27 to a96fcf4 Compare January 20, 2025 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: config Related to .golangci.yml and/or cli options area: exclusions
Projects
None yet
2 participants