{"record":{"id":"daa780486f79b47c","repo":"hashicorp/terraform","slug":"error-selecting-workspace-s-daa780","errorCode":null,"errorMessage":"Error selecting workspace: %s","messagePattern":"Error selecting workspace: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/output.go","lineNumber":81,"sourceCode":"\t}\n\n\t// Load the backend\n\tb, backendDiags := c.backend(\".\", view)\n\tdiags = diags.Append(backendDiags)\n\tif backendDiags.HasErrors() {\n\t\treturn nil, diags\n\t}\n\n\t// Command can be aborted by interruption signals\n\tctx, done := c.InterruptibleContext(c.CommandContext())\n\tdefer done()\n\n\t// This is a read-only command\n\tc.ignoreRemoteVersionConflict(b)\n\n\tenv, 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 state\n\tsMgr, sDiags := b.StateMgr(env)\n\tif sDiags.HasErrors() {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to load state: %s\", sDiags.Err()))\n\t\treturn nil, diags\n\t}\n\n\toutput, err := sMgr.GetRootOutputValues(ctx)\n\tif err != nil {\n\t\treturn nil, diags.Append(err)\n\t}\n\n\treturn output, diags\n}\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/output.go#L63-L99","documentation":"OutputCommand.run retrieves the current workspace via c.Workspace(); any error (workspace file unreadable, workspace does not exist, backend workspace selection failure) is wrapped and the command aborts before loading state. The output command is read-only but still requires a resolvable workspace.","triggerScenarios":"terraform output is run; c.Workspace() returns err — TF_WORKSPACE points at a non-existent workspace, the .terraform/environment file is corrupt, or the backend rejects the workspace name.","commonSituations":"Setting TF_WORKSPACE to a workspace that was never created; running output right after switching backends without terraform init; CI using a workspace name with invalid characters; .terraform/environment left pointing at a deleted workspace.","solutions":["Run terraform workspace list to see valid workspaces and switch with terraform workspace select <name>.","Unset TF_WORKSPACE if it refers to a non-existent workspace, or create it with terraform workspace new.","Re-run terraform init after a backend change to refresh workspace state.","Delete a stale .terraform/environment file so the default workspace is used."],"exampleFix":"# before: TF_WORKSPACE=staging but workspace missing\nterraform output\n# after\nterraform workspace new staging   # or select existing\nterraform output","handlingStrategy":"validation","validationCode":"// Validate the workspace exists before running output.\nws, _ := c.Workspace()\nif _, err := backend.Workspaces(); err != nil || !workspaceExists(ws) {\n    return fmt.Errorf(\"workspace %q invalid; run 'terraform workspace list'\", ws)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not point TF_WORKSPACE at uncreated workspaces.","Run terraform init after backend changes.","Delete stale .terraform/environment when switching contexts."],"tags":["terraform","workspace","output","cli","state"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}