{"record":{"id":"757c10161363743b","repo":"hashicorp/terraform","slug":"error-loading-statefile-w","errorCode":null,"errorMessage":"Error loading statefile: %w","messagePattern":"Error loading statefile: %w","errorType":"exception","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/show.go#L367-L403","documentation":"getStateFromPath opens the user-supplied path argument with os.Open; any error (file does not exist, permission denied, not a file) is wrapped with %w. This fires when terraform show <path> is given a path that cannot be opened at all — distinct from a path that opens but is not a valid statefile (739).","triggerScenarios":"terraform show <path> where os.Open(path) returns a non-nil error — ENOENT (no such file), EACCES (permission denied), or the path is a directory.","commonSituations":"Typo in the state file path; relative path resolved from the wrong cwd; state file deleted or never written; running show as a user without read permission; pointing at a directory by mistake.","solutions":["Verify the path exists and is a file: ls -l <path>.","Use an absolute path to avoid cwd-relative resolution issues.","Ensure the invoking user has read permission (chmod/chown as needed).","If the state was never written locally, point at the backend-managed state by running `terraform show` without a path argument."],"exampleFix":"# before\nterraform show ./tfstate/terraform.tfstate\n# after: absolute, verified path\nterraform show /home/user/proj/tfstate/terraform.tfstate","handlingStrategy":"validation","validationCode":"// Check the path exists and is readable before passing to terraform show.\ninfo, err := os.Stat(path)\nif err != nil {\n    return fmt.Errorf(\"state path %s cannot be opened: %w\", path, err)\n}\nif info.IsDir() {\n    return fmt.Errorf(\"state path %s is a directory, not a file\", path)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use absolute paths for state file arguments.","Confirm read permission for the invoking user.","Omit the path to show backend-managed state instead."],"tags":["terraform","show","state","io","filesystem"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}