Skip to content

Commit 3785a3b

Browse files
authored
dev: option to not check deprecation (#4591)
1 parent c00c1a5 commit 3785a3b

File tree

7 files changed

+46
-61
lines changed

7 files changed

+46
-61
lines changed

.golangci.yml

-9
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,11 @@ issues:
161161
- path: pkg/commands/run.go
162162
linters: [staticcheck]
163163
text: "SA1019: c.cfg.Run.ShowStats is deprecated: use Output.ShowStats instead."
164-
- path: pkg/commands/config.go
165-
linters: [staticcheck]
166-
text: "SA1019: cfg.Run.UseDefaultSkipDirs is deprecated: use Issues.UseDefaultExcludeDirs instead."
167-
- path: pkg/commands/linters.go
168-
linters: [staticcheck]
169-
text: "SA1019: c.cfg.Run.UseDefaultSkipDirs is deprecated: use Issues.UseDefaultExcludeDirs instead."
170164

171165
# Deprecated linter options.
172166
- path: pkg/golinters/errcheck.go
173167
linters: [staticcheck]
174168
text: "SA1019: errCfg.Exclude is deprecated: use ExcludeFunctions instead"
175-
- path: pkg/commands/run.go
176-
linters: [staticcheck]
177-
text: "SA1019: lsc.Errcheck.Exclude is deprecated: use ExcludeFunctions instead"
178169
- path: pkg/golinters/govet.go
179170
linters: [staticcheck]
180171
text: "SA1019: cfg.CheckShadowing is deprecated: the linter should be enabled inside Enable."

pkg/commands/config.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,10 @@ func (c *configCommand) preRunE(cmd *cobra.Command, args []string) error {
8484
// It only needs to know the path of the configuration file.
8585
cfg := config.NewDefault()
8686

87-
// Hack to hide deprecation messages related to `--skip-dirs-use-default`:
88-
// Flags are not bound then the default values, defined only through flags, are not applied.
89-
// In this command, file path and file information are the only requirements, i.e. it don't need flag values.
90-
//
91-
// TODO(ldez) add an option (check deprecation) to `Loader.Load()` but this require a dedicated PR.
92-
cfg.Run.UseDefaultSkipDirs = true
93-
9487
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts, cfg, args)
9588

96-
if err := loader.Load(); err != nil {
89+
err := loader.Load(config.LoadOptions{})
90+
if err != nil {
9791
return fmt.Errorf("can't load config: %w", err)
9892
}
9993

pkg/commands/linters.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,10 @@ func newLintersCommand(logger logutils.Log) *lintersCommand {
5959
}
6060

6161
func (c *lintersCommand) preRunE(cmd *cobra.Command, args []string) error {
62-
// Hack to hide deprecation messages related to `--skip-dirs-use-default`:
63-
// Flags are not bound then the default values, defined only through flags, are not applied.
64-
// In this command, linters information are the only requirements, i.e. it don't need flag values.
65-
//
66-
// TODO(ldez) add an option (check deprecation) to `Loader.Load()` but this require a dedicated PR.
67-
c.cfg.Run.UseDefaultSkipDirs = true
68-
6962
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg, args)
7063

71-
if err := loader.Load(); err != nil {
64+
err := loader.Load(config.LoadOptions{Validation: true})
65+
if err != nil {
7266
return fmt.Errorf("can't load config: %w", err)
7367
}
7468

pkg/commands/run.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ func (c *runCommand) persistentPreRunE(cmd *cobra.Command, args []string) error
154154

155155
loader := config.NewLoader(c.log.Child(logutils.DebugKeyConfigReader), c.viper, cmd.Flags(), c.opts.LoaderOptions, c.cfg, args)
156156

157-
if err := loader.Load(); err != nil {
157+
err := loader.Load(config.LoadOptions{CheckDeprecation: true, Validation: true})
158+
if err != nil {
158159
return fmt.Errorf("can't load config: %w", err)
159160
}
160161

pkg/config/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func (c *Config) GetConfigDir() string {
3333

3434
func (c *Config) Validate() error {
3535
validators := []func() error{
36-
c.Issues.Validate,
37-
c.Severity.Validate,
36+
c.Run.Validate,
37+
c.Output.Validate,
3838
c.LintersSettings.Validate,
3939
c.Linters.Validate,
40-
c.Output.Validate,
41-
c.Run.Validate,
40+
c.Issues.Validate,
41+
c.Severity.Validate,
4242
}
4343

4444
for _, v := range validators {

pkg/config/loader.go

+36-26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ type LoaderOptions struct {
2424
NoConfig bool // Flag only.
2525
}
2626

27+
type LoadOptions struct {
28+
CheckDeprecation bool
29+
Validation bool
30+
}
31+
2732
type Loader struct {
2833
opts LoaderOptions
2934

@@ -47,7 +52,7 @@ func NewLoader(log logutils.Log, v *viper.Viper, fs *pflag.FlagSet, opts LoaderO
4752
}
4853
}
4954

50-
func (l *Loader) Load() error {
55+
func (l *Loader) Load(opts LoadOptions) error {
5156
err := l.setConfigFile()
5257
if err != nil {
5358
return err
@@ -60,9 +65,11 @@ func (l *Loader) Load() error {
6065

6166
l.applyStringSliceHack()
6267

63-
err = l.handleDeprecation()
64-
if err != nil {
65-
return err
68+
if opts.CheckDeprecation {
69+
err = l.handleDeprecation()
70+
if err != nil {
71+
return err
72+
}
6673
}
6774

6875
l.handleGoVersion()
@@ -72,6 +79,13 @@ func (l *Loader) Load() error {
7279
return err
7380
}
7481

82+
if opts.Validation {
83+
err = l.cfg.Validate()
84+
if err != nil {
85+
return err
86+
}
87+
}
88+
7589
return nil
7690
}
7791

@@ -293,35 +307,39 @@ func (l *Loader) handleGoVersion() {
293307
}
294308

295309
func (l *Loader) handleDeprecation() error {
310+
if l.cfg.InternalTest || l.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" {
311+
return nil
312+
}
313+
296314
// Deprecated since v1.57.0
297315
if len(l.cfg.Run.SkipFiles) > 0 {
298-
l.warn("The configuration option `run.skip-files` is deprecated, please use `issues.exclude-files`.")
316+
l.log.Warnf("The configuration option `run.skip-files` is deprecated, please use `issues.exclude-files`.")
299317
l.cfg.Issues.ExcludeFiles = l.cfg.Run.SkipFiles
300318
}
301319

302320
// Deprecated since v1.57.0
303321
if len(l.cfg.Run.SkipDirs) > 0 {
304-
l.warn("The configuration option `run.skip-dirs` is deprecated, please use `issues.exclude-dirs`.")
322+
l.log.Warnf("The configuration option `run.skip-dirs` is deprecated, please use `issues.exclude-dirs`.")
305323
l.cfg.Issues.ExcludeDirs = l.cfg.Run.SkipDirs
306324
}
307325

308326
// The 2 options are true by default.
309327
// Deprecated since v1.57.0
310328
if !l.cfg.Run.UseDefaultSkipDirs {
311-
l.warn("The configuration option `run.skip-dirs-use-default` is deprecated, please use `issues.exclude-dirs-use-default`.")
329+
l.log.Warnf("The configuration option `run.skip-dirs-use-default` is deprecated, please use `issues.exclude-dirs-use-default`.")
312330
}
313331
l.cfg.Issues.UseDefaultExcludeDirs = l.cfg.Run.UseDefaultSkipDirs && l.cfg.Issues.UseDefaultExcludeDirs
314332

315333
// The 2 options are false by default.
316334
// Deprecated since v1.57.0
317335
if l.cfg.Run.ShowStats {
318-
l.warn("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`")
336+
l.log.Warnf("The configuration option `run.show-stats` is deprecated, please use `output.show-stats`")
319337
}
320338
l.cfg.Output.ShowStats = l.cfg.Run.ShowStats || l.cfg.Output.ShowStats
321339

322340
// Deprecated since v1.57.0
323341
if l.cfg.Output.Format != "" {
324-
l.warn("The configuration option `output.format` is deprecated, please use `output.formats`")
342+
l.log.Warnf("The configuration option `output.format` is deprecated, please use `output.formats`")
325343

326344
var f OutputFormats
327345
err := f.UnmarshalText([]byte(l.cfg.Output.Format))
@@ -341,49 +359,49 @@ func (l *Loader) handleLinterOptionDeprecations() {
341359
// Deprecated since v1.57.0,
342360
// but it was unofficially deprecated since v1.19 (2019) (https://github.com/golangci/golangci-lint/pull/697).
343361
if l.cfg.LintersSettings.Govet.CheckShadowing {
344-
l.warn("The configuration option `linters.govet.check-shadowing` is deprecated. " +
362+
l.log.Warnf("The configuration option `linters.govet.check-shadowing` is deprecated. " +
345363
"Please enable `shadow` instead, if you are not using `enable-all`.")
346364
}
347365

348366
// Deprecated since v1.42.0.
349367
if l.cfg.LintersSettings.Errcheck.Exclude != "" {
350-
l.warn("The configuration option `linters.errcheck.exclude` is deprecated, please use `linters.errcheck.exclude-functions`.")
368+
l.log.Warnf("The configuration option `linters.errcheck.exclude` is deprecated, please use `linters.errcheck.exclude-functions`.")
351369
}
352370

353371
// Deprecated since v1.44.0.
354372
if l.cfg.LintersSettings.Gci.LocalPrefixes != "" {
355-
l.warn("The configuration option `linters.gci.local-prefixes` is deprecated, please use `prefix()` inside `linters.gci.sections`.")
373+
l.log.Warnf("The configuration option `linters.gci.local-prefixes` is deprecated, please use `prefix()` inside `linters.gci.sections`.")
356374
}
357375

358376
// Deprecated since v1.33.0.
359377
if l.cfg.LintersSettings.Godot.CheckAll {
360-
l.warn("The configuration option `linters.godot.check-all` is deprecated, please use `linters.godot.scope: all`.")
378+
l.log.Warnf("The configuration option `linters.godot.check-all` is deprecated, please use `linters.godot.scope: all`.")
361379
}
362380

363381
// Deprecated since v1.44.0.
364382
if len(l.cfg.LintersSettings.Gomnd.Settings) > 0 {
365-
l.warn("The configuration option `linters.gomnd.settings` is deprecated. Please use the options " +
383+
l.log.Warnf("The configuration option `linters.gomnd.settings` is deprecated. Please use the options " +
366384
"`linters.gomnd.checks`,`linters.gomnd.ignored-numbers`,`linters.gomnd.ignored-files`,`linters.gomnd.ignored-functions`.")
367385
}
368386

369387
// Deprecated since v1.47.0
370388
if l.cfg.LintersSettings.Gofumpt.LangVersion != "" {
371-
l.warn("The configuration option `linters.gofumpt.lang-version` is deprecated, please use global `run.go`.")
389+
l.log.Warnf("The configuration option `linters.gofumpt.lang-version` is deprecated, please use global `run.go`.")
372390
}
373391

374392
// Deprecated since v1.47.0
375393
if l.cfg.LintersSettings.Staticcheck.GoVersion != "" {
376-
l.warn("The configuration option `linters.staticcheck.go` is deprecated, please use global `run.go`.")
394+
l.log.Warnf("The configuration option `linters.staticcheck.go` is deprecated, please use global `run.go`.")
377395
}
378396

379397
// Deprecated since v1.47.0
380398
if l.cfg.LintersSettings.Gosimple.GoVersion != "" {
381-
l.warn("The configuration option `linters.gosimple.go` is deprecated, please use global `run.go`.")
399+
l.log.Warnf("The configuration option `linters.gosimple.go` is deprecated, please use global `run.go`.")
382400
}
383401

384402
// Deprecated since v1.47.0
385403
if l.cfg.LintersSettings.Stylecheck.GoVersion != "" {
386-
l.warn("The configuration option `linters.stylecheck.go` is deprecated, please use global `run.go`.")
404+
l.log.Warnf("The configuration option `linters.stylecheck.go` is deprecated, please use global `run.go`.")
387405
}
388406
}
389407

@@ -408,14 +426,6 @@ func (l *Loader) handleEnableOnlyOption() error {
408426
return nil
409427
}
410428

411-
func (l *Loader) warn(format string) {
412-
if l.cfg.InternalTest || l.cfg.InternalCmdTest || os.Getenv(logutils.EnvTestRun) == "1" {
413-
return
414-
}
415-
416-
l.log.Warnf(format)
417-
}
418-
419429
func customDecoderHook() viper.DecoderConfigOption {
420430
return viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
421431
// Default hooks (https://github.com/spf13/viper/blob/518241257478c557633ab36e474dfcaeb9a3c623/viper.go#L135-L138).

pkg/lint/lintersdb/validator.go

-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ func NewValidator(m *Manager) *Validator {
2222
// Validate validates the configuration by calling all other validators for different
2323
// sections in the configuration and then some additional linter validation functions.
2424
func (v Validator) Validate(cfg *config.Config) error {
25-
err := cfg.Validate()
26-
if err != nil {
27-
return err
28-
}
29-
3025
validators := []func(cfg *config.Linters) error{
3126
v.validateLintersNames,
3227
v.validatePresets,

0 commit comments

Comments
 (0)