{"record":{"id":"757c10161363743b","repo":"hashicorp/terraform","slug":"error-loading-statefile-w","errorCode":null,"errorMessage":"Error loading statefile: %w","messagePattern":"Error loading statefile: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/show.go","lineNumber":385,"sourceCode":"\tconfig, buildDiags := terraform.BuildConfigWithGraph(\n\t\trootMod,\n\t\tloader.ModuleWalker(),\n\t\tvariables,\n\t\tconfigs.MockDataLoaderFunc(loader.LoadExternalMockData),\n\t)\n\tdiags = diags.Append(buildDiags)\n\tif buildDiags.HasErrors() {\n\t\treturn nil, diags\n\t}\n\n\treturn config, diags\n}\n\n// getStateFromPath returns a statefile if the user-supplied path points to a statefile.\nfunc getStateFromPath(path string) (*statefile.File, error) {\n\tfile, err := os.Open(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Error loading statefile: %w\", err)\n\t}\n\tdefer file.Close()\n\n\tvar stateFile *statefile.File\n\tstateFile, err = statefile.Read(file)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Error reading %s as a statefile: %w\", path, err)\n\t}\n\treturn stateFile, nil\n}\n\n// getStateFromBackend returns the State for the current workspace, if available.\nfunc getStateFromBackend(b backend.Backend, workspace string) (*statefile.File, error) {\n\t// Get the state store for the given workspace\n\tstateStore, sDiags := b.StateMgr(workspace)\n\tif sDiags.HasErrors() {\n\t\treturn nil, fmt.Errorf(\"Failed to load state manager: %w\", sDiags.Err())\n\t}","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/show.go#L367-L403","documentation":"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').","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the exact path exists with 'ls -la <path>' and that it is a regular file.","Use an absolute path to avoid working-directory ambiguity.","Check read permissions on the file and parent directories.","If you meant to show current state, run 'terraform show' with no path argument."],"exampleFix":"// before\n$ terraform show ./tfplan                # wrong dir\n// after\n$ terraform show $(pwd)/tfplan/plan       # correct absolute path","handlingStrategy":"validation","validationCode":"// Pre-flight: confirm the path passed to 'terraform show' is a readable regular file\npackage main\n\nfunc preflightShowPath(p string) error {\n\tabs, _ := filepath.Abs(p)\n\tinfo, err := os.Stat(abs)\n\tif err != nil { return fmt.Errorf(\"path %s: %w\", abs, err) }\n\tif info.IsDir() { return fmt.Errorf(\"path %s is a directory, expected a state/plan file\", abs) }\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use absolute paths when invoking 'terraform show <path>'.","Verify the file exists and is readable before passing it.","Run 'terraform show' with no argument to show current state."],"tags":["show","statefile","filesystem","path"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}