{"record":{"id":"20903a2135b1a635","repo":"golang/go","slug":"directory-s-is-outside-module-root-s-s","errorCode":null,"errorMessage":"directory %s is outside module root%s (%s)","messagePattern":"directory (.+?) is outside module root(.+?) \\((.+?)\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/search/search.go","lineNumber":343,"sourceCode":"\tif len(modRoots) > 0 {\n\t\tabs, err := filepath.Abs(dir)\n\t\tif err != nil {\n\t\t\tm.AddError(err)\n\t\t\treturn\n\t\t}\n\t\tvar found bool\n\t\tfor _, mr := range modRoots {\n\t\t\tif mr != \"\" && str.HasFilePathPrefix(abs, mr) {\n\t\t\t\tfound = true\n\t\t\t\tmodRoot = mr\n\t\t\t}\n\t\t}\n\t\tif !found {\n\t\t\tplural := \"\"\n\t\t\tif len(modRoots) > 1 {\n\t\t\t\tplural = \"s\"\n\t\t\t}\n\t\t\tm.AddError(fmt.Errorf(\"directory %s is outside module root%s (%s)\", abs, plural, strings.Join(modRoots, \", \")))\n\t\t}\n\t}\n\n\tignorePatterns := parseIgnorePatterns(modRoot)\n\ttags := imports.Tags()\n\t// If dir is actually a symlink to a directory,\n\t// we want to follow it (see https://go.dev/issue/50807).\n\t// Add a trailing separator to force that to happen.\n\tdir = str.WithFilePathSeparator(dir)\n\terr := fsys.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {\n\t\tif err != nil {\n\t\t\treturn err // Likely a permission error, which could interfere with matching.\n\t\t}\n\t\tif !d.IsDir() {\n\t\t\treturn nil\n\t\t}\n\t\ttop := false\n\t\tif path == dir {","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/search/search.go#L325-L361","documentation":"Thrown during package-pattern matching (e.g., `go list ./...`). The go tool resolves the wildcard pattern's starting directory to an absolute path and checks whether it begins with any known module root. If the directory is not inside any module or workspace member root, the scan is aborted because packages outside module roots have no resolvable import path.","triggerScenarios":"Running `go list`, `go build`, or `go test` with a `...` wildcard from a directory outside all module roots. Fires in MatchPatternTree when len(modRoots) > 0 and str.HasFilePathPrefix(abs, mr) is false for every mr in modRoots.","commonSituations":"Wrong working directory (e.g., in /tmp instead of the module root); a go.work file that does not cover the current directory; symlinks whose target resolves outside the module tree; confusion between GOPATH mode and module mode.","solutions":["cd into your module's root directory (where go.mod lives) before running the command","Verify with `go env GOMOD` — it must show a go.mod path, not be empty","If using a workspace, ensure go.work includes the module that covers your directory (`go work use .`)","Avoid invoking go commands through symlinks that resolve outside the module"],"exampleFix":"// before (run from /tmp — outside any module)\ngo list ./myproject/...\n\n// after (run from inside the module root)\ncd /path/to/myproject\ngo list ./...","handlingStrategy":"validation","validationCode":"# Verify you're inside a module before running wildcard patterns\nif [ -z \"$(go env GOMOD)\" ] || [ \"$(go env GOMOD)\" = \"/dev/null\" ]; then\n  echo \"ERROR: not inside a Go module — cd to module root first\"\n  exit 1\nfi\ngo list ./...","typeGuard":null,"tryCatchPattern":"# In a script, check for the error and guide the user:\nif ! go list ./... 2>&1 | grep -q 'outside module root'; then\n  echo 'packages matched successfully'\nelse\n  echo 'Run from inside the module root (where go.mod lives)'\n  exit 1\nfi","preventionTips":["Always cd into the module root before running go commands with wildcards","Add a pre-commit or CI check that verifies `go env GOMOD` is non-empty","Avoid running go commands through symlinks that may resolve outside the module"],"tags":["filesystem","module","go-toolchain","package-matching"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}