hashicorp/terraform · warning

Failed to select workspace: %s

Error message

Failed to select workspace: %s

What it means

Returned by selectWorkspace() when the interactive workspace-selection prompt's UIInput().Input() call fails with an error. This occurs after detecting that the current workspace is missing and there are multiple backend workspaces to choose from. The %s wraps the I/O error from the input reader.

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

Solutions

  1. Pre-select the workspace with `terraform workspace select <name>` so no interactive prompt is needed.
  2. Run with -input=false and ensure a valid workspace is pre-selected.
  3. Ensure stdin is a live TTY for interactive sessions, or feed the numbered choice via a properly kept-open pipe.

Example fix

# before: broken stdin during selection prompt
terraform init
# Failed to select workspace: EOF
# after: select non-interactively first
terraform workspace select dev
terraform init
Defensive patterns

Strategy: validation

Validate before calling

// Avoid the selection prompt by pre-selecting a valid workspace
name := getSelectedWorkspace()
if name == "" || !workspaceExists(name) {
    return errors.New("pre-select a valid workspace to avoid interactive prompt")
}

Prevention

When it happens

Trigger: selectWorkspace issues the 'select-workspace' numbered prompt (because current workspace is missing and multiple exist) and the UIInput reader returns a non-nil error — EOF, broken pipe, or read failure on the prompt channel.

Common situations: Interactive selection triggered in a context where stdin is broken (piped input that closed, terminal detached mid-prompt, automation that doesn't expect the prompt); running under a wrapper that intercepts stdin incorrectly.

Related errors


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