{"record":{"id":"5937a9b6b0dc76e9","repo":"hashicorp/terraform","slug":"error-saving-temporary-state-s","errorCode":null,"errorMessage":"Error saving temporary state: %s","messagePattern":"Error saving temporary state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend_migrate.go","lineNumber":541,"sourceCode":"\n\t// Save both to a temporary\n\ttd, err := os.MkdirTemp(\"\", \"terraform\")\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"Error creating temporary directory: %s\", err)\n\t}\n\tdefer os.RemoveAll(td)\n\n\t// Helper to write the state\n\tsaveHelper := func(n, path string, s *states.State) error {\n\t\tmgr := statemgr.NewFilesystem(path)\n\t\treturn mgr.WriteState(s)\n\t}\n\n\t// Write the states\n\tsourcePath := filepath.Join(td, fmt.Sprintf(\"1-%s.tfstate\", opts.SourceType))\n\tdestinationPath := filepath.Join(td, fmt.Sprintf(\"2-%s.tfstate\", opts.DestinationType))\n\tif err := saveHelper(opts.SourceType, sourcePath, source); err != nil {\n\t\treturn false, fmt.Errorf(\"Error saving temporary state: %s\", err)\n\t}\n\tif err := saveHelper(opts.DestinationType, destinationPath, destination); err != nil {\n\t\treturn false, fmt.Errorf(\"Error saving temporary state: %s\", err)\n\t}\n\n\tsrcWord := backendHumanName(opts.Source)\n\tdstWord := backendHumanName(opts.Destination)\n\n\t// Ask for confirmation\n\tvar inputOpts *terraform.InputOpts\n\tif opts.DestinationType == \"cloud\" {\n\t\tappName := \"HCP Terraform\"\n\t\tif cloudBackend, ok := opts.Destination.(*cloud.Cloud); ok {\n\t\t\tappName = cloudBackend.AppName()\n\t\t}\n\t\tinputOpts = &terraform.InputOpts{\n\t\t\tId:    \"backend-migrate-to-tfc\",\n\t\t\tQuery: \"Do you want to copy existing state to HCP Terraform?\",","sourceCodeStart":523,"sourceCodeEnd":559,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta_backend_migrate.go#L523-L559","documentation":"Returned by `backendMigrateNonEmptyConfirm` when writing the SOURCE state snapshot to the temp directory fails. The helper opens a filesystem state manager at the temp path and calls `WriteState`; failure prevents the confirmation diff from being shown.","triggerScenarios":"Fires when `saveHelper(opts.SourceType, sourcePath, source)` returns an error — the temp directory was created (697 passed) but writing `1-<SourceType>.tfstate` failed due to disk-full mid-write, permission flip, or a serialization error in the state.","commonSituations":"tmpfs filled by the source state size, concurrent cleanup racing with the write, an extremely large state that exceeds the temp volume, or filesystem encoding issues in `opts.SourceType`.","solutions":["Point `TMPDIR` at a larger writable volume and retry.","Use `-force-copy` to bypass the diff confirmation for non-empty states.","Free space on the temp filesystem.","If the source state is suspiciously large, audit it for runaway resources before migrating."],"exampleFix":"# before\nterraform init   # temp write of source state failed\n# after\nTMPDIR=/data/tftmp terraform init -force-copy","handlingStrategy":"validation","validationCode":"# Ensure temp volume has at least 2x the largest state size.\nTMPDIR=${TMPDIR:-/tmp}; df -h \"$TMPDIR\"\n# Optionally bypass the diff:\nterraform init -force-copy","typeGuard":null,"tryCatchPattern":"# On source-snapshot write failure, enlarge TMPDIR or skip the diff:\nif ! terraform init >/tmp/init.log 2>&1; then\n  grep -q 'Error saving temporary state' /tmp/init.log \\\n    && { echo 'Temp too small for source snapshot; set TMPDIR or use -force-copy' >&2; exit 2; }\n  cat /tmp/init.log; exit 1\nfi","preventionTips":["Provision temp space at least 2-3x the largest state file size.","Use -force-copy in trusted CI to avoid writing snapshots.","Audit unusually large states before migration."],"tags":["backend","migration","temp-dir","state-write","source","confirmation","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}