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.RemotePlanJSONView on GitHub (pinned to c9def3e214)
Solutions
- Run 'terraform init' to establish the backend and workspace marker.
- Run 'terraform workspace select <name>' to repair the environment file.
- Verify backend config and credentials are valid.
- 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
- Run 'terraform init' before 'terraform show' in fresh checkouts.
- Use 'terraform workspace select' to keep the environment marker valid.
- Pass an explicit state/plan file path to bypass workspace selection.
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
- error loading state: %w
- Error selecting workspace: %s
- Failed to load state: %s
- Failed to load state manager: %w
- Failed to load state: %w
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/a8dafa05641e751d.
Report an issue: GitHub.