hashicorp/terraform · error

Currently selected workspace

Error message

Currently selected workspace %q does not exist

What it means

Returned from `Meta.selectWorkspace` when the currently selected workspace (from `m.Workspace()`) is not among the workspaces the backend reports, the backend has more than one workspace (so auto-select does not apply), and `m.input` is false so the interactive selection prompt cannot be offered. This is the non-interactive equivalent of 'workspace not found, please choose'.

Solutions

  1. Set `TF_WORKSPACE=<existing-name>` or run `terraform workspace select <existing-name>` locally first.
  2. Re-create the missing workspace in HCP Terraform, or pick a valid one from the list.
  3. Remove the stale local workspace marker so Terraform falls back to default.
  4. If running in CI, ensure the pipeline selects a known-good workspace before non-interactive commands.

Example fix

# before: stale selection + non-interactive
export TF_WORKSPACE=old-name
terraform plan -input=false
# after
terraform workspace select prod
export TF_WORKSPACE=prod
terraform plan -input=false
Defensive patterns

Strategy: validation

Validate before calling

// Before running non-interactive commands, ensure the selected workspace exists.
ws, _ := m.Workspace()
existing, _ := b.Workspaces()
found := false
for _, w := range existing { if w == ws { found = true; break } }
if !found {
    return fmt.Errorf("workspace %q missing; select a valid one or set TF_WORKSPACE", ws)
}

Type guard

func workspaceExists(name string, existing []string) bool {
    for _, w := range existing { if w == name { return true } }
    return false
}

Try / catch

null

Prevention

When it happens

Trigger: After listing workspaces (>= 2), none matches the locally selected name, and `-input=false` / `TF_INPUT=0` / a non-TTY context disables prompting. The single-workspace auto-select branch (`len(workspaces) == 1`) was already skipped.

Common situations: CI pipeline with `-input=false` after the workspace was renamed or deleted in HCP Terraform; `TF_WORKSPACE` set to a stale name; switching branches where the workspace marker points to a name that no longer exists; the workspace was deleted by a teammate.

Related errors


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

Appendix: source

Thrown at internal/command/meta_backend.go:299

	// Check if any of the existing workspaces matches the selected
	// workspace and create a numbered list of existing workspaces.
	var list strings.Builder
	for i, w := range workspaces {
		if w == workspace {
			log.Printf("[TRACE] Meta.selectWorkspace: the currently selected workspace is present in the configured backend (%s)", workspace)
			return nil
		}
		fmt.Fprintf(&list, "%d. %s\n", i+1, w)
	}

	// If the backend only has a single workspace, select that as the current workspace
	if len(workspaces) == 1 {
		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")

View on GitHub (pinned to d32a084675)