{"record":{"id":"507502ec0731245c","repo":"hashicorp/packer","slug":"failed-to-generate-diff-for-s-s","errorCode":null,"errorMessage":"failed to generate diff for %s: %s","messagePattern":"failed to generate diff for (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"hcl2template/formatter.go","lineNumber":175,"sourceCode":"\tif filename != \"-\" {\n\t\ts := []byte(fmt.Sprintf(\"%s\\n\", filename))\n\t\t_, _ = f.Output.Write(s)\n\t}\n\n\tif f.Write {\n\t\tif filename == \"-\" {\n\t\t\t_, _ = f.Output.Write(outSrc)\n\t\t} else {\n\t\t\tif err := os.WriteFile(filename, outSrc, 0644); err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t}\n\t}\n\n\tif f.ShowDiff {\n\t\tdiff, err := bytesDiff(inSrc, outSrc, filename)\n\t\tif err != nil {\n\t\t\treturn outSrc, fmt.Errorf(\"failed to generate diff for %s: %s\", filename, err)\n\t\t}\n\t\t_, _ = f.Output.Write(diff)\n\t}\n\n\treturn outSrc, nil\n}\n\n// shouldIgnoreFile returns true if the given filename (which must not have a\n// directory path ahead of it) should be ignored as e.g. an editor swap file.\nfunc (f *HCL2Formatter) shouldIgnoreFile(name string) bool {\n\treturn strings.HasPrefix(name, \".\") || // Unix-like hidden files\n\t\tstrings.HasSuffix(name, \"~\") || // vim\n\t\tstrings.HasPrefix(name, \"#\") && strings.HasSuffix(name, \"#\") // emacs\n}\n\n// bytesDiff returns the unified diff of b1 and b2\n// Shamelessly copied from Terraform's fmt command.\nfunc bytesDiff(b1, b2 []byte, path string) (data []byte, err error) {","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/formatter.go#L157-L193","documentation":"Packer's HCL2 formatter (packer fmt) failed to generate the unified diff between the original and formatted template. bytesDiff returned an error while comparing inSrc and outSrc after formatting succeeded, so the formatter aborts that file with this wrapped message instead of printing a diff.","triggerScenarios":"Running `packer fmt -diff` on a template where the internal diff generation fails (e.g. an I/O or encoding error in bytesDiff's diff lib while comparing source bytes for filename).","commonSituations":"Using `packer fmt --diff` in CI or editors with unusual file encodings or unreadable/odd template files; rarely seen since both buffers are in memory and diff errors are uncommon.","solutions":["Re-run `packer fmt` without -diff to confirm the file still formats correctly","Check the wrapped underlying error (the %s suffix) for the real cause","Re-encode the template file as plain UTF-8 text and retry `packer fmt -diff`","If it persists, upgrade Packer; if reproducible, file an issue with the template"],"exampleFix":"// before\npacker fmt -diff template.pkr.hcl   // fails: failed to generate diff for ...\n// after\npacker fmt template.pkr.hcl         // formats without needing the diff path","handlingStrategy":"fallback","validationCode":"// before running packer fmt -diff\nfile template.pkr.hcl   # confirm plain text, UTF-8\niconv -f UTF-8 -t UTF-8 template.pkr.hcl >/dev/null && echo ok","typeGuard":null,"tryCatchPattern":"// CI: fall back to plain fmt if diff generation fails\nif ! packer fmt -diff -check .; then\n  packer fmt .\nfi","preventionTips":["Keep templates plain UTF-8 text","Run `packer fmt` regularly so diffs stay small","Pin a recent Packer version where formatter issues are fixed"],"tags":["hcl2","fmt","diff"],"backgroundTag":"fmt-diff-generation-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"}