hashicorp/packer · error

file %s is not a HCL file

Error message

file %s is not a HCL file

What it means

formatter.processFile formats only files recognized as HCL2 (.hcl2, .pkr.hcl, .pkrvars.hcl, or variable files per isHcl2FileOrVarFile), or stdin when filename is '-'. Any other filename is rejected with this error rather than being accidentally reformatted.

Source

Thrown at hcl2template/formatter.go:121

				}
			}
		}
	}

	return bytesModified, diags
}

// processFile formats the source contents of filename and return the formatted data.
// overwriting the contents of the original when the f.Write is true; a diff of the changes
// will be outputted if f.ShowDiff is true.
func (f *HCL2Formatter) processFile(filename string) ([]byte, error) {

	if f.Output == nil {
		f.Output = os.Stdout
	}

	if !(filename == "-") && !isHcl2FileOrVarFile(filename) {
		return nil, fmt.Errorf("file %s is not a HCL file", filename)
	}

	var in io.Reader
	var err error

	if filename == "-" {
		in = os.Stdin
		f.ShowDiff = false
	} else {
		in, err = os.Open(filename)
		if err != nil {
			return nil, fmt.Errorf("failed to open %s: %s", filename, err)
		}
	}

	inSrc, err := io.ReadAll(in)
	if err != nil {
		return nil, fmt.Errorf("failed to read %s: %s", filename, err)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Rename the file with a recognized extension, e.g. 'mytemplate.pkr.hcl'
  2. For variable files use '*.pkrvars.hcl'
  3. If formatting JSON templates is intended, use a JSON formatter instead — packer fmt only handles HCL2

Example fix

// before
packer fmt template.txt
// after
mv template.txt template.pkr.hcl
packer fmt template.pkr.hcl
Defensive patterns

Strategy: validation

Validate before calling

func isHclTemplate(p string) bool {
  return strings.HasSuffix(p, ".pkr.hcl") || strings.HasSuffix(p, ".pkr.hcl2") || strings.HasSuffix(p, ".pkrvars.hcl")
}

Try / catch

if err := f.FormatFile(path); err != nil {
  if strings.Contains(err.Error(), "is not a HCL file") { /* skip or rename */ }
}

Prevention

When it happens

Trigger: Calling formatFile/processFile (via 'packer fmt') on a file whose extension is not a recognized HCL2 template or var-file extension.

Common situations: Running 'packer fmt' on JSON templates (.json), shell scripts, or arbitrary files passed by glob; renamed template files missing the .pkr.hcl suffix.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/1236fe5b51b1bd09. Report an issue: GitHub.