{"record":{"id":"cd07fed76b31891e","repo":"golang/go","slug":"directory-prefix-s-does-not-contain-s","errorCode":null,"errorMessage":"directory prefix %s does not contain %s","messagePattern":"directory prefix (.+?) does not contain (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/load.go","lineNumber":544,"sourceCode":"\t\t// The pattern is local, but it is a wildcard. Its packages will\n\t\t// only resolve to paths if they are inside of the standard\n\t\t// library, the main module, or some dependency of the main\n\t\t// module. Verify that before we walk the filesystem: a filesystem\n\t\t// walk in a directory like /var or /etc can be very expensive!\n\t\tdir := filepath.Dir(filepath.Clean(m.Pattern()[:i+3]))\n\t\tabsDir := dir\n\t\tif !filepath.IsAbs(dir) {\n\t\t\tabsDir = filepath.Join(base.Cwd(), dir)\n\t\t}\n\n\t\tmodRoot := findModuleRoot(absDir)\n\t\tif !slices.Contains(modRoots, modRoot) && search.InDir(absDir, cfg.GOROOTsrc) == \"\" && pathInModuleCache(ld, ctx, absDir, rs) == \"\" {\n\t\t\tm.Dirs = []string{}\n\t\t\tscope := \"main module or its selected dependencies\"\n\t\t\tif ld.inWorkspaceMode() {\n\t\t\t\tscope = \"modules listed in go.work or their selected dependencies\"\n\t\t\t}\n\t\t\tm.AddError(fmt.Errorf(\"directory prefix %s does not contain %s\", base.ShortPath(absDir), scope))\n\t\t\treturn\n\t\t}\n\t}\n\n\tm.MatchDirs(modRoots)\n}\n\n// resolveLocalPackage resolves a filesystem path to a package path.\nfunc resolveLocalPackage(ld *Loader, ctx context.Context, dir string, rs *Requirements) (string, error) {\n\tvar absDir string\n\tif filepath.IsAbs(dir) {\n\t\tabsDir = filepath.Clean(dir)\n\t} else {\n\t\tabsDir = filepath.Join(base.Cwd(), dir)\n\t}\n\n\tbp, err := cfg.BuildContext.ImportDir(absDir, 0)\n\tif err != nil && (bp == nil || len(bp.IgnoredGoFiles) == 0) {","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/load.go#L526-L562","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before — running from outside the module\ncd /tmp && go build ./myproject/...\n\n# after — run from inside the module\ncd /path/to/module && go build ./...","handlingStrategy":"validation","validationCode":"// Before running 'go build ./...', verify the cwd is inside a module.\nfunc ensureInModule() error {\n    if mod := os.Getenv(\"GOMOD\"); mod == \"\" || mod == \"/dev/null\" {\n        return fmt.Errorf(\"not inside a Go module; cd to module root or run 'go mod init'\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["package-resolution","wildcard","go-build","go-list","module-root"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}