{"record":{"id":"4419a8a0252138e1","repo":"hashicorp/terraform","slug":"currently-selected-workspace-q-does-not-exist","errorCode":null,"errorMessage":"Currently selected workspace %q does not exist","messagePattern":"Currently selected workspace %q does not exist","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend.go","lineNumber":299,"sourceCode":"\t// Check if any of the existing workspaces matches the selected\n\t// workspace and create a numbered list of existing workspaces.\n\tvar list strings.Builder\n\tfor i, w := range workspaces {\n\t\tif w == workspace {\n\t\t\tlog.Printf(\"[TRACE] Meta.selectWorkspace: the currently selected workspace is present in the configured backend (%s)\", workspace)\n\t\t\treturn nil\n\t\t}\n\t\tfmt.Fprintf(&list, \"%d. %s\\n\", i+1, w)\n\t}\n\n\t// If the backend only has a single workspace, select that as the current workspace\n\tif len(workspaces) == 1 {\n\t\tlog.Printf(\"[TRACE] Meta.selectWorkspace: automatically selecting the single workspace provided by the backend (%s)\", workspaces[0])\n\t\treturn m.SetWorkspace(workspaces[0])\n\t}\n\n\tif !m.input {\n\t\treturn fmt.Errorf(\"Currently selected workspace %q does not exist\", workspace)\n\t}\n\n\t// Otherwise, ask the user to select a workspace from the list of existing workspaces.\n\tv, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{\n\t\tId: \"select-workspace\",\n\t\tQuery: fmt.Sprintf(\n\t\t\t\"\\n[reset][bold][yellow]The currently selected workspace (%s) does not exist.[reset]\",\n\t\t\tworkspace),\n\t\tDescription: fmt.Sprintf(\n\t\t\tstrings.TrimSpace(inputBackendSelectWorkspace), list.String()),\n\t})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Failed to select workspace: %s\", err)\n\t}\n\n\tidx, err := strconv.Atoi(v)\n\tif err != nil || (idx < 1 || idx > len(workspaces)) {\n\t\treturn fmt.Errorf(\"Failed to select workspace: input not a valid number\")","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta_backend.go#L281-L317","documentation":"Returned from `Meta.selectWorkspace` when the currently selected workspace (from `m.Workspace()`) is not among the workspaces the backend reports, the backend has more than one workspace (so auto-select does not apply), and `m.input` is false so the interactive selection prompt cannot be offered. This is the non-interactive equivalent of 'workspace not found, please choose'.","triggerScenarios":"After listing workspaces (>= 2), none matches the locally selected name, and `-input=false` / `TF_INPUT=0` / a non-TTY context disables prompting. The single-workspace auto-select branch (`len(workspaces) == 1`) was already skipped.","commonSituations":"CI pipeline with `-input=false` after the workspace was renamed or deleted in HCP Terraform; `TF_WORKSPACE` set to a stale name; switching branches where the workspace marker points to a name that no longer exists; the workspace was deleted by a teammate.","solutions":["Set `TF_WORKSPACE=<existing-name>` or run `terraform workspace select <existing-name>` locally first.","Re-create the missing workspace in HCP Terraform, or pick a valid one from the list.","Remove the stale local workspace marker so Terraform falls back to default.","If running in CI, ensure the pipeline selects a known-good workspace before non-interactive commands."],"exampleFix":"# before: stale selection + non-interactive\nexport TF_WORKSPACE=old-name\nterraform plan -input=false\n# after\nterraform workspace select prod\nexport TF_WORKSPACE=prod\nterraform plan -input=false","handlingStrategy":"validation","validationCode":"// Before running non-interactive commands, ensure the selected workspace exists.\nws, _ := m.Workspace()\nexisting, _ := b.Workspaces()\nfound := false\nfor _, w := range existing { if w == ws { found = true; break } }\nif !found {\n    return fmt.Errorf(\"workspace %q missing; select a valid one or set TF_WORKSPACE\", ws)\n}","typeGuard":"func workspaceExists(name string, existing []string) bool {\n    for _, w := range existing { if w == name { return true } }\n    return false\n}","tryCatchPattern":"null","preventionTips":["Pin `TF_WORKSPACE` to a known-existing workspace in CI.","Run `terraform workspace select` before non-interactive commands.","Coordinate workspace renames/deletes across the team."],"tags":["terraform","workspace","cloud","non-interactive","ci"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}