Help us shape the future of Go! Take the 2026 Go Developer Survey

gomoddirectives

package module
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2026 License: Apache-2.0 Imports: 14 Imported by: 22

README

gomoddirectives

A linter that handle directives into go.mod.

Sponsor

Usage

Inside golangci-lint

Recommended.

linters:
  enable:
    - gomoddirectives

  settings:
    gomoddirectives:
      # Allow local `replace` directives.
      # Default: false
      replace-local: true
      
      # List of allowed `replace` directives.
      # Default: []
      replace-allow-list:
        - launchpad.net/gocheck

      # Allow all `replace` directives.
      # Default: false
      replace-allow-all: true

      # Allow to not explain why the version has been retracted in the `retract` directives.
      # Default: false
      retract-allow-no-explanation: true
      
      # Forbid the use of the `exclude` directives.
      # Default: false
      exclude-forbidden: true
  
      # Forbid the use of the `ignore` directives (go >= 1.25).
      # Default: false
      ignore-forbidden: true
  
      # 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\.22\.\d+$'
  
      # 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: '1\.\d+(\.0)?$'

      # Check the validity of the module path.
      # Default: false
      check-module-path: true
As a CLI
gomoddirectives [flags]

Flags:
  -check-module-path
        Check module path validity
  -exclude
        Forbid the use of exclude directives
  -godebug
        Forbid the use of godebug directives
  -goversion string
        Pattern to validate go min version directive
  -h    Show this help.
  -ignore
        Forbid the use of ignore directives
  -list value
        List of allowed replace directives
  -local
        Allow local replace directives
  -all-replace
        Allow all replace directives
  -retract-no-explanation
        Allow to use retract directives without explanation
  -tool
        Forbid the use of tool directives
  -toolchain
        Forbid the use of toolchain directive
  -toolchain-pattern string
        Pattern to validate toolchain directive

Details

retract directives
  • Force explanation for retract directives.
module example.com/foo

go 1.22

require (
	github.com/ldez/grignotin v0.4.1
)

retract (
    v1.0.0 // Explanation
)
replace directives
  • Ban all replace directives.
  • Allow only local replace directives.
  • Allow only some replace directives.
  • Allow all replace directives.
  • Detect duplicated replace directives.
  • Detect identical replace directives.
module example.com/foo

go 1.22

require (
	github.com/ldez/grignotin v0.4.1
)

replace github.com/ldez/grignotin => ../grignotin/
exclude directives
  • Ban all exclude directives.
module example.com/foo

go 1.22

require (
	github.com/ldez/grignotin v0.4.1
)

exclude (
    golang.org/x/crypto v1.4.5
    golang.org/x/text v1.6.7
)
ignore directives
  • Ban all ignore directives.
module example.com/foo

go 1.25

require (
	github.com/ldez/grignotin v0.4.1
)

ignore (
    ./foo/bar/path
    foo/bar
)
tool directives
  • Ban all tool directives.
module example.com/foo

go 1.24

tool (
    example.com/module/cmd/a
    example.com/module/cmd/b
)
toolchain directive
  • Ban toolchain directive.
  • Use a regular expression to constraint the Go minimum version.
module example.com/foo

go 1.22

toolchain go1.23.3
godebug directives
  • Ban godebug directive.
module example.com/foo

go 1.22

godebug default=go1.21
godebug (
    panicnil=1
    asynctimerchan=0
)
go directive
  • Use a regular expression to constraint the Go minimum version.
module example.com/foo

go 1.22.0
module path
  • Check the validity of the module path.
module example.com/foo

go 1.22

Documentation

Overview

Package gomoddirectives a linter that handle directives into `go.mod`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetModuleFile

func GetModuleFile() (*modfile.File, error)

GetModuleFile gets module file.

Types

type Options

type Options struct {
	ReplaceAllowAll           bool
	ReplaceAllowList          []string
	ReplaceAllowLocal         bool
	ExcludeForbidden          bool
	IgnoreForbidden           bool
	RetractAllowNoExplanation bool
	ToolchainForbidden        bool
	ToolchainPattern          *regexp.Regexp
	ToolForbidden             bool
	GoDebugForbidden          bool
	GoVersionPattern          *regexp.Regexp
	CheckModulePath           bool
}

Options the analyzer options.

type Result

type Result struct {
	Reason string
	Start  token.Position
	End    token.Position
}

Result the analysis result.

func Analyze

func Analyze(opts Options) ([]Result, error)

Analyze analyzes a project.

func AnalyzeFile added in v0.2.0

func AnalyzeFile(file *modfile.File, opts Options) []Result

AnalyzeFile analyzes a mod file.

func AnalyzePass added in v0.4.0

func AnalyzePass(pass *analysis.Pass, opts Options) ([]Result, error)

AnalyzePass analyzes a pass.

func NewResult

func NewResult(file *modfile.File, line *modfile.Line, reason string) Result

NewResult creates a new Result.

func (Result) String

func (r Result) String() string

Directories

Path Synopsis
cmd
gomoddirectives command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL