golangci/golangci-lint · error

get go.mod path: %w

Error message

get go.mod path: %w

What it means

GetBasePath returns this wrapped error when the configured relative path mode is RelativePathModeGoMod and goenv.GetOne(ctx, goenv.GOMOD) fails to resolve the go.mod file path. The underlying cause (module resolution failure, missing toolchain, bad GOFLAGS, etc.) is preserved via %w. golangci-lint needs the go.mod directory to compute relative paths for reports.

Source

Thrown at pkg/fsutils/basepath.go:43

func AllRelativePathModes() []string {
	return []string{RelativePathModeGoMod, RelativePathModeGitRoot, RelativePathModeCfg, RelativePathModeWd}
}

func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
	mode = cmp.Or(mode, RelativePathModeCfg)

	switch mode {
	case RelativePathModeCfg:
		if cfgDir == "" {
			return GetBasePath(ctx, RelativePathModeWd, cfgDir)
		}

		return cfgDir, nil

	case RelativePathModeGoMod:
		goMod, err := goenv.GetOne(ctx, goenv.GOMOD)
		if err != nil {
			return "", fmt.Errorf("get go.mod path: %w", err)
		}

		return filepath.Dir(goMod), nil

	case RelativePathModeGitRoot:
		root, err := gitRoot(ctx)
		if err != nil {
			return "", fmt.Errorf("get git root: %w", err)
		}

		return root, nil

	case RelativePathModeWd:
		wd, err := Getwd()
		if err != nil {
			return "", fmt.Errorf("get wd: %w", err)
		}

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Fix the underlying `go env GOMOD` failure (inspect the wrapped error) — ensure the go toolchain is installed and on PATH
  2. Run golangci-lint inside a valid Go module directory that contains a go.mod
  3. Switch run.relative-path-mode to 'git-root' or 'wd' if go.mod resolution is not required

Example fix

// before (config)
run:
  relative-path-mode: gomod
// after
run:
  relative-path-mode: wd
Defensive patterns

Strategy: try-catch

Validate before calling

// verify go.mod resolvable before running
cmd := exec.Command("go", "env", "GOMOD")
if out, err := cmd.Output(); err != nil || strings.TrimSpace(string(out)) == "/dev/null" {
  return fmt.Errorf("no resolvable go.mod: %v %s", err, out)
}

Try / catch

basePath, err := fsutils.GetBasePath(ctx, mode)
var wrapped error
if errors.As(err, &wrapped) {
  log.Fatalf("get base path failed: %v", err) // inspect wrapped go env GOMOD cause
}

Prevention

When it happens

Trigger: Calling golangci-lint with run.relative-path-mode: gomod in a directory where `go env GOMOD` fails — e.g. no Go toolchain installed, or the go command errors resolving the module.

Common situations: Running golangci-lint outside a Go module with gomod mode forced; broken Go installation; CI images without the go binary; GOPATH/GOFLAGS env misconfiguration.

Related errors


AI-assisted analysis of golangci/golangci-lint@ed7a235d2d (2026-09-02). Data as JSON: /api/errors/1952f9c38890c918. Report an issue: GitHub.