hashicorp/terraform · warning

Failed to select workspace: input not a valid number

Error message

Failed to select workspace: input not a valid number

What it means

Returned by selectWorkspace() when the user's response to the numbered workspace-selection prompt is not a valid integer, or is out of range. strconv.Atoi fails (non-numeric) or the parsed index is <1 or >len(workspaces). This is a pure input-validation failure on interactive selection.

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 c9def3e214)

Solutions

  1. Re-run and enter a number between 1 and the count shown in the prompt list.
  2. Avoid the prompt entirely by pre-selecting with `terraform workspace select <name>`.

Example fix

// before: typed name instead of number
> 1. dev  2. prod
> prod
// after
> 2
Defensive patterns

Strategy: validation

Validate before calling

// If scripting the prompt, validate the numeric choice beforehand
if idx, err := strconv.Atoi(choice); err != nil || idx < 1 || idx > len(workspaces) {
    return fmt.Errorf("invalid selection %q; must be 1..%d", choice, len(workspaces))
}

Prevention

When it happens

Trigger: At the 'The currently selected workspace does not exist' numbered prompt, the user enters a non-numeric string, a number less than 1, or a number greater than the listed workspace count.

Common situations: User types the workspace name instead of its number; typos; zero or negative numbers; copying a stale list where the indices shifted.

Related errors


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