hashicorp/terraform · error

Error loading statefile: %w

Error message

Error loading statefile: %w

What it means

Wrapped error from getStateFromPath when os.Open(path) fails — i.e. the file passed to 'terraform show <path>' cannot be opened. This is the first attempt in the state-file parsing branch: if the path is not a directory, not present, or lacks read permission, the open fails and %w carries the OS error (often 'no such file or directory' or 'permission denied').

Source

Thrown at internal/command/show.go:385

	config, buildDiags := terraform.BuildConfigWithGraph(
		rootMod,
		loader.ModuleWalker(),
		variables,
		configs.MockDataLoaderFunc(loader.LoadExternalMockData),
	)
	diags = diags.Append(buildDiags)
	if buildDiags.HasErrors() {
		return nil, diags
	}

	return config, diags
}

// getStateFromPath returns a statefile if the user-supplied path points to a statefile.
func getStateFromPath(path string) (*statefile.File, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, fmt.Errorf("Error loading statefile: %w", err)
	}
	defer file.Close()

	var stateFile *statefile.File
	stateFile, err = statefile.Read(file)
	if err != nil {
		return nil, fmt.Errorf("Error reading %s as a statefile: %w", path, err)
	}
	return stateFile, nil
}

// getStateFromBackend returns the State for the current workspace, if available.
func getStateFromBackend(b backend.Backend, workspace string) (*statefile.File, error) {
	// Get the state store for the given workspace
	stateStore, sDiags := b.StateMgr(workspace)
	if sDiags.HasErrors() {
		return nil, fmt.Errorf("Failed to load state manager: %w", sDiags.Err())
	}

View on GitHub (pinned to c9def3e214)

Solutions

  1. Verify the exact path exists with 'ls -la <path>' and that it is a regular file.
  2. Use an absolute path to avoid working-directory ambiguity.
  3. Check read permissions on the file and parent directories.
  4. If you meant to show current state, run 'terraform show' with no path argument.

Example fix

// before
$ terraform show ./tfplan                # wrong dir
// after
$ terraform show $(pwd)/tfplan/plan       # correct absolute path
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight: confirm the path passed to 'terraform show' is a readable regular file
package main

func preflightShowPath(p string) error {
	abs, _ := filepath.Abs(p)
	info, err := os.Stat(abs)
	if err != nil { return fmt.Errorf("path %s: %w", abs, err) }
	if info.IsDir() { return fmt.Errorf("path %s is a directory, expected a state/plan file", abs) }
	return nil
}

Prevention

When it happens

Trigger: Running 'terraform show <path>' where <path> does not exist, points to a directory, is unreadable, or the path is mistyped. getStateFromPath is only reached after plan-file parsing already failed.

Common situations: Typo in the plan/state file path; pointing at a directory instead of a file; file deleted between plan and show; permission/ownership mismatch; relative path resolved against the wrong working directory.

Related errors


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