{"record":{"id":"a8dafa05641e751d","repo":"hashicorp/terraform","slug":"error-selecting-workspace-s-a8dafa","errorCode":null,"errorMessage":"error selecting workspace: %s","messagePattern":"error selecting workspace: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/show.go","lineNumber":181,"sourceCode":"\t}\n\n\treturn plan, jsonPlan, stateFile, config, schemas, diags\n}\nfunc (c *ShowCommand) showFromLatestStateSnapshot() (*statefile.File, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\t// Load the backend\n\tb, backendDiags := c.backend(\".\", c.viewType)\n\tdiags = diags.Append(backendDiags)\n\tif backendDiags.HasErrors() {\n\t\treturn nil, diags\n\t}\n\tc.ignoreRemoteVersionConflict(b)\n\n\t// Load the workspace\n\tworkspace, err := c.Workspace()\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"error selecting workspace: %s\", err))\n\t\treturn nil, diags\n\t}\n\n\t// Get the latest state snapshot from the backend for the current workspace\n\tstateFile, stateErr := getStateFromBackend(b, workspace)\n\tif stateErr != nil {\n\t\tdiags = diags.Append(stateErr)\n\t\treturn nil, diags\n\t}\n\n\treturn stateFile, diags\n}\n\nfunc (c *ShowCommand) showFromPath(path string) (*plans.Plan, *cloudplan.RemotePlanJSON, *statefile.File, *configs.Config, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\tvar planErr, stateErr error\n\tvar plan *plans.Plan\n\tvar jsonPlan *cloudplan.RemotePlanJSON","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/show.go#L163-L199","documentation":"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.","triggerScenarios":"Running 'terraform show' with no path when the workspace file (.terraform/environment) is unreadable/missing or the backend cannot resolve the current workspace name.","commonSituations":"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.","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."],"exampleFix":"// before\n$ terraform show\nerror selecting workspace: ...\n// after\n$ terraform init\n$ terraform workspace select default\n$ terraform show","handlingStrategy":"validation","validationCode":"// Pre-flight: ensure the backend/workspace marker exists before 'terraform show' (no path)\npackage main\n\nfunc ensureWorkspaceReady(dataDir string) error {\n\tif _, err := os.Stat(filepath.Join(dataDir, \"backend\")); err != nil {\n\t\treturn fmt.Errorf(\"backend not initialized; run 'terraform init'\")\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["workspace","show","state","backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}