hashicorp/terraform · error
Error selecting workspace: %s
Error message
Error selecting workspace: %s
What it means
Emitted by InitCommand.run when c.Workspace() fails after a backend is available (init_run.go:314-316). Workspace() resolves the active workspace name (default, or via TF_WORKSPACE / backend config / terraform workspace). The %s carries the underlying error. Init aborts before touching state.
Source
Thrown at internal/command/init_run.go:316
return 1
}
}
providerHook := &providerPolicyHook{
client: policyClient,
view: view,
rootModule: rootModEarly,
}
var state *states.State
// If we have a functional backend (either just initialized or initialized
// on a previous run) we'll use the current state as a potential source
// of provider dependencies.
if back != nil {
c.ignoreRemoteVersionConflict(back)
workspace, err := c.Workspace()
if err != nil {
diags = diags.Append(fmt.Errorf("Error selecting workspace: %s", err))
view.Diagnostics(diags)
return 1
}
sMgr, sDiags := back.StateMgr(workspace)
if sDiags.HasErrors() {
diags = diags.Append(fmt.Errorf("Error loading state: %s", sDiags.Err()))
view.Diagnostics(diags)
return 1
}
if err := sMgr.RefreshState(); err != nil {
diags = diags.Append(fmt.Errorf("Error refreshing state: %s", err))
view.Diagnostics(diags)
return 1
}
state = sMgr.State()
}View on GitHub (pinned to c9def3e214)
Solutions
- Unset TF_WORKSPACE or set it to an existing workspace: `unset TF_WORKSPACE` then `terraform workspace select <name>`.
- Create the workspace if the backend requires it: `terraform workspace new <name>`.
- Reset the local backend state marker if corrupted: back up and remove `.terraform/terraform.tfstate`.
- Inspect the wrapped error for backend-specific reasons (e.g. S3/consul listing failure) and fix credentials/connectivity.
Example fix
# before $ TF_WORKSPACE=prod terraform init # 'prod' does not exist # after $ unset TF_WORKSPACE $ terraform workspace new prod $ terraform init
Defensive patterns
Strategy: try-catch
Validate before calling
// If TF_WORKSPACE is set, validate the workspace exists for the backend.
if ws := os.Getenv("TF_WORKSPACE"); ws != "" {
if err := run("terraform", "workspace", "select", ws); err != nil {
return fmt.Errorf("workspace %q not selectable: %w", ws, err)
}
} Try / catch
// Fall back to the default workspace if a custom one cannot be selected.
if err := run("terraform", "init"); err != nil {
os.Unsetenv("TF_WORKSPACE")
err = run("terraform", "init")
} Prevention
- Do not set TF_WORKSPACE to a non-existent workspace for backends that don't auto-create.
- Create workspaces explicitly with `terraform workspace new` before referencing them.
- Reset a corrupted .terraform/terraform.tfstate marker when workspace resolution misbehaves.
When it happens
Trigger: A backend is initialized but the workspace name cannot be resolved: TF_WORKSPACE set to a name that doesn't exist in a multi-workspace backend, the configured workspace environment var is invalid, the local backend's workspace state is corrupt, or the backend returns an error computing workspaces.
Common situations: Setting TF_WORKSPACE to a non-existent workspace for a backend that does not auto-create workspaces; misconfigured backend workspace listing; running init with TF_WORKSPACE exported incorrectly in CI; corrupted .terraform/terraform.tfstate workspace marker.
Related errors
- default workspace not supported You can create a new workspa
- workspaces not supported
- empty state name
- cannot delete default state
- missing state name
AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07).
Data as JSON: /api/errors/4dab76f87adddee3.
Report an issue: GitHub.