{"record":{"id":"a8dafa05641e751d","repo":"hashicorp/terraform","slug":"error-selecting-workspace-s-a8dafa","errorCode":null,"errorMessage":"error selecting workspace: %s","messagePattern":"error selecting workspace: (.+?)","errorType":"exception","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/show.go#L163-L199","documentation":"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.","triggerScenarios":"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.","commonSituations":"Setting TF_WORKSPACE to an uncreated workspace; switching backend without init; stale .terraform/environment after workspace deletion; invalid characters in workspace name.","solutions":["Run terraform workspace list and select a valid workspace with terraform workspace select.","Unset TF_WORKSPACE or create the named workspace with terraform workspace new.","Run terraform init after backend changes.","Remove a stale .terraform/environment to fall back to the default workspace."],"exampleFix":"# before: TF_WORKSPACE=prod-missing\nterraform show\n# after\nterraform workspace select prod\nterraform show","handlingStrategy":"validation","validationCode":"// Validate workspace before show.\nws, err := c.Workspace()\nif err != nil {\n    return fmt.Errorf(\"workspace selection failed; check TF_WORKSPACE/.terraform/environment: %w\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure TF_WORKSPACE refers to an existing workspace.","Run terraform init after backend changes.","Clear stale .terraform/environment files."],"tags":["terraform","workspace","show","cli","state"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}