{"record":{"id":"2f36e64d73e4264e","repo":"hashicorp/packer","slug":"error-creating-s-writer-s","errorCode":null,"errorMessage":"error creating %s writer: %s","messagePattern":"error creating (.+?) writer: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"post-processor/compress/post-processor.go","lineNumber":164,"sourceCode":"\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)\n\t\tif err != nil {\n\t\t\treturn nil, false, false, fmt.Errorf(errTmpl, p.config.Algorithm, err)\n\t\t}\n\t\tdefer output.Close()\n\tcase \"bzip2\":\n\t\tui.Say(fmt.Sprintf(\"Using bzip2 compression with 1 core for %s (library does not support MT)\",\n\t\t\ttarget))\n\t\toutput, err = makeBZIP2Writer(outputFile, p.config.CompressionLevel)\n\t\tif err != nil {\n\t\t\treturn nil, false, false, fmt.Errorf(errTmpl, p.config.Algorithm, err)\n\t\t}\n\t\tdefer output.Close()\n\tcase \"lz4\":\n\t\tui.Say(fmt.Sprintf(\"Using lz4 compression with %d cores for %s\",\n\t\t\truntime.GOMAXPROCS(-1), target))\n\t\toutput, err = makeLZ4Writer(outputFile, p.config.CompressionLevel)\n\t\tif err != nil {\n\t\t\treturn nil, false, false, fmt.Errorf(errTmpl, p.config.Algorithm, err)\n\t\t}\n\t\tdefer output.Close()","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/post-processor/compress/post-processor.go#L146-L182","documentation":"The bgzf compression writer could not be created. makeBGZFWriter returns ErrInvalidCompressionLevel (or a bgzf library error) when bgzf.NewWriterLevel rejects the configured compression level or writer parameters, and PostProcess wraps it via the errTmpl message naming the algorithm.","triggerScenarios":"Algorithm is \"bgzf\" and bgzf.NewWriterLevel(outputFile, p.config.CompressionLevel, runtime.GOMAXPROCS(-1)) returns an error — practically always an out-of-range compression_level (-2 or >9).","commonSituations":"Setting compression_level outside the gzip range for bgzf; passing a string/invalid value that survived config decoding as a bad integer; copy-pasted level from an lz4-specific config.","solutions":["Set compression_level to an integer in -1..9 (or remove it to use the default)","Run packer validate to catch the bad level before the build","If using bgzf, confirm no config override injects a level outside the flate range"],"exampleFix":"// before\nalgorithm = \"bgzf\"\ncompression_level = 12\n// after\nalgorithm = \"bgzf\"\ncompression_level = 9","handlingStrategy":"validation","validationCode":"if cfg.CompressionLevel < -1 || cfg.CompressionLevel > 9 {\n    return fmt.Errorf(\"bgzf requires compression_level in -1..9, got %d\", cfg.CompressionLevel)\n}","typeGuard":null,"tryCatchPattern":"out, err := makeBGZFWriter(outputFile, cfg.CompressionLevel)\nif errors.Is(err, compress.ErrInvalidCompressionLevel) {\n    // retry with default level\n    out, err = makeBGZFWriter(outputFile, -1)\n}","preventionTips":["Keep compression_level within -1..9 when using bgzf","Run packer validate to catch bad levels pre-build","Omit compression_level to use defaults"],"tags":["compression","bgzf","configuration"],"backgroundTag":"invalid-compression-level","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"}