{"record":{"id":"c5b373ddf6ec160a","repo":"golang/go","slug":"s-outside-modules-listed-in-go-work-or-their-sele","errorCode":null,"errorMessage":"%s outside modules listed in go.work or their selected dependencies","messagePattern":"(.+?) outside modules listed in go\\.work or their selected dependencies","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/load.go","lineNumber":673,"sourceCode":"\tif sub := search.InDir(absDir, cfg.GOROOTsrc); sub != \"\" && sub != \".\" && !strings.Contains(sub, \"@\") {\n\t\tpkg := filepath.ToSlash(sub)\n\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","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/load.go#L655-L691","documentation":"Thrown by resolveLocalPackage (load.go:673) in workspace mode when the directory has no resolvable package path AND findModuleRoot(absDir) returns empty — meaning the directory is not inside any module at all, not just an unlisted one. The scope is 'modules listed in go.work or their selected dependencies'. This is the workspace-mode equivalent of being completely outside all modules.","triggerScenarios":"In workspace mode (go.work active), running a go command on a directory that has no go.mod above it and is not in the module cache or GOROOT. findModuleRoot returns \"\", so the 'go work use' suggestion is skipped and this broader 'outside' error is emitted.","commonSituations":"Running go commands from /tmp, a scratch directory, or a path with no module context while a go.work is active elsewhere. The GOWORK env var points at a workspace but the cwd is unrelated.","solutions":["Move or create your code inside one of the modules listed in go.work.","If you have a new module, initialize it ('go mod init') and add it to the workspace ('go work use').","If the go.work is stale or unintended, unset GOWORK (GOWORK=off) to operate in single-module mode."],"exampleFix":"# before — outside all workspace modules\ncd /scratch && GOWORK=/home/me/project/go.work go build ./...\n\n# after — work inside a listed module, or disable workspace:\nGOWORK=off go build ./...\n# or: cd /home/me/project/moduleA && go build ./...","handlingStrategy":"validation","validationCode":"// Verify cwd is inside some workspace module before running commands.\nfunc inWorkspaceModule(absDir string) bool {\n    for _, mod := range ld.MainModules.Versions() {\n        root := ld.MainModules.ModRoot(mod)\n        if root != \"\" && str.HasFilePathPrefix(absDir, root) {\n            return true\n        }\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure your working directory is inside a module listed in go.work.","If the workspace is irrelevant, disable it with GOWORK=off.","Use 'go work use' to add new modules to the workspace before building them."],"tags":["workspace","go-work","package-resolution","no-module"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}