ashanbrown / forbidigo

Go linter for forbidding identifiers
Other
119 stars 10 forks source link

Include/Exclude Files by Pattern #42

Open mvrahden opened 10 months ago

mvrahden commented 10 months ago

Simple Example

We want to prevent devs from using os.Setenv in Tests and use t.Setenv instead. However, the usage of os.Setenv in source this could be a viable solution. Therefore we'd like have a filename include matcher, to enable this forbidigo rule only on certain filepatterns, e.g. only on suffix _test\.go$.

Proposal

Add the option of a filename exclude/include pattern to the config, which helps to dismiss certain files from the rule-enforcement.

ashanbrown commented 10 months ago

@mvrahden Thanks for sharing that use case. Just to check, are you using this linter standalone or in the context of golangci-lint? Used standalone, the linter doesn't have too much control over exclusions. golangci-lint definitely has more flexible ways of excluding rules.

mvrahden commented 10 months ago

Hi @ashanbrown, we use it in the context of golangci-lint

atom-andrew commented 10 months ago

Does golangci-lint not allow you to control where your rules apply? Recently there were some updates to golangci-lint, so you could also both include and exclude a set of files.

mvrahden commented 10 months ago

Interesting, I wasn't aware of that change. I'll look into it

jachym-tousek-keboola commented 7 months ago

@atom-andrew I don't think that helps with forbidgo because using that approach you'd disable forbidgo completely for some files. However this feature is needed on a per-pattern bases. We do need some forbidgo patterns to be applied globally while others should be only applied only to specific files.

ashanbrown commented 6 months ago

Is excluding rules by text insufficient to handle these cases? The notes there say "Also, you can use issues.exclude-rules config option for per-path or per-linter configuration." Possibly you might need to annotate your rules (i.e. include some special string in them), so that you can easily find it to exclude.

Just to step back again, I'd definitely prefer to avoid building complex configuration logic forbidigo that can be handled by golang-ci (and shared with other linters). I suppose what is fairly different between forbidigo and other linters is that the rules are all user-defined. I'm not working actively in golang myself these days, but I'd be curious if there are other linters that have user-defined rules and how they handle these exclusions.

mvrahden commented 6 months ago

Hi @ashanbrown, I can confirm that the exclude by path and by text option would work. Thanks for pointing it out