{"record":{"id":"d36d4cc46360c367","repo":"golang/go","slug":"s-outside-main-module-or-its-selected-dependencie","errorCode":null,"errorMessage":"%s outside main module or its selected dependencies","messagePattern":"(.+?) outside main module or its selected dependencies","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/load.go","lineNumber":675,"sourceCode":"\t\tif pkg == \"builtin\" {\n\t\t\treturn \"\", errPkgIsBuiltin\n\t\t}\n\t\treturn pkg, nil\n\t}\n\n\tpkg := pathInModuleCache(ld, ctx, absDir, rs)\n\tif pkg == \"\" {\n\t\tdirstr := fmt.Sprintf(\"directory %s\", base.ShortPath(absDir))\n\t\tif dirstr == \"directory .\" {\n\t\t\tdirstr = \"current directory\"\n\t\t}\n\t\tif ld.inWorkspaceMode() {\n\t\t\tif mr := findModuleRoot(absDir); mr != \"\" {\n\t\t\t\treturn \"\", fmt.Errorf(\"%s is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using:\\n\\tgo work use %s\", dirstr, base.ShortPath(mr))\n\t\t\t}\n\t\t\treturn \"\", fmt.Errorf(\"%s outside modules listed in go.work or their selected dependencies\", dirstr)\n\t\t}\n\t\treturn \"\", fmt.Errorf(\"%s outside main module or its selected dependencies\", dirstr)\n\t}\n\treturn pkg, nil\n}\n\nvar (\n\terrDirectoryNotFound = errors.New(\"directory not found\")\n\terrPkgIsGorootSrc    = errors.New(\"GOROOT/src is not an importable package\")\n\terrPkgIsBuiltin      = errors.New(`\"builtin\" is a pseudo-package, not an importable package`)\n)\n\n// pathInModuleCache returns the import path of the directory dir,\n// if dir is in the module cache copy of a module in our build list.\nfunc pathInModuleCache(ld *Loader, ctx context.Context, dir string, rs *Requirements) string {\n\ttryMod := func(m module.Version) (string, bool) {\n\t\tif gover.IsToolchain(m.Path) {\n\t\t\treturn \"\", false\n\t\t}\n\t\tvar root string","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/load.go#L657-L693","documentation":"Thrown by resolveLocalPackage (load.go:675) in non-workspace mode when the directory has no resolvable package path — it is outside the main module, outside GOROOT/src, and not in the module cache for any selected dependency. This is the standard 'directory outside module' error for single-module builds. The 'directory .' is special-cased to read 'current directory'.","triggerScenarios":"Running 'go build' or 'go list' on a filesystem path that resolves to a directory outside the main module root and all its dependencies' cached copies. No go.work is active, so the non-workspace branch is taken.","commonSituations":"Running go commands from /tmp or a directory without a go.mod ancestor. Pointing at an absolute path unrelated to the module. Misconfigured GOMOD or working outside the module tree.","solutions":["Change directory into the main module root (where go.mod lives) before running go commands.","Run 'go mod init' if you intend to start a new module in the current directory.","Verify with 'go env GOMOD' that a go.mod is detected."],"exampleFix":"# before\ncd /tmp/random && go build ./...\n\n# after\ncd /home/me/myproject && go build ./...","handlingStrategy":"validation","validationCode":"// Verify cwd is inside a Go module before running build commands.\nfunc ensureInsideModule() error {\n    info, err := os.Stat(\"go.mod\")\n    if err != nil || info.IsDir() {\n        return fmt.Errorf(\"no go.mod found; run 'go mod init' or cd to module root\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run go commands from within a directory that has a go.mod ancestor.","Use 'go env GOMOD' to verify module detection before building.","Run 'go mod init <module-path>' when starting a new project."],"tags":["package-resolution","module-root","go-build","no-module"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}