{"record":{"id":"f27b62fc5dd31f23","repo":"hashicorp/packer","slug":"unable-to-create-dir-for-archive-s-s","errorCode":null,"errorMessage":"Unable to create dir for archive %s: %s","messagePattern":"Unable to create dir for archive (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"post-processor/compress/post-processor.go","lineNumber":144,"sourceCode":"\t\tgeneratedData = make(map[interface{}]interface{})\n\t}\n\n\t// These are extra variables that will be made available for interpolation.\n\tgeneratedData[\"BuildName\"] = p.config.PackerBuildName\n\tgeneratedData[\"BuilderType\"] = p.config.PackerBuilderType\n\tp.config.ctx.Data = generatedData\n\n\ttarget, err := interpolate.Render(p.config.OutputPath, &p.config.ctx)\n\tif err != nil {\n\t\treturn nil, false, false, fmt.Errorf(\"Error interpolating output value: %s\", err)\n\t} else {\n\t\tfmt.Println(target)\n\t}\n\n\tnewArtifact := &Artifact{Path: target}\n\n\tif err = os.MkdirAll(filepath.Dir(target), os.FileMode(0755)); err != nil {\n\t\treturn nil, false, false, fmt.Errorf(\n\t\t\t\"Unable to create dir for archive %s: %s\", target, err)\n\t}\n\toutputFile, err := os.Create(target)\n\tif err != nil {\n\t\treturn nil, false, false, fmt.Errorf(\n\t\t\t\"Unable to create archive %s: %s\", target, err)\n\t}\n\tdefer outputFile.Close()\n\n\t// Setup output interface. If we're using compression, output is a\n\t// compression writer. Otherwise it's just a file.\n\tvar output io.WriteCloser\n\terrTmpl := \"error creating %s writer: %s\"\n\tswitch p.config.Algorithm {\n\tcase \"bgzf\":\n\t\tui.Say(fmt.Sprintf(\"Using bgzf compression with %d cores for %s\",\n\t\t\truntime.GOMAXPROCS(-1), target))\n\t\toutput, err = makeBGZFWriter(outputFile, p.config.CompressionLevel)","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/post-processor/compress/post-processor.go#L126-L162","documentation":"PostProcess creates the parent directory of the archive target via os.MkdirAll with mode 0755 before creating the file. This error means the OS refused to create that directory tree (permission denied, path component is a file, read-only filesystem, etc.).","triggerScenarios":"os.MkdirAll(filepath.Dir(target), 0755) fails during PostProcess — e.g. output_path's directory cannot be created because a parent lacks write permission or a component exists as a regular file.","commonSituations":"output_path pointing under a root-owned directory; running packer as a non-privileged user; an existing file where a directory is expected (e.g. 'out' is a file); read-only container volume or mounted output dir.","solutions":["Pre-create the target directory with correct ownership/permissions (mkdir -p)","Ensure no existing file occupies a path segment of the target directory","Run packer with a user that has write access to the output location","Point output_path at a writable volume"],"exampleFix":"// before (unwritable)\noutput = \"/root/output/artifact.tar.gz\"\n// after\noutput = \"./output/artifact.tar.gz\"","handlingStrategy":"validation","validationCode":"dir := filepath.Dir(renderedTarget)\nif fi, err := os.Stat(dir); err == nil && !fi.IsDir() {\n    return fmt.Errorf(\"%s exists and is not a directory\", dir)\n}\nif err := os.MkdirAll(dir, 0755); err != nil {\n    return fmt.Errorf(\"cannot create %s: %w\", dir, err)\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n    var pe *os.PathError\n    if errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission) {\n        // fall back to a writable temp/output directory\n    }\n}","preventionTips":["Pre-create output directories with correct ownership","Ensure no file occupies a directory-name path segment","Run packer as a user with write access to the output location","Avoid read-only mounts for output paths"],"tags":["filesystem","permissions","post-processor"],"backgroundTag":"mkdir-permission-denied","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"}