{"record":{"id":"95fbc24a3c5cc7ed","repo":"golang/go","slug":"directory-q-is-outside-source-root-q","errorCode":null,"errorMessage":"directory %q is outside source root %q","messagePattern":"directory %q is outside source root %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/vcs/vcs.go","lineNumber":494,"sourceCode":"\trepo           string                              // repository to use (expand with match of re)\n\tvcs            string                              // version control system to use (expand with match of re)\n\tcheck          func(match map[string]string) error // additional checks\n\tschemelessRepo bool                                // if true, the repo pattern lacks a scheme\n}\n\nvar allowmultiplevcs = godebug.New(\"allowmultiplevcs\")\n\n// FromDir inspects dir and its parents to determine the\n// version control system and code repository to use.\n// If no repository is found, FromDir returns an error\n// equivalent to os.ErrNotExist.\nfunc FromDir(dir, srcRoot string) (repoDir string, vcsCmd *Cmd, err error) {\n\t// Clean and double-check that dir is in (a subdirectory of) srcRoot.\n\tdir = filepath.Clean(dir)\n\tif srcRoot != \"\" {\n\t\tsrcRoot = filepath.Clean(srcRoot)\n\t\tif len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator {\n\t\t\treturn \"\", nil, fmt.Errorf(\"directory %q is outside source root %q\", dir, srcRoot)\n\t\t}\n\t}\n\n\torigDir := dir\n\tfor len(dir) > len(srcRoot) {\n\t\tfor _, vcs := range vcsList {\n\t\t\tif isVCSRootDir(dir, vcs.Roots) {\n\t\t\t\tif vcsCmd == nil {\n\t\t\t\t\t// Record first VCS we find.\n\t\t\t\t\tvcsCmd = vcs\n\t\t\t\t\trepoDir = dir\n\t\t\t\t\tif allowmultiplevcs.Value() == \"1\" {\n\t\t\t\t\t\tallowmultiplevcs.IncNonDefault()\n\t\t\t\t\t\treturn repoDir, vcsCmd, nil\n\t\t\t\t\t}\n\t\t\t\t\t// If allowmultiplevcs is not set, keep looking for\n\t\t\t\t\t// repositories in current and parent directories and report\n\t\t\t\t\t// an error if one is found to mitigate VCS injection","sourceCodeStart":476,"sourceCodeEnd":512,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/vcs/vcs.go#L476-L512","documentation":"In GOPATH mode (GO111MODULE=off), FromDir walks up from the package directory to locate a VCS root. As a precondition it verifies the directory is a strict subdirectory of the configured source root (GOPATH/src). If the directory equals or is not properly contained within srcRoot, it is rejected.","triggerScenarios":"GOPATH mode build/test where the package directory resolves to a path that is not inside GOPATH/src — the check `len(dir) <= len(srcRoot) || dir[len(srcRoot)] != Separator` fails.","commonSituations":"GOPATH not set or set incorrectly; package directory outside GOPATH/src; using a relative path that resolves outside GOPATH; symlink escaping the source root.","solutions":["Ensure GOPATH is set correctly: `go env GOPATH`","Place your package under $GOPATH/src/<import-path>/","Switch to module mode: run `go mod init` in your project and remove GO111MODULE=off","Verify the directory is a genuine subdirectory of GOPATH/src (not a sibling or parent)"],"exampleFix":"# before: package outside GOPATH/src in GOPATH mode\nexport GO111MODULE=off\ngo build /tmp/myproject  # fails\n\n# after: use module mode instead\ngo mod init example.com/myproject  # in the project dir\ngo build ./...","handlingStrategy":"validation","validationCode":"# In GOPATH mode, verify the package dir is inside GOPATH/src\nSRCROOT=\"$(go env GOPATH)/src\"\nPKGDIR=\"$(pwd)\"\ncase \"$PKGDIR/\" in\n  \"$SRCROOT\"/*) echo 'OK: inside GOPATH/src' ;;\n  *) echo \"ERROR: $PKGDIR is outside GOPATH/src ($SRCROOT)\" ;;\nesac","typeGuard":null,"tryCatchPattern":"# Detect and guide: suggest module mode\ngo build ./... 2>&1 | grep -q 'outside source root' && {\n  echo 'Switch to module mode: run go mod init in your project'\n  exit 1\n}","preventionTips":["Prefer module mode (go.mod) over GOPATH mode for new projects","If using GOPATH mode, always work inside $GOPATH/src","Verify GOPATH is set: `go env GOPATH`"],"tags":["vcs","gopath","filesystem","legacy"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}