golang/go · error

%s outside main module or its selected dependencies

Error message

%s outside main module or its selected dependencies

What it means

Thrown by resolveLocalPackage (load.go:675) in non-workspace mode when the directory has no resolvable package path — it is outside the main module, outside GOROOT/src, and not in the module cache for any selected dependency. This is the standard 'directory outside module' error for single-module builds. The 'directory .' is special-cased to read 'current directory'.

Source

Thrown at src/cmd/go/internal/modload/load.go:675

		if pkg == "builtin" {
			return "", errPkgIsBuiltin
		}
		return pkg, nil
	}

	pkg := pathInModuleCache(ld, ctx, absDir, rs)
	if pkg == "" {
		dirstr := fmt.Sprintf("directory %s", base.ShortPath(absDir))
		if dirstr == "directory ." {
			dirstr = "current directory"
		}
		if ld.inWorkspaceMode() {
			if mr := findModuleRoot(absDir); mr != "" {
				return "", 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))
			}
			return "", fmt.Errorf("%s outside modules listed in go.work or their selected dependencies", dirstr)
		}
		return "", fmt.Errorf("%s outside main module or its selected dependencies", dirstr)
	}
	return pkg, nil
}

var (
	errDirectoryNotFound = errors.New("directory not found")
	errPkgIsGorootSrc    = errors.New("GOROOT/src is not an importable package")
	errPkgIsBuiltin      = errors.New(`"builtin" is a pseudo-package, not an importable package`)
)

// pathInModuleCache returns the import path of the directory dir,
// if dir is in the module cache copy of a module in our build list.
func pathInModuleCache(ld *Loader, ctx context.Context, dir string, rs *Requirements) string {
	tryMod := func(m module.Version) (string, bool) {
		if gover.IsToolchain(m.Path) {
			return "", false
		}
		var root string

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Change directory into the main module root (where go.mod lives) before running go commands.
  2. Run 'go mod init' if you intend to start a new module in the current directory.
  3. Verify with 'go env GOMOD' that a go.mod is detected.

Example fix

# before
cd /tmp/random && go build ./...

# after
cd /home/me/myproject && go build ./...
Defensive patterns

Strategy: validation

Validate before calling

// Verify cwd is inside a Go module before running build commands.
func ensureInsideModule() error {
    info, err := os.Stat("go.mod")
    if err != nil || info.IsDir() {
        return fmt.Errorf("no go.mod found; run 'go mod init' or cd to module root")
    }
    return nil
}

Prevention

When it happens

Trigger: Running 'go build' or 'go list' on a filesystem path that resolves to a directory outside the main module root and all its dependencies' cached copies. No go.work is active, so the non-workspace branch is taken.

Common situations: Running go commands from /tmp or a directory without a go.mod ancestor. Pointing at an absolute path unrelated to the module. Misconfigured GOMOD or working outside the module tree.

Related errors


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