{"record":{"id":"2b6a85ffacbd9a50","repo":"hashicorp/terraform","slug":"failed-to-check-current-workspace-w","errorCode":null,"errorMessage":"Failed to check current workspace: %w","messagePattern":"Failed to check current workspace: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend.go","lineNumber":1211,"sourceCode":"\t\t// TODO: Navigating state upgrades that do not force an explicit migration &&\n\t\t// identifying when migration is required.\n\n\t\t// We're not initializing\n\t\t// AND the config's and backend state file's hash values match, indicating that the stored config is valid and completely unchanged.\n\t\t// AND we're not providing any overrides. An override can mean a change overriding an unchanged backend block (indicated by the hash value).\n\t\tif (uint64(cHash) == s.StateStore.Hash) && (!opts.Init || opts.ConfigOverride == nil) {\n\t\t\tlog.Printf(\"[TRACE] Meta.Backend: using already-initialized, unchanged %q state_store configuration\", stateStoreConfig.Type)\n\t\t\tsavedStateStore, sssDiags := m.savedStateStore(sMgr)\n\t\t\tdiags = diags.Append(sssDiags)\n\t\t\t// Verify that selected workspace exist. Otherwise prompt user to create one\n\t\t\tif opts.Init && savedStateStore != nil {\n\t\t\t\tif err := m.selectWorkspace(savedStateStore); err != nil {\n\t\t\t\t\tif errors.Is(err, &errBackendNoExistingWorkspaces{}) {\n\t\t\t\t\t\t// We tolerate no workspaces if we're using a state store and\n\t\t\t\t\t\t// the default workspace is selected.\n\t\t\t\t\t\tws, err := m.Workspace()\n\t\t\t\t\t\tif err != nil {\n\t\t\t\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to check current workspace: %w\", err))\n\t\t\t\t\t\t\treturn nil, diags\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif ws == backend.DefaultStateName {\n\t\t\t\t\t\t\t// If the default workspace is selected, no workspaces existing _may_ be expected.\n\t\t\t\t\t\t\t// It's valid for the default workspace's state to not be created until the first apply takes place.\n\t\t\t\t\t\t\t// However, it could be that the user is configuring their working directory for the first time but\n\t\t\t\t\t\t\t// they expect pre-existing state to be in the store from previous actions. In that case, the user\n\t\t\t\t\t\t\t// should realise their mistake once they generate a plan.\n\t\t\t\t\t\t\t//\n\t\t\t\t\t\t\t// So here, we will just ignore the error.\n\t\t\t\t\t\t} else {\n\t\t\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\t\t\tdiags = diags.Append(err)\n\t\t\t\t\t\t\treturn nil, diags\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// Report all other errors\n\t\t\t\t\t\tdiags = diags.Append(err)","sourceCodeStart":1193,"sourceCodeEnd":1229,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta_backend.go#L1193-L1229","documentation":"Appended as a diagnostic during state_store initialization in Meta.Backend() when, after selectWorkspace returns errBackendNoExistingWorkspaces (tolerated for the default workspace), the follow-up m.Workspace() call to decide tolerance fails. The %w wraps the Workspace() error. This is a defensive re-check that surfaces an unresolvable workspace name rather than silently misclassifying it.","triggerScenarios":"State_store backend returned no workspaces, the code attempts to verify whether the default workspace is selected (to tolerate the empty case), and Workspace() itself errors — typically because TF_WORKSPACE is invalid or .terraform/environment is corrupted (errors 642/643).","commonSituations":"A state_store-based config where, simultaneously, the backend has no workspaces AND the local workspace selector is invalid — e.g. TF_WORKSPACE set to a bad name during init against a fresh state store; corrupted .terraform/environment after a state_store migration.","solutions":["Fix the underlying Workspace() error: validate TF_WORKSPACE and .terraform/environment (errors 642/643).","Ensure the default workspace is selected (`terraform workspace select default` or unset TF_WORKSPACE) so the no-workspaces tolerance path applies cleanly.","Re-run `terraform init` after correcting the workspace selector."],"exampleFix":"# before: invalid TF_WORKSPACE + empty state store\nTF_WORKSPACE=\"bad name\" terraform init\n# Failed to check current workspace\n# after\nunset TF_WORKSPACE\nterraform init","handlingStrategy":"validation","validationCode":"// Ensure workspace selector is valid before state_store init\nname := os.Getenv(\"TF_WORKSPACE\")\nif name != \"\" && !arguments.ValidWorkspaceName(name) {\n    log.Fatalf(\"TF_WORKSPACE=%q invalid; fix before init\", name)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep TF_WORKSPACE valid or unset (default workspace) during state_store init.","Validate .terraform/environment isn't corrupted before init.","Unset TF_WORKSPACE when initializing against a fresh state store."],"tags":["state-store","workspace","tf-workspace","init"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}