golang/go · error

%v is not a regular file

Error message

%v is not a regular file

What it means

Thrown by lookDir (during `go work use`) when a candidate directory's go.mod exists but is not a regular file — it is a symlink to nowhere, a directory, a device, a pipe, etc. The go command requires go.mod to be a genuine regular file before adding the directory to the workfile.

Source

Thrown at src/cmd/go/internal/workcmd/use.go:121

	// lookDir updates the entry in keepDirs for the directory dir,
	// which is either absolute or relative to the current working directory
	// (not necessarily the directory containing the workfile).
	lookDir := func(dir string) {
		absDir, dir := pathRel(workDir, dir)

		file := filepath.Join(absDir, "go.mod")
		fi, err := fsys.Stat(file)
		if err != nil {
			if os.IsNotExist(err) {
				keepDirs[absDir] = ""
			} else {
				sw.Error(err)
			}
			return
		}

		if !fi.Mode().IsRegular() {
			sw.Error(fmt.Errorf("%v is not a regular file", base.ShortPath(file)))
			return
		}

		if dup := keepDirs[absDir]; dup != "" && dup != dir {
			base.Errorf(`go: already added "%s" as "%s"`, dir, dup)
		}
		keepDirs[absDir] = dir
	}

	for _, useDir := range args {
		absArg, _ := pathRel(workDir, useDir)

		info, err := fsys.Stat(absArg)
		if err != nil {
			// Errors raised from os.Stat are formatted to be more user-friendly.
			if os.IsNotExist(err) {
				err = fmt.Errorf("directory %v does not exist", base.ShortPath(absArg))
			}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Inspect the entry: `ls -l ./mymod/go.mod` and `file ./mymod/go.mod`.
  2. If it is a broken symlink, remove it and recreate the file (e.g. `rm go.mod && go mod init`).
  3. If go.mod is a directory, rename it and run `go mod init`.
  4. Re-clone the module/repository if the entry is corrupt.

Example fix

# before
// ls -l mymod/go.mod  -> symlink to nonexistent

# after
// rm mymod/go.mod && (cd mymod && go mod init example.com/mymod)
Defensive patterns

Strategy: validation

Validate before calling

// Verify go.mod is a regular file before `go work use`
goMod := filepath.Join(dir, "go.mod")
fi, err := os.Lstat(goMod)
if err != nil { log.Fatalf("go.mod missing: %v", err) }
if !fi.Mode().IsRegular() {
    log.Fatalf("%s is not a regular file (mode %s)", goMod, fi.Mode())
}

Prevention

When it happens

Trigger: Run `go work use ./mymod` where `./mymod/go.mod` is a broken symlink, a directory (someone ran `mkdir go.mod`), or some other non-regular filesystem entry. fsys.Stat succeeds but fi.Mode().IsRegular() is false, so lookDir reports the error via ShortPath.

Common situations: A broken symlink left by a failed `ln -s`; go.mod accidentally created as a directory; a FUSE bind mount presenting go.mod oddly; a git submodule checkout where go.mod is a sparse-checkout placeholder; tooling that replaced go.mod with a fifo/socket.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/a360fe6581d2272e. Report an issue: GitHub.