{"record":{"id":"624143e31788c18e","repo":"hashicorp/packer","slug":"failed-to-create-temp-file-w","errorCode":null,"errorMessage":"failed to create temp file: %w","messagePattern":"failed to create temp file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/hcp-sbom/packer_release_fetch.go","lineNumber":105,"sourceCode":"\t\tsemverList = append(semverList, v)\n\t}\n\n\tif len(semverList) == 0 {\n\t\treturn \"\", fmt.Errorf(\"no stable Packer releases found in index at %s\", indexURL)\n\t}\n\n\tsort.Sort(semver.Collection(semverList))\n\tlatest := semverList[len(semverList)-1]\n\tlog.Printf(\"[INFO] Latest stable Packer version from releases index: %s\", latest.Original())\n\treturn latest.Original(), nil\n}\n\n// downloadURLToTempFile downloads url into a new temp file and returns its path.\n// On any error the temp file is removed. The caller owns the returned file on success.\nfunc downloadURLToTempFile(ctx context.Context, client *http.Client, url, suffix string) (string, error) {\n\tf, err := os.CreateTemp(\"\", \"packer-dl-*\"+suffix)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create temp file: %w\", err)\n\t}\n\ttmpPath := f.Name()\n\n\treq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)\n\tif err != nil {\n\t\t_ = f.Close()\n\t\t_ = os.Remove(tmpPath)\n\t\treturn \"\", err\n\t}\n\n\tresp, err := client.Do(req)\n\tif err != nil {\n\t\t_ = f.Close()\n\t\t_ = os.Remove(tmpPath)\n\t\treturn \"\", fmt.Errorf(\"HTTP request failed: %w\", err)\n\t}\n\tdefer func() { _ = resp.Body.Close() }()\n","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/hcp-sbom/packer_release_fetch.go#L87-L123","documentation":"downloadURLToTempFile starts by creating a uniquely named temporary file via os.CreateTemp(\"\", \"packer-dl-*\"+suffix) to hold the downloaded artifact. If the OS cannot create that file, the function aborts immediately with this wrapped error before any network I/O happens. Because the temp file was never created, there is nothing to clean up and the wrapped os error is the underlying cause (e.g. no space, read-only dir, or TMPDIR problems).","triggerScenarios":"os.CreateTemp fails because the temp directory (TMPDIR/TMP/TEMP or /tmp) does not exist or is not writable, the filesystem holding it is full, the process lacks write permission, resource limits (quota, inodes, fd) are exhausted, or a security policy (sandbox/seccomp) blocks temp-file creation.","commonSituations":"CI runners with a tiny or read-only /tmp; containers where TMPDIR points at a deleted or non-existent volume; disk-full during large builds; hardened containers running as non-root with restricted temp dirs.","solutions":["Check free disk space on the filesystem backing the temp directory (df -h $TMPDIR) and free space if full.","Verify the temp directory exists and is writable by the Packer process: ls -ld ${TMPDIR:-/tmp} and test with touch.","If TMPDIR/TMP/TEMP is set to a bad path, unset it or point it at an existing writable directory.","Run the process with enough OS permissions (or outside the sandbox) so it may create files in the temp dir, or set TMPDIR to a directory it can write.","Inspect the wrapped error in the message (e.g. 'permission denied', 'no space left on device') and fix the matching OS condition."],"exampleFix":"// before: default temp dir may be unusable in the container\npath, err := downloadURLToTempFile(ctx, client, url, \".zip\")\n// after: point the process at a known-writable workspace dir first\nos.Setenv(\"TMPDIR\", \"/workspace/.tmp\") // must exist and be writable\nif err := os.MkdirAll(\"/workspace/.tmp\", 0o755); err != nil { return err }\npath, err := downloadURLToTempFile(ctx, client, url, \".zip\")","handlingStrategy":"validation","validationCode":"// Verify the temp directory is writable before invoking the download\nfunc ensureTempWritable() error {\n    dir := os.TempDir()\n    if info, err := os.Stat(dir); err != nil || !info.IsDir() {\n        return fmt.Errorf(\"temp dir %q missing\", dir)\n    }\n    probe, err := os.CreateTemp(dir, \"probe-*\")\n    if err != nil {\n        return fmt.Errorf(\"temp dir %q not writable: %w\", dir, err)\n    }\n    name := probe.Name()\n    probe.Close()\n    os.Remove(name)\n    return nil\n}","typeGuard":null,"tryCatchPattern":"path, err := downloadURLToTempFile(ctx, client, url, \".zip\")\nif err != nil {\n    if errors.Is(err, os.ErrPermission) || strings.Contains(err.Error(), \"no space left on device\") {\n        // fix environment: free space or set TMPDIR to a writable dir, then retry once\n        os.Setenv(\"TMPDIR\", \"/var/tmp\")\n        path, err = downloadURLToTempFile(ctx, client, url, \".zip\")\n    }\n    if err != nil {\n        return fmt.Errorf(\"cannot stage download: %w\", err)\n    }\n}","preventionTips":["Set TMPDIR explicitly in containers/CI to a known writable volume with several GB free.","Monitor disk usage on the build agent's temp partition and fail fast on low-space alerts.","Never run builds with a read-only root filesystem unless TMPDIR points at an ephemeral writable mount.","Include the wrapped os error when reporting so 'permission denied' vs 'no space left' is distinguishable.","Probe temp-dir writability once at job startup rather than mid-build."],"tags":["filesystem","temp-file","os","disk-space"],"backgroundTag":"temp-file-creation-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}