{"record":{"id":"c6dc98ce5bb5c13b","repo":"hashicorp/packer","slug":"failed-to-open-source-file-s-for-reading-s","errorCode":null,"errorMessage":"Failed to open source file %s for reading: %s","messagePattern":"Failed to open source file (.+?) for reading: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"post-processor/compress/post-processor.go","lineNumber":236,"sourceCode":"\t\tui.Say(fmt.Sprintf(\"Zipping %s\", target))\n\t\terr = createZipArchive(artifact.Files(), output)\n\t\tif err != nil {\n\t\t\treturn nil, false, false, fmt.Errorf(\"Error creating zip: %s\", err)\n\t\t}\n\tdefault:\n\t\t// Filename indicates no tarball (just compress) so we'll do an io.Copy\n\t\t// into our compressor.\n\t\tif len(artifact.Files()) != 1 {\n\t\t\treturn nil, false, false, fmt.Errorf(\n\t\t\t\t\"Can only have 1 input file when not using tar/zip. Found %d \"+\n\t\t\t\t\t\"files: %v\", len(artifact.Files()), artifact.Files())\n\t\t}\n\t\tarchiveFile := artifact.Files()[0]\n\t\tui.Say(fmt.Sprintf(\"Archiving %s with %s\", archiveFile, compression))\n\n\t\tsource, err := os.Open(archiveFile)\n\t\tif err != nil {\n\t\t\treturn nil, false, false, fmt.Errorf(\n\t\t\t\t\"Failed to open source file %s for reading: %s\",\n\t\t\t\tarchiveFile, err)\n\t\t}\n\t\tdefer source.Close()\n\n\t\tif _, err = io.Copy(output, source); err != nil {\n\t\t\treturn nil, false, false, fmt.Errorf(\"Failed to compress %s: %s\",\n\t\t\t\tarchiveFile, err)\n\t\t}\n\t}\n\n\tui.Say(fmt.Sprintf(\"Archive %s completed\", target))\n\n\treturn newArtifact, false, false, nil\n}\n\nfunc (config *Config) detectFromFilename() {\n\tvar result [][]string","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/post-processor/compress/post-processor.go#L218-L254","documentation":"The single-file compression path of Packer's compress post-processor could not open the artifact's source file for reading. os.Open failed, so no bytes were copied into the compressor and the build failed. This is a read-side file-access error, not a compression error.","triggerScenarios":"The default (non tar/zip) branch calls os.Open(archiveFile) on the artifact's single file and the OS returns an error: file missing, permission denied, or path is a directory/symlink to nothing.","commonSituations":"A previous post-processor deleted or renamed the artifact file; artifact path became invalid after artifact.Files() was captured; running Packer as a user lacking read permission on the build output; file on a detached/unmounted volume.","solutions":["Verify the file listed in the error exists and is readable by the Packer process (ls -l / stat)","Check permissions/ownership on the output directory and the artifact file","Ensure no prior post-processor removes or moves artifact files before compress runs","Re-run the build to rule out a transient filesystem issue"],"exampleFix":"# before (unreadable file)\nchmod 600 output/disk.img\n# after\nchmod 644 output/disk.img","handlingStrategy":"validation","validationCode":"// verify the single artifact file is openable before compression\nif len(artifact.Files()) == 1 {\n    f, err := os.Open(artifact.Files()[0])\n    if err != nil { return fmt.Errorf(\"source unreadable: %w\", err) }\n    f.Close()\n}","typeGuard":null,"tryCatchPattern":"// Go: surface the OS-level cause from the wrapped error\nif err != nil && strings.Contains(err.Error(), \"Failed to open source file\") {\n    if errors.Is(err, os.ErrPermission) || strings.Contains(err.Error(), \"permission denied\") {\n        // fix permissions and retry\n    }\n    return err\n}","preventionTips":["Ensure the Packer process user owns/can read build outputs","Avoid renaming or removing artifact files in earlier post-processors","Run packer validate and a smoke build after changing post-processor order","Use absolute, stable output_directory paths"],"tags":["packer","post-processor","filesystem","file-access"],"backgroundTag":"file-open-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"}