{"record":{"id":"ec6ebdd005675d9d","repo":"hashicorp/terraform","slug":"failed-to-select-workspace-input-not-a-valid-numb","errorCode":null,"errorMessage":"Failed to select workspace: input not a valid number","messagePattern":"Failed to select workspace: input not a valid number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend.go","lineNumber":317,"sourceCode":"\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\")\n\t}\n\n\tworkspace = workspaces[idx-1]\n\tlog.Printf(\"[TRACE] Meta.selectWorkspace: setting the current workspace according to user selection (%s)\", workspace)\n\treturn m.SetWorkspace(workspace)\n}\n\n// BackendForLocalPlan is similar to Backend, but uses settings that were\n// stored in a plan when preparing the returned operations backend.\n// The plan's data may describe `backend` or `state_store` configuration.\n//\n// The current workspace name is also stored as part of the plan, and so this\n// method will check that it matches the currently-selected workspace name\n// and produce error diagnostics if not.\nfunc (m *Meta) BackendForLocalPlan(plan *plans.Plan) (backendrun.OperationsBackend, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\t// Check the workspace name in the plan matches the current workspace","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta_backend.go#L299-L335","documentation":"Returned from `Meta.selectWorkspace` when the user's response to the workspace-selection prompt is not a valid integer index into the list. Either `strconv.Atoi(v)` fails (non-numeric input like 'abc') or the parsed index is outside `[1, len(workspaces)]`. The user did respond, but with an out-of-range or non-numeric value.","triggerScenarios":"The user typed a workspace name instead of its number, typed `0` or a number larger than the list length, typed non-numeric text, or an automated input feed supplied a bad value. `Atoi` error or bounds check triggers the return.","commonSituations":"User misunderstands the prompt and types the workspace name; user types `0` thinking it's zero-indexed; piped input supplies the wrong token; list is short and user guesses a high number.","solutions":["Re-run and enter the NUMBER shown next to the desired workspace (1-based).","Avoid the prompt entirely: `terraform workspace select <name>` or `export TF_WORKSPACE=<name>`.","If scripting the input, ensure you echo a valid 1-based integer matching a listed workspace."],"exampleFix":"# before: typed workspace name at numbered prompt\n# 'select-workspace' -> 'prod'\n# after: type the number shown\nterraform workspace select prod   # bypass prompt\n# or at prompt: type '2' (the number next to 'prod')","handlingStrategy":"validation","validationCode":"// Validate user input is a 1-based integer in range before consuming it.\nidx, err := strconv.Atoi(v)\nif err != nil || idx < 1 || idx > len(workspaces) {\n    return errors.New(\"enter the 1-based number next to the desired workspace\")\n}","typeGuard":"func validSelection(v string, n int) (int, bool) {\n    i, err := strconv.Atoi(v)\n    if err != nil || i < 1 || i > n { return 0, false }\n    return i, true\n}","tryCatchPattern":"null","preventionTips":["Instruct users to type the NUMBER, not the workspace name.","Prefer `terraform workspace select <name>` to bypass the numbered prompt.","When scripting, echo a valid 1-based integer."],"tags":["terraform","workspace","input","validation","interactive"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}