{"record":{"id":"834d5a545a806369","repo":"hashicorp/terraform","slug":"failed-to-select-a-workspace-w","errorCode":null,"errorMessage":"Failed to select a workspace: %w","messagePattern":"Failed to select a workspace: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend.go","lineNumber":2317,"sourceCode":"\t\t\t\t\t// So here, we will just ignore the error.\n\t\t\t\t} else {\n\t\t\t\t\t// User needs to run a `terraform workspace new` command to create the missing custom workspace.\n\t\t\t\t\tdiags = append(diags, tfdiags.Sourceless(\n\t\t\t\t\t\ttfdiags.Error,\n\t\t\t\t\t\tfmt.Sprintf(\"Workspace %q has not been created yet\", ws),\n\t\t\t\t\t\tfmt.Sprintf(\"State store %q in provider %s (%q) reports that no workspaces currently exist. To create the custom workspace %q use the command `terraform workspace new %s`.\",\n\t\t\t\t\t\t\tc.Type,\n\t\t\t\t\t\t\tc.Provider.Name,\n\t\t\t\t\t\t\tc.ProviderAddr,\n\t\t\t\t\t\t\tws,\n\t\t\t\t\t\t\tws,\n\t\t\t\t\t\t),\n\t\t\t\t\t))\n\t\t\t\t\treturn nil, diags\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// For all other errors, report via diagnostics\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to select a workspace: %w\", err))\n\t\t\t}\n\t\t}\n\t}\n\tif diags.HasErrors() {\n\t\treturn nil, diags\n\t}\n\n\t// Update backend state file\n\tif err := backendSMgr.WriteState(s); err != nil {\n\t\tdiags = diags.Append(errBackendWriteSavedDiag(err))\n\t\treturn nil, diags\n\t}\n\tif err := backendSMgr.PersistState(); err != nil {\n\t\tdiags = diags.Append(errBackendWriteSavedDiag(err))\n\t\treturn nil, diags\n\t}\n\n\treturn b, diags","sourceCodeStart":2299,"sourceCodeEnd":2335,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta_backend.go#L2299-L2335","documentation":"In Meta.stateStore_C_s (meta_backend.go:2317), the catch-all branch for selectWorkspace errors that are NOT errBackendNoExistingWorkspaces. selectWorkspace verifies the configured/selected workspace exists in the state store; any failure other than 'no workspaces exist at all' surfaces here (e.g. the backend's Workspaces() call to list/verify errored, or the workspace listing itself failed).","triggerScenarios":"The state-store backend returned an error enumerating or verifying workspaces (auth, network, permissions on the remote store); a workspace name that exists but cannot be read due to store-side issues.","commonSituations":"Wrong/expired credentials for the state store, a misconfigured store endpoint, insufficient IAM/bucket/object permissions, or a transient outage of the storage backend.","solutions":["Read the wrapped `%w` for the backend-specific cause (auth, 403, network, not-found).","Verify state-store credentials and that the principal can list/read the configured store/path.","Confirm the workspace name is valid and exists: `terraform workspace list` / `terraform workspace select <name>`.","Retry after resolving connectivity/auth; transient store errors are often intermittent."],"exampleFix":"// before: terraform init   (fails: Failed to select a workspace: AccessDenied)\n// after: # grant list/get on the state store, then\nterraform init","handlingStrategy":"validation","validationCode":"// Before init, confirm the state store can list workspaces with the configured credentials.\nfunc stateStoreCanListWorkspaces(b backend.Backend) error {\n    diags := b.Workspaces()\n    if diags.HasErrors() { return diags.Err() }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Transient backend errors enumerating workspaces are often worth one retry.\nif err := cmd.Init(); err != nil && strings.Contains(err.Error(), \"Failed to select a workspace\") {\n    time.Sleep(backoff); return cmd.Init()\n}","preventionTips":["Grant the state-store principal list+read permissions, not just get.","Verify the configured workspace name exists before init.","Use fresh, non-expired credentials for the state store.","Confirm state-store endpoint/path correctness before first init."],"tags":["state-store","workspace","init","auth","backend"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}