hashicorp/terraform · error

Failed to select workspace: input not a valid number

Error message

Failed to select workspace: input not a valid number

What it means

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.

Solutions

  1. Re-run and enter the NUMBER shown next to the desired workspace (1-based).
  2. Avoid the prompt entirely: `terraform workspace select <name>` or `export TF_WORKSPACE=<name>`.
  3. If scripting the input, ensure you echo a valid 1-based integer matching a listed workspace.

Example fix

# before: typed workspace name at numbered prompt
# 'select-workspace' -> 'prod'
# after: type the number shown
terraform workspace select prod   # bypass prompt
# or at prompt: type '2' (the number next to 'prod')
Defensive patterns

Strategy: validation

Validate before calling

// Validate user input is a 1-based integer in range before consuming it.
idx, err := strconv.Atoi(v)
if err != nil || idx < 1 || idx > len(workspaces) {
    return errors.New("enter the 1-based number next to the desired workspace")
}

Type guard

func validSelection(v string, n int) (int, bool) {
    i, err := strconv.Atoi(v)
    if err != nil || i < 1 || i > n { return 0, false }
    return i, true
}

Try / catch

null

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/ec6ebdd005675d9d. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/meta_backend.go:317

		return fmt.Errorf("Currently selected workspace %q does not exist", workspace)
	}

	// Otherwise, ask the user to select a workspace from the list of existing workspaces.
	v, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{
		Id: "select-workspace",
		Query: fmt.Sprintf(
			"\n[reset][bold][yellow]The currently selected workspace (%s) does not exist.[reset]",
			workspace),
		Description: fmt.Sprintf(
			strings.TrimSpace(inputBackendSelectWorkspace), list.String()),
	})
	if err != nil {
		return fmt.Errorf("Failed to select workspace: %s", err)
	}

	idx, err := strconv.Atoi(v)
	if err != nil || (idx < 1 || idx > len(workspaces)) {
		return fmt.Errorf("Failed to select workspace: input not a valid number")
	}

	workspace = workspaces[idx-1]
	log.Printf("[TRACE] Meta.selectWorkspace: setting the current workspace according to user selection (%s)", workspace)
	return m.SetWorkspace(workspace)
}

// BackendForLocalPlan is similar to Backend, but uses settings that were
// stored in a plan when preparing the returned operations backend.
// The plan's data may describe `backend` or `state_store` configuration.
//
// The current workspace name is also stored as part of the plan, and so this
// method will check that it matches the currently-selected workspace name
// and produce error diagnostics if not.
func (m *Meta) BackendForLocalPlan(plan *plans.Plan) (backendrun.OperationsBackend, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics

	// Check the workspace name in the plan matches the current workspace

View on GitHub (pinned to d32a084675)