{"record":{"id":"daa780486f79b47c","repo":"hashicorp/terraform","slug":"error-selecting-workspace-s-daa780","errorCode":null,"errorMessage":"Error selecting workspace: %s","messagePattern":"Error selecting workspace: (.+?)","errorType":"console","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/output.go#L63-L99","documentation":"Wrapped error from OutputCommand.runStateReadingCommand when c.Workspace() fails. Workspace() resolves the active Terraform workspace (named state environment) from the local .terraform/environment file or backend state. Failure means the workspace name could not be read or selected, so 'terraform output' cannot determine which state to load.","triggerScenarios":"Running 'terraform output' when the workspace file (.terraform/environment) is unreadable, the backend reports no default workspace, or workspace selection logic errors (e.g. a local backend missing the environment marker file).","commonSituations":"Switched workspaces then deleted .terraform/environment; running in a fresh checkout before 'terraform init' established a backend; backend workspace listing failed due to credentials; multiple developers hitting a shared local checkout with conflicting workspace files.","solutions":["Run 'terraform init' to (re)establish the backend and default workspace marker.","Run 'terraform workspace list' / 'terraform workspace select <name>' to repair the environment file.","Verify backend credentials and config are intact so workspace selection succeeds.","If using -state flag directly, ensure the path is valid and skip workspace selection."],"exampleFix":"// before\n$ terraform output\nError selecting workspace: ...\n// after\n$ terraform init\n$ terraform workspace select default\n$ terraform output","handlingStrategy":"validation","validationCode":"// Pre-flight: ensure a workspace is selected before 'terraform output'\npackage main\n\nfunc ensureWorkspaceSelected(dataDir string) error {\n\tenvFile := filepath.Join(dataDir, \"environment\")\n\tif _, err := os.Stat(filepath.Join(dataDir, \"backend\")); err != nil {\n\t\t// no backend marker -> init not run\n\t\treturn fmt.Errorf(\"backend not initialized; run 'terraform init'\")\n\t}\n\t_ = envFile // absence is fine (defaults to default workspace)\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run 'terraform init' after cloning to establish the backend/workspace marker.","Use 'terraform workspace select <name>' explicitly in CI scripts.","Avoid deleting .terraform/environment by hand."],"tags":["workspace","state","backend","output"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}