golang/go · error

%s outside modules listed in go.work or their selected depen

Error message

%s outside modules listed in go.work or their selected dependencies

What it means

Thrown by resolveLocalPackage (load.go:673) in workspace mode when the directory has no resolvable package path AND findModuleRoot(absDir) returns empty — meaning the directory is not inside any module at all, not just an unlisted one. The scope is 'modules listed in go.work or their selected dependencies'. This is the workspace-mode equivalent of being completely outside all modules.

Source

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

	if sub := search.InDir(absDir, cfg.GOROOTsrc); sub != "" && sub != "." && !strings.Contains(sub, "@") {
		pkg := filepath.ToSlash(sub)
		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

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Move or create your code inside one of the modules listed in go.work.
  2. If you have a new module, initialize it ('go mod init') and add it to the workspace ('go work use').
  3. If the go.work is stale or unintended, unset GOWORK (GOWORK=off) to operate in single-module mode.

Example fix

# before — outside all workspace modules
cd /scratch && GOWORK=/home/me/project/go.work go build ./...

# after — work inside a listed module, or disable workspace:
GOWORK=off go build ./...
# or: cd /home/me/project/moduleA && go build ./...
Defensive patterns

Strategy: validation

Validate before calling

// Verify cwd is inside some workspace module before running commands.
func inWorkspaceModule(absDir string) bool {
    for _, mod := range ld.MainModules.Versions() {
        root := ld.MainModules.ModRoot(mod)
        if root != "" && str.HasFilePathPrefix(absDir, root) {
            return true
        }
    }
    return false
}

Prevention

When it happens

Trigger: In workspace mode (go.work active), running a go command on a directory that has no go.mod above it and is not in the module cache or GOROOT. findModuleRoot returns "", so the 'go work use' suggestion is skipped and this broader 'outside' error is emitted.

Common situations: Running go commands from /tmp, a scratch directory, or a path with no module context while a go.work is active elsewhere. The GOWORK env var points at a workspace but the cwd is unrelated.

Related errors


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