{"record":{"id":"cf928b102653a371","repo":"golang/go","slug":"s-s-is-not-within-module-s-rooted-at-s","errorCode":null,"errorMessage":"%s%s is not within module%s rooted at %s","messagePattern":"(.+?)(.+?) is not within module(.+?) rooted at (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modget/get.go","lineNumber":803,"sourceCode":"\t\t\t\t\tabsDetail = fmt.Sprintf(\" (%s)\", absPath)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Absolute paths like C:\\foo and relative paths like ../foo... are\n\t\t\t// restricted to matching packages in the main module.\n\t\t\tpkgPattern, mainModule := ld.MainModules.DirImportPath(ld, ctx, q.pattern)\n\t\t\tif pkgPattern == \".\" {\n\t\t\t\tld.MustHaveModRoot()\n\t\t\t\tversions := ld.MainModules.Versions()\n\t\t\t\tmodRoots := make([]string, 0, len(versions))\n\t\t\t\tfor _, m := range versions {\n\t\t\t\t\tmodRoots = append(modRoots, ld.MainModules.ModRoot(m))\n\t\t\t\t}\n\t\t\t\tvar plural string\n\t\t\t\tif len(modRoots) != 1 {\n\t\t\t\t\tplural = \"s\"\n\t\t\t\t}\n\t\t\t\treturn errSet(fmt.Errorf(\"%s%s is not within module%s rooted at %s\", q.pattern, absDetail, plural, strings.Join(modRoots, \", \")))\n\t\t\t}\n\n\t\t\tmatch := modload.MatchInModule(ld, ctx, pkgPattern, mainModule, imports.AnyTags())\n\t\t\tif len(match.Errs) > 0 {\n\t\t\t\treturn pathSet{err: match.Errs[0]}\n\t\t\t}\n\n\t\t\tif len(match.Pkgs) == 0 {\n\t\t\t\tif q.raw == \"\" || q.raw == \".\" {\n\t\t\t\t\treturn errSet(fmt.Errorf(\"no package to get in current directory\"))\n\t\t\t\t}\n\t\t\t\tif !q.isWildcard() {\n\t\t\t\t\tld.MustHaveModRoot()\n\t\t\t\t\treturn errSet(fmt.Errorf(\"%s%s is not a package in module rooted at %s\", q.pattern, absDetail, ld.MainModules.ModRoot(mainModule)))\n\t\t\t\t}\n\t\t\t\tsearch.WarnUnmatched([]*search.Match{match})\n\t\t\t\treturn pathSet{}\n\t\t\t}","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modget/get.go#L785-L821","documentation":"This error is produced when running 'go get' with a local path query ('.', './...', '../pkg') and the resolved path is not within any module root. performLocalQueries calls MainModules.DirImportPath which returns pkgPattern='.' when the path falls outside all module roots. The error lists all module root directories so the user can see where modules are actually rooted.","triggerScenarios":"Running 'go get .' or 'go get ./subdir' from a directory that is not inside any module's root directory. This happens when there's no go.mod in the current directory or any parent directory, or when the path navigates outside the module root.","commonSituations":"Running 'go get .' from a directory with no go.mod (no module initialized). Running 'go get ../sibling' where the sibling directory is outside the module root. A monorepo where the module root is at /repo but the user is in /other/path. The GOPATH mode is being used but the directory isn't under GOPATH/src.","solutions":["Initialize a module: run 'go mod init <module-path>' in the directory where you want the module root.","Navigate to a directory inside the module root before running 'go get .'.","Verify you're inside a module: check that 'go list -m' succeeds and shows your module.","If working with an existing project, clone it properly so the go.mod is in a parent directory of your working path."],"exampleFix":"# before\n$ cd /tmp/some-dir\n$ go get .\n# . (/tmp/some-dir) is not within module rooted at ...\n\n# after: initialize module first\n$ cd /tmp/myproject\n$ go mod init example.com/myproject\n$ go get .","handlingStrategy":"validation","validationCode":"// Check if current directory is inside a Go module before go get .\nfunc validateInModule() error {\n    dir, err := os.Getwd()\n    if err != nil { return err }\n    for d := dir; d != \"/\" && d != \".\"; d = filepath.Dir(d) {\n        if _, err := os.Stat(filepath.Join(d, \"go.mod\")); err == nil {\n            return nil // found go.mod\n        }\n    }\n    return fmt.Errorf(\"no go.mod found in current or parent directories; run: go mod init\")\n}","typeGuard":null,"tryCatchPattern":"if strings.Contains(stderr, \"is not within module\") {\n    // Not inside a module; suggest initialization\n    // exec.Command(\"go\", \"mod\", \"init\", \"example.com/myproject\")\n}","preventionTips":["Run 'go mod init' before using 'go get .' in a new directory","Verify module root with 'go list -m' before local path queries","Use 'go env GOMOD' to check if you're inside a module (returns path to go.mod or empty)","Clone repositories to the correct directory structure matching their module paths"],"tags":["go-get","go-modules","local-path","module-root"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}