{"record":{"id":"4dab76f87adddee3","repo":"hashicorp/terraform","slug":"error-selecting-workspace-s","errorCode":null,"errorMessage":"Error selecting workspace: %s","messagePattern":"Error selecting workspace: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/init_run.go","lineNumber":316,"sourceCode":"\t\t\treturn 1\n\t\t}\n\t}\n\tproviderHook := &providerPolicyHook{\n\t\tclient:     policyClient,\n\t\tview:       view,\n\t\trootModule: rootModEarly,\n\t}\n\n\tvar state *states.State\n\n\t// If we have a functional backend (either just initialized or initialized\n\t// on a previous run) we'll use the current state as a potential source\n\t// of provider dependencies.\n\tif back != nil {\n\t\tc.ignoreRemoteVersionConflict(back)\n\t\tworkspace, err := c.Workspace()\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error selecting workspace: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tsMgr, sDiags := back.StateMgr(workspace)\n\t\tif sDiags.HasErrors() {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error loading state: %s\", sDiags.Err()))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tif err := sMgr.RefreshState(); err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error refreshing state: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tstate = sMgr.State()\n\t}","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/init_run.go#L298-L334","documentation":"Emitted by InitCommand.run when c.Workspace() fails after a backend is available (init_run.go:314-316). Workspace() resolves the active workspace name (default, or via TF_WORKSPACE / backend config / terraform workspace). The %s carries the underlying error. Init aborts before touching state.","triggerScenarios":"A backend is initialized but the workspace name cannot be resolved: TF_WORKSPACE set to a name that doesn't exist in a multi-workspace backend, the configured workspace environment var is invalid, the local backend's workspace state is corrupt, or the backend returns an error computing workspaces.","commonSituations":"Setting TF_WORKSPACE to a non-existent workspace for a backend that does not auto-create workspaces; misconfigured backend workspace listing; running init with TF_WORKSPACE exported incorrectly in CI; corrupted .terraform/terraform.tfstate workspace marker.","solutions":["Unset TF_WORKSPACE or set it to an existing workspace: `unset TF_WORKSPACE` then `terraform workspace select <name>`.","Create the workspace if the backend requires it: `terraform workspace new <name>`.","Reset the local backend state marker if corrupted: back up and remove `.terraform/terraform.tfstate`.","Inspect the wrapped error for backend-specific reasons (e.g. S3/consul listing failure) and fix credentials/connectivity."],"exampleFix":"# before\n$ TF_WORKSPACE=prod terraform init   # 'prod' does not exist\n# after\n$ unset TF_WORKSPACE\n$ terraform workspace new prod\n$ terraform init","handlingStrategy":"try-catch","validationCode":"// If TF_WORKSPACE is set, validate the workspace exists for the backend.\nif ws := os.Getenv(\"TF_WORKSPACE\"); ws != \"\" {\n    if err := run(\"terraform\", \"workspace\", \"select\", ws); err != nil {\n        return fmt.Errorf(\"workspace %q not selectable: %w\", ws, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"// Fall back to the default workspace if a custom one cannot be selected.\nif err := run(\"terraform\", \"init\"); err != nil {\n    os.Unsetenv(\"TF_WORKSPACE\")\n    err = run(\"terraform\", \"init\")\n}","preventionTips":["Do not set TF_WORKSPACE to a non-existent workspace for backends that don't auto-create.","Create workspaces explicitly with `terraform workspace new` before referencing them.","Reset a corrupted .terraform/terraform.tfstate marker when workspace resolution misbehaves."],"tags":["terraform-init","workspace","backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}