golang/go · error
directory %v does not exist
Error message
directory %v does not exist
What it means
Thrown by `go work use` when fsys.Stat fails on a user-supplied path and os.IsNotExist is true. The command walks each argument with pathRel, stats the absolute path, and rewrites a raw os.Stat error into this friendly message via base.ShortPath. It is purely a path-existence check before the directory is considered for the go.work use directive.
Source
Thrown at src/cmd/go/internal/workcmd/use.go:138
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))
}
sw.Error(err)
continue
} else if !info.IsDir() {
sw.Error(fmt.Errorf("%s is not a directory", base.ShortPath(absArg)))
continue
}
if !*useR {
lookDir(useDir)
continue
}
// Add or remove entries for any subdirectories that still exist.
// If the root itself is a symlink to a directory,
// we want to follow it (see https://go.dev/issue/50807).
// Add a trailing separator to force that to happen.
fsys.WalkDir(str.WithFilePathSeparator(useDir), func(path string, d fs.DirEntry, err error) error {View on GitHub (pinned to b6b368adc5)
Solutions
- Verify the path exists: `ls -la <path>` or `test -d <path>` before re-running.
- Check your current working directory and use an absolute path or correct relative path.
- If the directory was deleted intentionally, remove the corresponding `use` directive from go.work manually or run `go work edit -dropuse=<path>`.
- Recreate the directory if it should exist (e.g. `mkdir -p <path>` and ensure it contains a go.mod).
Example fix
// before go work use ./mymoddule // typo // after go work use ./mymodule
Defensive patterns
Strategy: validation
Validate before calling
// before `go work use <p>`:
info, err := os.Stat(p)
if err != nil || !info.IsDir() {
log.Fatalf("%s is not an existing directory", p)
} Prevention
- Resolve paths to absolute form before passing to `go work use`.
- Validate directories with os.Stat + IsDir in scripts that build go.work.
- Run `go work use` from a fixed cwd (the go.work root) to keep relative paths stable.
When it happens
Trigger: Running `go work use <path>` (or `go work use -r <path>`) where <path> does not resolve to anything on disk. The loop at use.go:131 calls fsys.Stat(absArg); a non-existent absArg yields ENOENT and this error is reported through sw.Error.
Common situations: Typos in the directory argument, running `go work use` from the wrong working directory, relative paths that assume a different cwd, paths that were deleted/moved between shell tab-completion and command execution, or symlinks pointing at removed targets.
Related errors
- %s is not a directory
- reading go.work: %w
- file is empty
- deleted in overlay
- cannot open directory in overlay
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/008c1e04cc22428b.
Report an issue: GitHub.