{"record":{"id":"1c6c98ba53f2cffa","repo":"golang/go","slug":"goroot-src-is-not-an-importable-package","errorCode":null,"errorMessage":"GOROOT/src is not an importable package","messagePattern":"GOROOT/src is not an importable package","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/load.go","lineNumber":682,"sourceCode":"\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\n\t\tvar err error\n\t\tif repl := Replacement(ld, m); repl.Path != \"\" && repl.Version == \"\" {\n\t\t\troot = repl.Path\n\t\t\tif !filepath.IsAbs(root) {\n\t\t\t\troot = filepath.Join(replaceRelativeTo(ld), root)\n\t\t\t}\n\t\t} else if repl.Path != \"\" {","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/load.go#L664-L700","documentation":"`$GOROOT/src` is the directory that contains the standard library, but it is not itself an importable Go package. The loader returns this sentinel (errPkgIsGorootSrc) when something asks it to load that exact directory as a package.","triggerScenarios":"Importing or building the literal path `GOROOT/src` (or passing that directory to go list/build), typically via tooling that walks GOROOT and treats each directory as a package.","commonSituations":"Static-analysis tools, doc generators, or scripts that recurse over GOROOT and fail to skip the root src directory.","solutions":["Target a concrete standard-library package path (e.g. fmt, net/http) instead of GOROOT/src.","In GOROOT-walking tools, skip the bare src directory and only process its children."],"exampleFix":"// before\npkg, _ := importer.Default().Import(\"src\")\n\n// after\npkg, _ := importer.Default().Import(\"fmt\")","handlingStrategy":"validation","validationCode":"// Skip the bare GOROOT/src directory when walking GOROOT.\nfunc skipGorootSrc(dir, goroot string) bool {\n    return filepath.Clean(dir) == filepath.Join(goroot, \"src\")\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Never import the literal path `src` or `GOROOT/src`.","In GOROOT-walking tools, process only child directories of src."],"tags":["goroot","stdlib","import","loader"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}