{"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":"exception","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta_backend.go#L2299-L2335","documentation":"On the first-time state_store init path, when selectWorkspace fails with any error OTHER than errBackendNoExistingWorkspaces, that error is wrapped here. It represents the state-store provider rejecting workspace selection/verification (auth, connectivity, or a custom workspace error from the provider).","triggerScenarios":"selectWorkspace(b) returns a non-empty-workspaces error during state_store init. Triggers: provider cannot list/verify workspaces (auth failure, endpoint down), the selected workspace does not exist and is not the default, or the provider's Workspaces API errored.","commonSituations":"Wrong/insufficient provider credentials for the workspace API; the named workspace was deleted out-of-band; provider endpoint misconfigured or temporarily unreachable; selecting a custom workspace that was never created.","solutions":["Read inner %w for the provider-specific workspace error (auth, 404, network).","Fix provider credentials/endpoint in the state_store provider_config block, then re-init.","Create the missing workspace (`tofu workspace new <name>`) or switch to one that exists.","If the store legitimately has no workspaces and you want the default, ensure TF_WORKSPACE is unset so selectWorkspace takes the empty-store path instead."],"exampleFix":"// before\nstate_store \"acme\" {\n  provider = \"example.com/acme/storage\"\n  provider_config { token = \"wrong\" }\n}\ntofu init  # -> Failed to select a workspace\n\n// after: correct credentials / target an existing workspace\nstate_store \"acme\" {\n  provider = \"example.com/acme/storage\"\n  provider_config { token = \"correct\" }\n}\ntofu init","handlingStrategy":"try-catch","validationCode":"// Before init, verify the selected workspace exists (or is the default) in the\n// state store, so selectWorkspace does not fail.\nfunc workspaceExistsOrIsDefault(b backend.Backend, want string) error {\n    if want == backend.DefaultStateName { return nil }\n    s, err := b.Workspaces()\n    if err != nil { return fmt.Errorf(\"cannot list workspaces: %w\", err) }\n    for _, w := range s { if w == want { return nil } }\n    return fmt.Errorf(\"workspace %q does not exist; create with 'tofu workspace new %s'\", want, want)\n}","typeGuard":null,"tryCatchPattern":"if err := m.selectWorkspace(b); err != nil {\n    if errors.Is(err, &errBackendNoExistingWorkspaces{}) {\n        // handled by default-workspace check elsewhere\n    } else {\n        if isAuthErr(err) {\n            diags = diags.Append(fmt.Errorf(\"Failed to select a workspace (check provider credentials): %w\", err))\n        } else {\n            diags = diags.Append(fmt.Errorf(\"Failed to select a workspace: %w\", err))\n        }\n    }\n}","preventionTips":["Pre-create non-default workspaces in the state store before init.","Use correct, sufficiently-permissioned provider credentials for the workspace API.","Unset TF_WORKSPACE when initializing an empty store to use the default."],"tags":["state-store","workspace","provider","init","authentication"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}