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

Merged
merged 34 commits into from
Jan 28, 2025
Merged

Conversation

ldez
Copy link
Member

@ldez ldez commented Jan 20, 2025

This PR is a part of the v2: #5300


Important

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.paths-except
  • 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:
    presets:
      - 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
    paths-except:
      - '^base/a/.+'

Fixes #5297
Related to #5298
Fixes #4635
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
@ldez ldez force-pushed the feat/linter-exclusions branch 3 times, most recently from 8a37c87 to a2516ac Compare January 23, 2025 15:41
pkg/config/base_rule.go Outdated Show resolved Hide resolved
pkg/result/processors/exclusion_paths.go Outdated Show resolved Hide resolved
pkg/result/processors/exclusion_paths.go Outdated Show resolved Hide resolved
@ldez ldez force-pushed the feat/linter-exclusions branch 2 times, most recently from 3fa2ebb to 7748b59 Compare January 27, 2025 15:17
@ldez
Copy link
Member Author

ldez commented Jan 28, 2025

@bombsimon @alexandear Can I do something to ease the review?

@bombsimon
Copy link
Member

@bombsimon @alexandear Can I do something to ease the review?

Sorry for slow review! I don't think so, mostly just ben busy and given your very thorough research on this topic in the issue (thanks for that amazing work) I wanted to also catch up with all your findings to better understand these changes.

I'll try to get to this asap during the week!

pkg/config/run.go Show resolved Hide resolved
pkg/fsutils/basepath.go Show resolved Hide resolved
pkg/result/processors/path_relativity.go Outdated Show resolved Hide resolved
pkg/result/processors/path_relativity.go Outdated Show resolved Hide resolved
pkg/result/processors/path_relativity.go Outdated Show resolved Hide resolved
pkg/result/processors/exclusion_rules.go Outdated Show resolved Hide resolved
pkg/result/processors/exclusion_rules.go Outdated Show resolved Hide resolved
pkg/result/processors/exclusion_rules.go Outdated Show resolved Hide resolved
pkg/result/processors/exclusion_rules.go Show resolved Hide resolved
pkg/result/processors/exclusion_paths.go Show resolved Hide resolved
Copy link
Member

@bombsimon bombsimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, good stuff!

pkg/config/loader.go Show resolved Hide resolved
pkg/config/linters_exclusions.go Outdated Show resolved Hide resolved
pkg/config/loader.go Show resolved Hide resolved
pkg/config/linters_exclusions.go Show resolved Hide resolved
pkg/fsutils/basepath.go Show resolved Hide resolved
pkg/golinters/gocritic/testdata/gocritic.yml Show resolved Hide resolved
pkg/result/processors/exclusion_default.go Outdated Show resolved Hide resolved
pkg/result/processors/path_relativity.go Outdated Show resolved Hide resolved
pkg/result/processors/path_relativity.go Outdated Show resolved Hide resolved
@ldez ldez force-pushed the feat/linter-exclusions branch from f5a67ef to 3faf3af Compare January 28, 2025 22:28
@ldez
Copy link
Member Author

ldez commented Jan 28, 2025

I'm so excited to merge this PR, I know this is mainly internals but I spend a lot of time on it, and the next PR (I will open it 1 minute after the merge of this one) will add the fmt command 🎉

@ldez
Copy link
Member Author

ldez commented Jan 28, 2025

I will press the merge button 🎉
I'm so excited

@ldez ldez merged commit 98c110b into golangci:master Jan 28, 2025
18 checks passed
@ldez ldez deleted the feat/linter-exclusions branch January 28, 2025 22:49
@ldez ldez added this to the next milestone Feb 1, 2025
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
4 participants