golang/go · error

%s%s is not within module%s rooted at %s

Error message

%s%s is not within module%s rooted at %s

What it means

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.

Source

Thrown at src/cmd/go/internal/modget/get.go:803

					absDetail = fmt.Sprintf(" (%s)", absPath)
				}
			}

			// Absolute paths like C:\foo and relative paths like ../foo... are
			// restricted to matching packages in the main module.
			pkgPattern, mainModule := ld.MainModules.DirImportPath(ld, ctx, q.pattern)
			if pkgPattern == "." {
				ld.MustHaveModRoot()
				versions := ld.MainModules.Versions()
				modRoots := make([]string, 0, len(versions))
				for _, m := range versions {
					modRoots = append(modRoots, ld.MainModules.ModRoot(m))
				}
				var plural string
				if len(modRoots) != 1 {
					plural = "s"
				}
				return errSet(fmt.Errorf("%s%s is not within module%s rooted at %s", q.pattern, absDetail, plural, strings.Join(modRoots, ", ")))
			}

			match := modload.MatchInModule(ld, ctx, pkgPattern, mainModule, imports.AnyTags())
			if len(match.Errs) > 0 {
				return pathSet{err: match.Errs[0]}
			}

			if len(match.Pkgs) == 0 {
				if q.raw == "" || q.raw == "." {
					return errSet(fmt.Errorf("no package to get in current directory"))
				}
				if !q.isWildcard() {
					ld.MustHaveModRoot()
					return errSet(fmt.Errorf("%s%s is not a package in module rooted at %s", q.pattern, absDetail, ld.MainModules.ModRoot(mainModule)))
				}
				search.WarnUnmatched([]*search.Match{match})
				return pathSet{}
			}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Initialize a module: run 'go mod init <module-path>' in the directory where you want the module root.
  2. Navigate to a directory inside the module root before running 'go get .'.
  3. Verify you're inside a module: check that 'go list -m' succeeds and shows your module.
  4. If working with an existing project, clone it properly so the go.mod is in a parent directory of your working path.

Example fix

# before
$ cd /tmp/some-dir
$ go get .
# . (/tmp/some-dir) is not within module rooted at ...

# after: initialize module first
$ cd /tmp/myproject
$ go mod init example.com/myproject
$ go get .
Defensive patterns

Strategy: validation

Validate before calling

// Check if current directory is inside a Go module before go get .
func validateInModule() error {
    dir, err := os.Getwd()
    if err != nil { return err }
    for d := dir; d != "/" && d != "."; d = filepath.Dir(d) {
        if _, err := os.Stat(filepath.Join(d, "go.mod")); err == nil {
            return nil // found go.mod
        }
    }
    return fmt.Errorf("no go.mod found in current or parent directories; run: go mod init")
}

Try / catch

if strings.Contains(stderr, "is not within module") {
    // Not inside a module; suggest initialization
    // exec.Command("go", "mod", "init", "example.com/myproject")
}

Prevention

When it happens

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

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

Related errors


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