hashicorp/terraform · error

error selecting workspace: %s

Error message

error selecting workspace: %s

What it means

Wrapped error from ShowCommand.showFromLatestStateSnapshot when c.Workspace() fails while loading the current state snapshot for 'terraform show' (no path argument). Workspace() resolves the active workspace/environment; failure means the state to display cannot be identified. %s embeds the workspace selection error.

Source

Thrown at internal/command/show.go:181

	}

	return plan, jsonPlan, stateFile, config, schemas, diags
}
func (c *ShowCommand) showFromLatestStateSnapshot() (*statefile.File, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

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

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

	// Get the latest state snapshot from the backend for the current workspace
	stateFile, stateErr := getStateFromBackend(b, workspace)
	if stateErr != nil {
		diags = diags.Append(stateErr)
		return nil, diags
	}

	return stateFile, diags
}

func (c *ShowCommand) showFromPath(path string) (*plans.Plan, *cloudplan.RemotePlanJSON, *statefile.File, *configs.Config, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics
	var planErr, stateErr error
	var plan *plans.Plan
	var jsonPlan *cloudplan.RemotePlanJSON

View on GitHub (pinned to c9def3e214)

Solutions

  1. Run 'terraform init' to establish the backend and workspace marker.
  2. Run 'terraform workspace select <name>' to repair the environment file.
  3. Verify backend config and credentials are valid.
  4. Pass an explicit state or plan file path to 'terraform show' to bypass workspace selection.

Example fix

// before
$ terraform show
error selecting workspace: ...
// after
$ terraform init
$ terraform workspace select default
$ terraform show
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: ensure the backend/workspace marker exists before 'terraform show' (no path)
package main

func ensureWorkspaceReady(dataDir string) error {
	if _, err := os.Stat(filepath.Join(dataDir, "backend")); err != nil {
		return fmt.Errorf("backend not initialized; run 'terraform init'")
	}
	return nil
}

Prevention

When it happens

Trigger: Running 'terraform show' with no path when the workspace file (.terraform/environment) is unreadable/missing or the backend cannot resolve the current workspace name.

Common situations: Fresh checkout without 'terraform init' having created the backend/workspace marker; .terraform/environment deleted or corrupted; backend credentials issue preventing workspace enumeration; shared checkout with conflicting workspace files.

Related errors


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