diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 22a0cb066b98..0463522b7e30 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -1369,6 +1369,9 @@ linters-settings: # Forbid the use of the `toolchain` directive. # Default: false toolchain-forbidden: true + # Defines a pattern to validate `toolchain` directive. + # Default: '' (no match) + toolchain-pattern: 'go1\.23\.\d+$' # Forbid the use of the `tool` directives. # Default: false tool-forbidden: true diff --git a/go.mod b/go.mod index 44c8747b7b27..d7ec60c956e7 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.5.0 + github.com/ldez/gomoddirectives v0.6.0 github.com/ldez/grignotin v0.6.0 github.com/ldez/tagliatelle v0.6.0 github.com/ldez/usetesting v0.2.2 diff --git a/go.sum b/go.sum index c584bca80d19..2604368bd86b 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.5.0 h1:x16F2o7rXblmwVP2AW/OwYP3VUid6wqWlyERwNP5FqA= -github.com/ldez/gomoddirectives v0.5.0/go.mod h1:TuwOGYoPAoENDWQpe8DMqEm5nIfjrxZXmxX/CExWyZ4= +github.com/ldez/gomoddirectives v0.6.0 h1:Jyf1ZdTeiIB4dd+2n4qw+g4aI9IJ6JyfOZ8BityWvnA= +github.com/ldez/gomoddirectives v0.6.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.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index e8660eb5f558..db3a7523e04c 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1680,6 +1680,10 @@ "type": "boolean", "default": false }, + "toolchain-pattern": { + "description": "Defines a pattern to validate `toolchain` directive.", + "type": "string" + }, "tool-forbidden": { "description": "Forbid the use of the `tool` directives.", "type": "boolean", diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 4496ab7aba6d..6738399b9147 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -586,6 +586,7 @@ type GoModDirectivesSettings struct { ExcludeForbidden bool `mapstructure:"exclude-forbidden"` RetractAllowNoExplanation bool `mapstructure:"retract-allow-no-explanation"` ToolchainForbidden bool `mapstructure:"toolchain-forbidden"` + ToolchainPattern string `mapstructure:"toolchain-pattern"` ToolForbidden bool `mapstructure:"tool-forbidden"` GoDebugForbidden bool `mapstructure:"go-debug-forbidden"` GoVersionPattern string `mapstructure:"go-version-pattern"` diff --git a/pkg/golinters/gomoddirectives/gomoddirectives.go b/pkg/golinters/gomoddirectives/gomoddirectives.go index 88c93d70402f..f8f47ba2b473 100644 --- a/pkg/golinters/gomoddirectives/gomoddirectives.go +++ b/pkg/golinters/gomoddirectives/gomoddirectives.go @@ -30,6 +30,15 @@ func New(settings *config.GoModDirectivesSettings) *goanalysis.Linter { opts.ToolForbidden = settings.ToolForbidden opts.GoDebugForbidden = settings.GoDebugForbidden + if settings.ToolchainPattern != "" { + exp, err := regexp.Compile(settings.ToolchainPattern) + if err != nil { + internal.LinterLogger.Fatalf("%s: invalid toolchain pattern: %v", linterName, err) + } else { + opts.ToolchainPattern = exp + } + } + if settings.GoVersionPattern != "" { exp, err := regexp.Compile(settings.GoVersionPattern) if err != nil {