{"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":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend_migrate.go","lineNumber":521,"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\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?\",\n\t\t\tDescription: fmt.Sprintf(\n\t\t\t\tstrings.TrimSpace(inputBackendMigrateNonEmptyCloud),\n\t\t\t\topts.SourceType, sourcePath, destinationPath, appName),","sourceCodeStart":503,"sourceCodeEnd":539,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta_backend_migrate.go#L503-L539","documentation":"Returned by backendMigrateNonEmptyConfirm's saveHelper when writing the SOURCE state to a temp file fails (saveHelper at line 512-515, called for source at line 520). The statemgr.NewFilesystem(path).WriteState(source) call errored, so the comparison files for the confirmation prompt couldn't be produced.","triggerScenarios":"After the temp dir was created successfully, writing the source .tfstate file fails: disk fills between mkdir and write, write permission on the temp subdir revoked, path too long, or WriteState encounters an internal serialization error. Wrapped at line 521.","commonSituations":"Disk nearly full — mkdir succeeds but write fails; antivirus/scanner locking newly created files on Windows; NFS/ network filesystem write hiccup; SELinux relabeling denying the write.","solutions":["Free disk space on the temp volume and retry.","Set TMPDIR to a local writable disk (avoid network filesystems for temp).","Disable or exempt terraform from AV scanners that lock new files.","Verify write permissions on the temp dir for the running user.","Retry; if persistent, capture the inner error for the exact write failure code."],"exampleFix":"# before: write source state to /tmp fails (NFS hiccup)\n#   Error saving temporary state: ... write: I/O error\n# after: use a local disk for TMPDIR\nexport TMPDIR=/local/tmp\nterraform init","handlingStrategy":"validation","validationCode":"# Verify temp dir is writable AND has space for at least the largest state.\nmaxstate=$(terraform state pull 2>/dev/null | wc -c)\navail=$(df -P \"${TMPDIR:-/tmp}\" | awk 'NR==2{print $4}')\n[ \"$((avail*1024))\" -gt \"$((maxstate*3))\" ] || { echo 'not enough temp space for both states'; exit 1; }\nterraform init","typeGuard":null,"tryCatchPattern":"# On temp write failure, switch TMPDIR and retry.\nterraform init 2>/tmp/init.err || {\n  grep -q 'Error saving temporary state' /tmp/init.err || exit 1\n  export TMPDIR=\"$HOME/.tf-tmp\"; mkdir -p \"$TMPDIR\"\n  terraform init\n}","preventionTips":["Ensure temp disk can hold at least 2x the largest state plus headroom.","Use a local disk for TMPDIR, not network filesystems.","Exempt terraform from AV scanners that lock new files.","Free disk before migrations involving large states."],"tags":["backend","state-migration","filesystem","temp-file","write-error","confirmation-prompt"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}