hashicorp/terraform · error

Failed to select workspace

Error message

Failed to select workspace: %s

What it means

Returned from `Meta.selectWorkspace` when the interactive workspace-selection prompt (`UIInput().Input` with id `select-workspace`) itself returns an error. The user was shown the numbered list of existing workspaces but the input mechanism failed before a choice could be read. Distinct from error 655 (invalid choice) — here the prompt never returned a value.

Solutions

  1. Avoid the prompt by pre-selecting: `terraform workspace select <name>` or `TF_WORKSPACE=<name>`.
  2. Re-run in a stable terminal and answer the prompt promptly.
  3. Use `-input=false` with an explicitly selected workspace so the prompt is never shown.

Example fix

# before: prompt errors mid-selection
terraform plan
# after: pre-select
terraform workspace select prod
terraform plan
Defensive patterns

Strategy: validation

Validate before calling

// Avoid the selection prompt by pre-selecting a workspace.
if err := m.SetWorkspace(wanted); err != nil { return err }
// then run with -input=false to skip prompts

Type guard

null

Try / catch

// UIInput errors are not retryable; pre-select the workspace to avoid the prompt.

Prevention

When it happens

Trigger: `UIInput().Input` errors due to stdin closure, interrupt (Ctrl-C), a non-TTY with input enabled but no piped value, or an internal UIInput error. The workspace list was built and displayed, but reading the selection failed.

Common situations: An interrupt during workspace selection; stdin closed by a wrapper; running interactively but a terminal multiplexer detached mid-prompt.

Related errors


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

Appendix: source

Thrown at internal/command/meta_backend.go:312

		log.Printf("[TRACE] Meta.selectWorkspace: automatically selecting the single workspace provided by the backend (%s)", workspaces[0])
		return m.SetWorkspace(workspaces[0])
	}

	if !m.input {
		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

View on GitHub (pinned to d32a084675)