{"record":{"id":"9c735f67ba8d52ff","repo":"golang/go","slug":"s-is-contained-in-a-module-that-is-not-one-of-the","errorCode":null,"errorMessage":"%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","messagePattern":"(.+?) 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 (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/load.go","lineNumber":671,"sourceCode":"\t}\n\n\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) {","sourceCodeStart":653,"sourceCodeEnd":689,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/load.go#L653-L689","documentation":"Thrown by resolveLocalPackage (load.go:671) in workspace mode when the directory is not resolvable in any listed module, but findModuleRoot(absDir) finds a containing go.mod — meaning the directory belongs to a module that is NOT listed in go.work. The error includes a suggestion to add it via 'go work use'. This is distinct from the case where no module root is found at all.","triggerScenarios":"Operating in workspace mode (a go.work file is active) and running go commands in a directory that has its own go.mod but that module was not added to the workspace. findModuleRoot returns a non-empty mr, triggering the 'add to workspace' suggestion.","commonSituations":"Creating a new module in a subdirectory of a workspace but forgetting to run 'go work use ./newmodule'. Cloning a dependency repo alongside the workspace and trying to build it directly. Multi-repo setups where not all modules are registered in go.work.","solutions":["Add the module to the workspace: run 'go work use <path>' with the path suggested in the error message.","Alternatively, leave workspace mode by unsetting GOWORK or running from the specific module's root.","Verify the workspace is correct with 'go work edit -print' after adding."],"exampleFix":"# before — module exists but not in go.work\ncd /workspace/newmodule && go build ./...\n# error: contained in a module not listed in go.work\n\n# after\ngo work use /workspace/newmodule\ngo build ./...","handlingStrategy":"validation","validationCode":"// Verify a directory's module is listed in the active go.work.\nfunc moduleInWorkspace(dir string) bool {\n    mr := findModuleRoot(dir)\n    for _, root := range modRoots {\n        if root == mr {\n            return true\n        }\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["After creating a new module, immediately run 'go work use <path>' to register it.","Periodically review 'go work edit -print' to ensure all relevant modules are listed.","Use 'go work sync' to keep workspace requirements consistent."],"tags":["workspace","go-work","package-resolution","module-root"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}