{"record":{"id":"08efb4663ba65d5f","repo":"hashicorp/packer","slug":"errinvalidcompressionlevel","errorCode":"ErrInvalidCompressionLevel","errorMessage":"Invalid compression level. Expected an integer from -1 to 9.","messagePattern":"Invalid compression level\\. Expected an integer from -1 to 9\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"post-processor/compress/post-processor.go","lineNumber":34,"sourceCode":"\t\"regexp\"\n\t\"runtime\"\n\n\t\"github.com/biogo/hts/bgzf\"\n\t\"github.com/dsnet/compress/bzip2\"\n\t\"github.com/hashicorp/hcl/v2/hcldec\"\n\t\"github.com/hashicorp/packer-plugin-sdk/common\"\n\tpackersdk \"github.com/hashicorp/packer-plugin-sdk/packer\"\n\t\"github.com/hashicorp/packer-plugin-sdk/template/config\"\n\t\"github.com/hashicorp/packer-plugin-sdk/template/interpolate\"\n\t\"github.com/klauspost/pgzip\"\n\t\"github.com/pierrec/lz4/v4\"\n\t\"github.com/ulikunitz/xz\"\n)\n\nvar (\n\t// ErrInvalidCompressionLevel is returned when the compression level passed\n\t// to gzip is not in the expected range. See compress/flate for details.\n\tErrInvalidCompressionLevel = fmt.Errorf(\n\t\t\"Invalid compression level. Expected an integer from -1 to 9.\")\n\n\tErrWrongInputCount = fmt.Errorf(\n\t\t\"Can only have 1 input file when not using tar/zip\")\n\n\tfilenamePattern = regexp.MustCompile(`(?:\\.([a-z0-9]+))`)\n)\n\ntype Config struct {\n\tcommon.PackerConfig `mapstructure:\",squash\"`\n\n\t// Fields from config file\n\tOutputPath       string `mapstructure:\"output\"`\n\tFormat           string `mapstructure:\"format\"`\n\tCompressionLevel int    `mapstructure:\"compression_level\"`\n\n\t// Derived fields\n\tArchive   string","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/post-processor/compress/post-processor.go#L16-L52","documentation":"ErrInvalidCompressionLevel is a sentinel error returned by the compress post-processor's writer factories when the configured compression level is not accepted by the underlying compressor. Despite the message mentioning -1..9 (gzip/flate range), it is also returned by bgzf and lz4 when their level mapping fails.","triggerScenarios":"1) makeBGZFWriter: bgzf.NewWriterLevel rejects the CompressionLevel value. 2) makeLZ4Writer: CompressionLevel is not a key in the levels map (e.g. 10, or a value outside 0-9 for lz4 mapping). 3) makePgzipWriter: pgzip writer level out of gzip range.","commonSituations":"Setting compression_level to a value valid for one algorithm but not another (e.g. lz4 with level 10, or negative values other than -1); HCL config typo; copy-pasting a gzip-level value into an lz4/bgzf config.","solutions":["Set compression_level to an integer in -1..9 (or omit it for the default)","Check which algorithm you configured; confirm the level is valid for that specific algorithm (lz4 accepts only values present in its levels map)","Re-run packer validate on the template"],"exampleFix":"// before\ncompression_level = 10\n// after\ncompression_level = 6","handlingStrategy":"validation","validationCode":"level := cfg.CompressionLevel // int\nif level < -1 || level > 9 {\n    return fmt.Errorf(\"compression_level %d out of range -1..9\", level)\n}","typeGuard":null,"tryCatchPattern":"out, err := postProcessor.Configure(cfg)\nif errors.Is(err, compress.ErrInvalidCompressionLevel) {\n    // fall back to a safe default level\n    cfg.CompressionLevel = -1\n    out, err = postProcessor.Configure(cfg)\n}","preventionTips":["Keep compression_level within -1..9 for gzip-family algorithms","Re-check the level whenever you change the algorithm","Omit compression_level to use library defaults","Run packer validate before builds"],"tags":["configuration","compression","validation"],"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"}