golang/go · error
directory prefix %s does not contain %s
Error message
directory prefix %s does not contain %s
What it means
Thrown during package pattern matching (load.go:544) when a wildcard pattern like './...' has a directory prefix that is not inside any module root, not inside GOROOT/src, and not in the module cache. The scope string distinguishes workspace mode ('modules listed in go.work or their selected dependencies') from normal mode ('main module or its selected dependencies'). This prevents expensive filesystem walks in unrelated directories like /var or /etc.
Source
Thrown at src/cmd/go/internal/modload/load.go:544
// The pattern is local, but it is a wildcard. Its packages will
// only resolve to paths if they are inside of the standard
// library, the main module, or some dependency of the main
// module. Verify that before we walk the filesystem: a filesystem
// walk in a directory like /var or /etc can be very expensive!
dir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3]))
absDir := dir
if !filepath.IsAbs(dir) {
absDir = filepath.Join(base.Cwd(), dir)
}
modRoot := findModuleRoot(absDir)
if !slices.Contains(modRoots, modRoot) && search.InDir(absDir, cfg.GOROOTsrc) == "" && pathInModuleCache(ld, ctx, absDir, rs) == "" {
m.Dirs = []string{}
scope := "main module or its selected dependencies"
if ld.inWorkspaceMode() {
scope = "modules listed in go.work or their selected dependencies"
}
m.AddError(fmt.Errorf("directory prefix %s does not contain %s", base.ShortPath(absDir), scope))
return
}
}
m.MatchDirs(modRoots)
}
// resolveLocalPackage resolves a filesystem path to a package path.
func resolveLocalPackage(ld *Loader, ctx context.Context, dir string, rs *Requirements) (string, error) {
var absDir string
if filepath.IsAbs(dir) {
absDir = filepath.Clean(dir)
} else {
absDir = filepath.Join(base.Cwd(), dir)
}
bp, err := cfg.BuildContext.ImportDir(absDir, 0)
if err != nil && (bp == nil || len(bp.IgnoredGoFiles) == 0) {View on GitHub (pinned to b6b368adc5)
Solutions
- Change directory (cd) into the module root or a workspace-listed directory before running 'go build ./...'.
- If using workspaces, ensure go.work exists and lists the module via 'go work use ./mymodule'.
- Verify the pattern prefix actually points into the module by checking 'go env GOMOD' shows a valid go.mod.
Example fix
# before — running from outside the module cd /tmp && go build ./myproject/... # after — run from inside the module cd /path/to/module && go build ./...
Defensive patterns
Strategy: validation
Validate before calling
// Before running 'go build ./...', verify the cwd is inside a module.
func ensureInModule() error {
if mod := os.Getenv("GOMOD"); mod == "" || mod == "/dev/null" {
return fmt.Errorf("not inside a Go module; cd to module root or run 'go mod init'")
}
return nil
} Prevention
- Always cd into the module root (where go.mod lives) before running wildcard go commands.
- Use 'go env GOMOD' to confirm module context before building.
- In workspace mode, ensure go.work lists the relevant modules.
When it happens
Trigger: Running 'go build ./...' or 'go list ./...' from a directory whose prefix is outside the main module, the workspace modules, GOROOT/src, and the module cache. The check explicitly short-circuits before MatchDirs to avoid costly walks.
Common situations: Running go commands from /tmp, /home, or another non-module directory. Incorrect GOPATH or module setup. A typo in the pattern causing it to point outside the module. Missing go.work when expecting workspace mode.
Related errors
- %s outside main module or its selected dependencies
- without -mod=vendor, directory %s has no package path
- %s is contained in a module that is not one of the workspace
- local imports disallowed
- file not found
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/cd07fed76b31891e.
Report an issue: GitHub.