golangci/golangci-lint · error

get git root: %w

Error message

get git root: %w

What it means

GetBasePath returns this wrapped error when the relative path mode is RelativePathModeGitRoot and gitRoot(ctx) fails. gitRoot runs `git rev-parse --show-toplevel`; any failure (not a git repository, git not installed, command error) is preserved via %w. golangci-lint uses the repo root to make reported file paths relative.

Source

Thrown at pkg/fsutils/basepath.go:51

	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)
		}

		return wd, nil

	default:
		return "", fmt.Errorf("unknown relative path mode: %s", mode)
	}
}

func gitRoot(ctx context.Context) (string, error) {

View on GitHub (pinned to ed7a235d2d)

Solutions

  1. Run golangci-lint inside a git repository (git clone instead of downloading archives)
  2. Fix git environment issues shown in the wrapped error, e.g. `git config --global --add safe.directory <path>` for ownership errors, or install git
  3. Change run.relative-path-mode to 'gomod' or 'wd' when a git root is unavailable

Example fix

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

Strategy: try-catch

Validate before calling

cmd := exec.Command("git", "rev-parse", "--show-toplevel")
if err := cmd.Run(); err != nil {
  return fmt.Errorf("not inside a git work tree; git-root path mode will fail")
}

Try / catch

basePath, err := fsutils.GetBasePath(ctx, fsutils.RelativePathModeGitRoot)
if err != nil {
  // fall back to wd mode or fix git env per wrapped error
  basePath, err = fsutils.GetBasePath(ctx, fsutils.RelativePathModeWd)
}

Prevention

When it happens

Trigger: Running golangci-lint with run.relative-path-mode: git-root outside a git work tree, in a repository with ownership/safe.directory problems, or where the git binary is missing.

Common situations: Downloading a project as a zip (no .git) and running the linter in it; CI checkouts with dubious-ownership errors; Docker images that strip the git binary.

Related errors


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