diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 402ac6511373..22a0cb066b98 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -1355,17 +1355,29 @@ linters-settings: gomoddirectives: # Allow local `replace` directives. # Default: false - replace-local: false + replace-local: true # List of allowed `replace` directives. # Default: [] replace-allow-list: - launchpad.net/gocheck # Allow to not explain why the version has been retracted in the `retract` directives. # Default: false - retract-allow-no-explanation: false + retract-allow-no-explanation: true # Forbid the use of the `exclude` directives. # Default: false - exclude-forbidden: false + exclude-forbidden: true + # Forbid the use of the `toolchain` directive. + # Default: false + toolchain-forbidden: true + # Forbid the use of the `tool` directives. + # Default: false + tool-forbidden: true + # Forbid the use of the `godebug` directive. + # Default: false + go-debug-forbidden: true + # Defines a pattern to validate `go` minimum version directive. + # Default: '' (no match) + go-version-pattern: '\d\.\d+(\.0)?' gomodguard: allowed: diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 708cf8048c17..68a0d1a90f66 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -1353,17 +1353,19 @@ linters-settings: gomoddirectives: # Allow local `replace` directives. # Default: false - replace-local: false + replace-local: true + # List of allowed `replace` directives. # Default: [] replace-allow-list: - launchpad.net/gocheck # Allow to not explain why the version has been retracted in the `retract` directives. # Default: false - retract-allow-no-explanation: false + retract-allow-no-explanation: true + # Forbid the use of the `exclude` directives. # Default: false - exclude-forbidden: false + exclude-forbidden: true gomodguard: allowed: diff --git a/go.mod b/go.mod index c0d43026e322..42ab96e750e0 100644 --- a/go.mod +++ b/go.mod @@ -66,7 +66,7 @@ require ( github.com/kunwardeep/paralleltest v1.0.10 github.com/kyoh86/exportloopref v0.1.11 github.com/lasiar/canonicalheader v1.1.2 - github.com/ldez/gomoddirectives v0.4.2 + github.com/ldez/gomoddirectives v0.5.0 github.com/ldez/grignotin v0.6.0 github.com/ldez/tagliatelle v0.6.0 github.com/ldez/usetesting v0.2.0 diff --git a/go.sum b/go.sum index 55a4fe3162e5..a6c2063ee400 100644 --- a/go.sum +++ b/go.sum @@ -351,8 +351,8 @@ github.com/kyoh86/exportloopref v0.1.11 h1:1Z0bcmTypkL3Q4k+IDHMWTcnCliEZcaPiIe0/ github.com/kyoh86/exportloopref v0.1.11/go.mod h1:qkV4UF1zGl6EkF1ox8L5t9SwyeBAZ3qLMd6up458uqA= github.com/lasiar/canonicalheader v1.1.2 h1:vZ5uqwvDbyJCnMhmFYimgMZnJMjwljN5VGY0VKbMXb4= github.com/lasiar/canonicalheader v1.1.2/go.mod h1:qJCeLFS0G/QlLQ506T+Fk/fWMa2VmBUiEI2cuMK4djI= -github.com/ldez/gomoddirectives v0.4.2 h1:qyiKJ97kyIZz8zi0qeKiXI73xEvQcDks4Tc/oN6R2eg= -github.com/ldez/gomoddirectives v0.4.2/go.mod h1:TuwOGYoPAoENDWQpe8DMqEm5nIfjrxZXmxX/CExWyZ4= +github.com/ldez/gomoddirectives v0.5.0 h1:x16F2o7rXblmwVP2AW/OwYP3VUid6wqWlyERwNP5FqA= +github.com/ldez/gomoddirectives v0.5.0/go.mod h1:TuwOGYoPAoENDWQpe8DMqEm5nIfjrxZXmxX/CExWyZ4= github.com/ldez/grignotin v0.6.0 h1:i++3002hxD5TpVto0iLjLrfz1V+yEJ+BBk4glb3aqC8= github.com/ldez/grignotin v0.6.0/go.mod h1:uaVTr0SoZ1KBii33c47O1M8Jp3OP3YDwhZCmzT9GHEk= github.com/ldez/tagliatelle v0.6.0 h1:1Muumft/shmQ0x96vA6a/OUgTjamRt8jUlZPLm1ruwA= diff --git a/jsonschema/golangci.jsonschema.json b/jsonschema/golangci.jsonschema.json index 4048750fedf5..e7c261b8b0dd 100644 --- a/jsonschema/golangci.jsonschema.json +++ b/jsonschema/golangci.jsonschema.json @@ -1638,7 +1638,7 @@ "replace-local": { "description": "Allow local `replace` directives.", "type": "boolean", - "default": true + "default": false }, "replace-allow-list": { "description": "List of allowed `replace` directives.", @@ -1650,12 +1650,12 @@ "retract-allow-no-explanation": { "description": "Allow to not explain why the version has been retracted in the `retract` directives.", "type": "boolean", - "default": true + "default": false }, "exclude-forbidden": { "description": "Forbid the use of the `exclude` directives.", "type": "boolean", - "default": true + "default": false } } }, diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 91366feef0c5..e8660eb5f558 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1668,12 +1668,32 @@ "retract-allow-no-explanation": { "description": "Allow to not explain why the version has been retracted in the `retract` directives.", "type": "boolean", - "default": true + "default": false }, "exclude-forbidden": { "description": "Forbid the use of the `exclude` directives.", "type": "boolean", - "default": true + "default": false + }, + "toolchain-forbidden": { + "description": "Forbid the use of the `toolchain` directive.", + "type": "boolean", + "default": false + }, + "tool-forbidden": { + "description": "Forbid the use of the `tool` directives.", + "type": "boolean", + "default": false + }, + "go-debug-forbidden": { + "description": "Forbid the use of the `godebug` directive.", + "type": "boolean", + "default": false + }, + "go-version-pattern": { + "description": "Defines a pattern to validate `go` minimum version directive.", + "type": "string", + "default": "" } } }, diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 6fca1003539f..4496ab7aba6d 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -585,6 +585,10 @@ type GoModDirectivesSettings struct { ReplaceLocal bool `mapstructure:"replace-local"` ExcludeForbidden bool `mapstructure:"exclude-forbidden"` RetractAllowNoExplanation bool `mapstructure:"retract-allow-no-explanation"` + ToolchainForbidden bool `mapstructure:"toolchain-forbidden"` + ToolForbidden bool `mapstructure:"tool-forbidden"` + GoDebugForbidden bool `mapstructure:"go-debug-forbidden"` + GoVersionPattern string `mapstructure:"go-version-pattern"` } type GoModGuardSettings struct { diff --git a/pkg/golinters/gomoddirectives/gomoddirectives.go b/pkg/golinters/gomoddirectives/gomoddirectives.go index 8fccbc89e11e..88c93d70402f 100644 --- a/pkg/golinters/gomoddirectives/gomoddirectives.go +++ b/pkg/golinters/gomoddirectives/gomoddirectives.go @@ -1,6 +1,7 @@ package gomoddirectives import ( + "regexp" "sync" "github.com/ldez/gomoddirectives" @@ -8,6 +9,7 @@ import ( "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/goanalysis" + "github.com/golangci/golangci-lint/pkg/golinters/internal" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/result" ) @@ -24,6 +26,18 @@ func New(settings *config.GoModDirectivesSettings) *goanalysis.Linter { opts.ReplaceAllowList = settings.ReplaceAllowList opts.RetractAllowNoExplanation = settings.RetractAllowNoExplanation opts.ExcludeForbidden = settings.ExcludeForbidden + opts.ToolchainForbidden = settings.ToolchainForbidden + opts.ToolForbidden = settings.ToolForbidden + opts.GoDebugForbidden = settings.GoDebugForbidden + + if settings.GoVersionPattern != "" { + exp, err := regexp.Compile(settings.GoVersionPattern) + if err != nil { + internal.LinterLogger.Fatalf("%s: invalid Go version pattern: %v", linterName, err) + } else { + opts.GoVersionPattern = exp + } + } } analyzer := &analysis.Analyzer{