hashicorp/packer · warning
failed to generate diff for %s: %s
Error message
failed to generate diff for %s: %s
What it means
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.
Source
Thrown at hcl2template/formatter.go:175
if filename != "-" {
s := []byte(fmt.Sprintf("%s\n", filename))
_, _ = f.Output.Write(s)
}
if f.Write {
if filename == "-" {
_, _ = f.Output.Write(outSrc)
} else {
if err := os.WriteFile(filename, outSrc, 0644); err != nil {
return nil, err
}
}
}
if f.ShowDiff {
diff, err := bytesDiff(inSrc, outSrc, filename)
if err != nil {
return outSrc, fmt.Errorf("failed to generate diff for %s: %s", filename, err)
}
_, _ = f.Output.Write(diff)
}
return outSrc, nil
}
// shouldIgnoreFile returns true if the given filename (which must not have a
// directory path ahead of it) should be ignored as e.g. an editor swap file.
func (f *HCL2Formatter) shouldIgnoreFile(name string) bool {
return strings.HasPrefix(name, ".") || // Unix-like hidden files
strings.HasSuffix(name, "~") || // vim
strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs
}
// bytesDiff returns the unified diff of b1 and b2
// Shamelessly copied from Terraform's fmt command.
func bytesDiff(b1, b2 []byte, path string) (data []byte, err error) {View on GitHub (pinned to eb36e3c3e4)
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
Example fix
// before packer fmt -diff template.pkr.hcl // fails: failed to generate diff for ... // after packer fmt template.pkr.hcl // formats without needing the diff path
Defensive patterns
Strategy: fallback
Validate before calling
// before running packer fmt -diff file template.pkr.hcl # confirm plain text, UTF-8 iconv -f UTF-8 -t UTF-8 template.pkr.hcl >/dev/null && echo ok
Try / catch
// CI: fall back to plain fmt if diff generation fails if ! packer fmt -diff -check .; then packer fmt . fi
Prevention
- Keep templates plain UTF-8 text
- Run `packer fmt` regularly so diffs stay small
- Pin a recent Packer version where formatter issues are fixed
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- file %s is not a HCL file
- must have at least one character
- dots are not allowed
- cannot use multiple consecutive dashes
- must contain only letters, digits, and dashes, and may not u
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/507502ec0731245c.
Report an issue: GitHub.