hashicorp/terraform · error

error loading state

Error message

error loading state: %w

What it means

Pre-operation setup calls b.StateMgr(op.Workspace); if constructing the state manager itself emits diagnostic errors, the first one is wrapped as 'error loading state'. This is about building/initializing the remote state manager, not yet reading state—the next step (RefreshState) has its own error.

Solutions

  1. Re-run `terraform init` to validate and refresh backend configuration.
  2. Inspect the wrapped diagnostic for the underlying cause (workspace lookup, auth, schema).
  3. Confirm cloud block fields (hostname, organization, workspace name) are correct.
  4. Verify the token has read access on the workspace's state.
Defensive patterns

Strategy: validation

Validate before calling

func backendConfigured(b *Cloud) error {
    if b.Organization == "" { return fmt.Errorf("cloud block missing organization") }
    if b.Hostname == "" { return fmt.Errorf("cloud block missing hostname") }
    if b.client == nil { return fmt.Errorf("cloud client uninitialized; run terraform init") }
    return nil
}

Prevention

When it happens

Trigger: b.StateMgr(workspace) returns sDiags.HasErrors(): backend cannot construct the state manager for the workspace—workspace lookup failure inside the state manager, bad backend configuration, locked-state manager init issues, or schema errors in the state config.

Common situations: Backend block misconfigured (missing credentials, wrong workspace name); state manager cannot resolve the remote workspace; partial backend config after a refactor; bucket/workspace naming changed.

Related errors


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

Appendix: source

Thrown at internal/cloud/backend_context.go:44

func (b *Cloud) LocalRun(ctx context.Context, op *backendrun.Operation) (*backendrun.LocalRun, statemgr.Full, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics
	ret := &backendrun.LocalRun{
		PlanOpts: &terraform.PlanOpts{
			Mode:    op.PlanMode,
			Targets: op.Targets,
		},
	}

	op.StateLocker = op.StateLocker.WithContext(ctx)

	// Get the remote workspace name.
	remoteWorkspaceName := b.getRemoteWorkspaceName(op.Workspace)

	// Get the latest state.
	log.Printf("[TRACE] cloud: requesting state manager for workspace %q", remoteWorkspaceName)
	stateMgr, sDiags := b.StateMgr(op.Workspace)
	if sDiags.HasErrors() {
		diags = diags.Append(fmt.Errorf("error loading state: %w", sDiags.Err()))
		return nil, nil, diags
	}

	log.Printf("[TRACE] cloud: requesting state lock for workspace %q", remoteWorkspaceName)
	if diags := op.StateLocker.Lock(stateMgr, op.Type.String()); diags.HasErrors() {
		return nil, nil, diags
	}

	defer func() {
		// If we're returning with errors, and thus not producing a valid
		// context, we'll want to avoid leaving the remote workspace locked.
		if diags.HasErrors() {
			diags = diags.Append(op.StateLocker.Unlock())
		}
	}()

	log.Printf("[TRACE] cloud: reading remote state for workspace %q", remoteWorkspaceName)
	if err := stateMgr.RefreshState(); err != nil {

View on GitHub (pinned to d32a084675)