hashicorp/terraform · error

error selecting workspace

Error message

error selecting workspace: %s

What it means

ShowCommand loads the backend then resolves the workspace with c.Workspace(); any error is wrapped and the show operation aborts before pulling state. This mirrors 730 but on the show code path — show needs a valid workspace to fetch the latest state snapshot from the backend.

Solutions

  1. Run terraform workspace list and select a valid workspace with terraform workspace select.
  2. Unset TF_WORKSPACE or create the named workspace with terraform workspace new.
  3. Run terraform init after backend changes.
  4. Remove a stale .terraform/environment to fall back to the default workspace.

Example fix

# before: TF_WORKSPACE=prod-missing
terraform show
# after
terraform workspace select prod
terraform show
Defensive patterns

Strategy: validation

Validate before calling

// Validate workspace before show.
ws, err := c.Workspace()
if err != nil {
    return fmt.Errorf("workspace selection failed; check TF_WORKSPACE/.terraform/environment: %w", err)
}

Prevention

When it happens

Trigger: terraform show (without a path argument, i.e. showing live state) is run; c.Workspace() returns err — TF_WORKSPACE names a missing workspace, .terraform/environment is stale, or backend workspace selection fails.

Common situations: Setting TF_WORKSPACE to an uncreated workspace; switching backend without init; stale .terraform/environment after workspace deletion; invalid characters in workspace name.

Related errors


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

Appendix: 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 d32a084675)