{"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":"console","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta_backend.go#L281-L317","documentation":"Returned by selectWorkspace() when input is disabled (m.input==false), there are multiple workspaces in the backend, and the currently selected workspace name is not among them. Because Terraform cannot prompt the user, it fails fast rather than silently picking the wrong state. The %q is the missing workspace name.","triggerScenarios":"Backend returns >1 workspaces, none equal the locally selected workspace (from .terraform/environment or TF_WORKSPACE), and -input=false (or non-interactive) prevents the selection prompt.","commonSituations":"CI runs with -input=false where the workspace was deleted/renamed remotely; switching branches where TF_WORKSPACE points to a workspace that doesn't exist in this backend; a fresh clone where no workspace has been selected yet but TF_WORKSPACE names a non-existent one.","solutions":["Pre-select a valid workspace with `terraform workspace select <name>` (add -or-create if needed) before the non-interactive run.","Set TF_WORKSPACE to a workspace that exists in the backend.","Create the missing workspace: `terraform workspace new <name>`."],"exampleFix":"# before: CI non-interactive, stale workspace\nterraform plan -input=false\n# Currently selected workspace \"old\" does not exist\n# after\nterraform workspace select prod\nterraform plan -input=false","handlingStrategy":"validation","validationCode":"// Pre-validate that the selected workspace exists before non-interactive run\nname := getSelectedWorkspace()\nws, _ := backendWorkspaces()\nif !contains(ws, name) {\n    log.Fatalf(\"workspace %q not found; available: %v\", name, ws)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `terraform workspace select <name>` before non-interactive plan/apply.","Keep TF_WORKSPACE in sync with workspaces that actually exist in the backend.","Use `terraform workspace list` to confirm available workspaces in CI."],"tags":["workspace","non-interactive","ci","select"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}