golang/go · error

cannot determine module path for source directory %s (%s) E

Error message

cannot determine module path for source directory %s (%s)

Example usage:
	'go mod init example.com/m' to initialize a v0 or v1 module
	'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.

What it means

'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).

Source

Thrown at src/cmd/go/internal/modload/init.go:1849

			return path, nil
		}
	}

	reason := "outside GOPATH, module path must be specified"
	if badPathErr != nil {
		// return a different error message if the module was in GOPATH, but
		// the module path determined above would be an invalid path.
		reason = fmt.Sprintf("bad module path inferred from directory in GOPATH: %v", badPathErr)
	}
	msg := `cannot determine module path for source directory %s (%s)

Example usage:
	'go mod init example.com/m' to initialize a v0 or v1 module
	'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.
`
	return "", fmt.Errorf(msg, dir, reason)
}

var importCommentRE = lazyregexp.New(`(?m)^package[ \t]+[^ \t\r\n/]+[ \t]+//[ \t]+import[ \t]+(\"[^"]+\")[ \t]*\r?\n`)

func findImportComment(file string) string {
	data, err := os.ReadFile(file)
	if err != nil {
		return ""
	}
	m := importCommentRE.FindSubmatch(data)
	if m == nil {
		return ""
	}
	path, err := strconv.Unquote(string(m[1]))
	if err != nil {
		return ""
	}
	return path

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Pass an explicit path: 'go mod init example.com/yourorg/yourproject'.
  2. Add a VCS 'origin' remote (git remote add origin <url>) so inference can derive the path, then re-run 'go mod init'.
  3. Move the project under GOPATH/src with a valid domain-prefixed layout if you rely on inference.
  4. Pick a stable, owned domain prefix (example.com, github.com/user) rather than a guessable placeholder.

Example fix

// before
$ cd /tmp/myproj && go mod init
// cannot determine module path for source directory /tmp/myproj (outside GOPATH/modules, no VCS metadata)

// after
$ go mod init github.com/youruser/myproj
Defensive patterns

Strategy: validation

Validate before calling

// Decide module path explicitly before 'go mod init'.
func resolveModulePath(dir string) (string, error) {
    // prefer explicit arg; fall back to VCS remote; else fail fast.
    if root, err := vcs.VCSCommand(dir, "remote"); err == nil && len(root) > 0 {
        return strings.TrimPrefix(root[0], "https://"), nil
    }
    return "", errors.New("cannot infer module path; pass one explicitly")
}

Try / catch

out, err := exec.Command("go", "mod", "init").CombinedOutput()
if err != nil && bytes.Contains(out, []byte("cannot determine module path")) {
    // prompt the user / use a default derived from the VCS remote
    path := derivePathFromGitRemote() // your helper
    out, err = exec.Command("go", "mod", "init", path).CombinedOutput()
}
return err

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/22c36d62269f5dfc. Report an issue: GitHub.