{"record":{"id":"22c36d62269f5dfc","repo":"golang/go","slug":"cannot-determine-module-path-for-source-directory","errorCode":null,"errorMessage":"cannot determine module path for source directory %s (%s)\n\nExample usage:\n\t'go mod init example.com/m' to initialize a v0 or v1 module\n\t'go mod init example.com/m/v2' to initialize a v2 module\n\nRun 'go help mod init' for more information.\n","messagePattern":"cannot determine module path for source directory (.+?) \\((.+?)\\)\n\nExample usage:\n\t'go mod init example\\.com/m' to initialize a v0 or v1 module\n\t'go mod init example\\.com/m/v2' to initialize a v2 module\n\nRun 'go help mod init' for more information\\.\n","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/modload/init.go","lineNumber":1849,"sourceCode":"\t\t\treturn path, nil\n\t\t}\n\t}\n\n\treason := \"outside GOPATH, module path must be specified\"\n\tif badPathErr != nil {\n\t\t// return a different error message if the module was in GOPATH, but\n\t\t// the module path determined above would be an invalid path.\n\t\treason = fmt.Sprintf(\"bad module path inferred from directory in GOPATH: %v\", badPathErr)\n\t}\n\tmsg := `cannot determine module path for source directory %s (%s)\n\nExample usage:\n\t'go mod init example.com/m' to initialize a v0 or v1 module\n\t'go mod init example.com/m/v2' to initialize a v2 module\n\nRun 'go help mod init' for more information.\n`\n\treturn \"\", fmt.Errorf(msg, dir, reason)\n}\n\nvar importCommentRE = lazyregexp.New(`(?m)^package[ \\t]+[^ \\t\\r\\n/]+[ \\t]+//[ \\t]+import[ \\t]+(\\\"[^\"]+\\\")[ \\t]*\\r?\\n`)\n\nfunc findImportComment(file string) string {\n\tdata, err := os.ReadFile(file)\n\tif err != nil {\n\t\treturn \"\"\n\t}\n\tm := importCommentRE.FindSubmatch(data)\n\tif m == nil {\n\t\treturn \"\"\n\t}\n\tpath, err := strconv.Unquote(string(m[1]))\n\tif err != nil {\n\t\treturn \"\"\n\t}\n\treturn path","sourceCodeStart":1831,"sourceCodeEnd":1867,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/modload/init.go#L1831-L1867","documentation":"'go mod init' was run without a module path argument AND Go could not infer one from GOPATH, VCS metadata, or any other heuristic. The error includes the directory and the reason inference failed, plus example usage. This is a user-facing fatal (base.Fatalf-style message flow).","triggerScenarios":"Running 'go mod init' (no args) in a directory that is outside GOPATH, has no VCS remote, and where no heuristic (import comment, etc.) yields a path. The function returns the formatted multi-line error.","commonSituations":"Running go mod init inside an arbitrary new project folder with no git remote and GOPATH unset/modules default; inside a Docker scratch dir; the inferred GOPATH path was itself invalid as a module path.","solutions":["Pass an explicit path: 'go mod init example.com/yourorg/yourproject'.","Add a VCS 'origin' remote (git remote add origin <url>) so inference can derive the path, then re-run 'go mod init'.","Move the project under GOPATH/src with a valid domain-prefixed layout if you rely on inference.","Pick a stable, owned domain prefix (example.com, github.com/user) rather than a guessable placeholder."],"exampleFix":"// before\n$ cd /tmp/myproj && go mod init\n// cannot determine module path for source directory /tmp/myproj (outside GOPATH/modules, no VCS metadata)\n\n// after\n$ go mod init github.com/youruser/myproj","handlingStrategy":"validation","validationCode":"// Decide module path explicitly before 'go mod init'.\nfunc resolveModulePath(dir string) (string, error) {\n    // prefer explicit arg; fall back to VCS remote; else fail fast.\n    if root, err := vcs.VCSCommand(dir, \"remote\"); err == nil && len(root) > 0 {\n        return strings.TrimPrefix(root[0], \"https://\"), nil\n    }\n    return \"\", errors.New(\"cannot infer module path; pass one explicitly\")\n}","typeGuard":null,"tryCatchPattern":"out, err := exec.Command(\"go\", \"mod\", \"init\").CombinedOutput()\nif err != nil && bytes.Contains(out, []byte(\"cannot determine module path\")) {\n    // prompt the user / use a default derived from the VCS remote\n    path := derivePathFromGitRemote() // your helper\n    out, err = exec.Command(\"go\", \"mod\", \"init\", path).CombinedOutput()\n}\nreturn err","preventionTips":["Always pass an explicit path to 'go mod init example.com/...'.","Add a git 'origin' remote so inference can work.","Scaffold new projects with a template that sets the module path.","Avoid placeholders like 'myproject'; use an owned domain."],"tags":["go-mod-init","module-path","inference","gopath","vcs"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}