hashicorp/terraform · error

Error selecting workspace: %s

Error message

Error selecting workspace: %s

What it means

Wrapped error from OutputCommand.runStateReadingCommand when c.Workspace() fails. Workspace() resolves the active Terraform workspace (named state environment) from the local .terraform/environment file or backend state. Failure means the workspace name could not be read or selected, so 'terraform output' cannot determine which state to load.

Source

Thrown at internal/command/output.go:81

	}

	// Load the backend
	b, backendDiags := c.backend(".", view)
	diags = diags.Append(backendDiags)
	if backendDiags.HasErrors() {
		return nil, diags
	}

	// Command can be aborted by interruption signals
	ctx, done := c.InterruptibleContext(c.CommandContext())
	defer done()

	// This is a read-only command
	c.ignoreRemoteVersionConflict(b)

	env, err := c.Workspace()
	if err != nil {
		diags = diags.Append(fmt.Errorf("Error selecting workspace: %s", err))
		return nil, diags
	}

	// Get the state
	sMgr, sDiags := b.StateMgr(env)
	if sDiags.HasErrors() {
		diags = diags.Append(fmt.Errorf("Failed to load state: %s", sDiags.Err()))
		return nil, diags
	}

	output, err := sMgr.GetRootOutputValues(ctx)
	if err != nil {
		return nil, diags.Append(err)
	}

	return output, diags
}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Run 'terraform init' to (re)establish the backend and default workspace marker.
  2. Run 'terraform workspace list' / 'terraform workspace select <name>' to repair the environment file.
  3. Verify backend credentials and config are intact so workspace selection succeeds.
  4. If using -state flag directly, ensure the path is valid and skip workspace selection.

Example fix

// before
$ terraform output
Error selecting workspace: ...
// after
$ terraform init
$ terraform workspace select default
$ terraform output
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: ensure a workspace is selected before 'terraform output'
package main

func ensureWorkspaceSelected(dataDir string) error {
	envFile := filepath.Join(dataDir, "environment")
	if _, err := os.Stat(filepath.Join(dataDir, "backend")); err != nil {
		// no backend marker -> init not run
		return fmt.Errorf("backend not initialized; run 'terraform init'")
	}
	_ = envFile // absence is fine (defaults to default workspace)
	return nil
}

Prevention

When it happens

Trigger: Running 'terraform output' when the workspace file (.terraform/environment) is unreadable, the backend reports no default workspace, or workspace selection logic errors (e.g. a local backend missing the environment marker file).

Common situations: Switched workspaces then deleted .terraform/environment; running in a fresh checkout before 'terraform init' established a backend; backend workspace listing failed due to credentials; multiple developers hitting a shared local checkout with conflicting workspace files.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/daa780486f79b47c. Report an issue: GitHub.