{"record":{"id":"1236fe5b51b1bd09","repo":"hashicorp/packer","slug":"file-s-is-not-a-hcl-file","errorCode":null,"errorMessage":"file %s is not a HCL file","messagePattern":"file (.+?) is not a HCL file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/formatter.go","lineNumber":121,"sourceCode":"\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn bytesModified, diags\n}\n\n// processFile formats the source contents of filename and return the formatted data.\n// overwriting the contents of the original when the f.Write is true; a diff of the changes\n// will be outputted if f.ShowDiff is true.\nfunc (f *HCL2Formatter) processFile(filename string) ([]byte, error) {\n\n\tif f.Output == nil {\n\t\tf.Output = os.Stdout\n\t}\n\n\tif !(filename == \"-\") && !isHcl2FileOrVarFile(filename) {\n\t\treturn nil, fmt.Errorf(\"file %s is not a HCL file\", filename)\n\t}\n\n\tvar in io.Reader\n\tvar err error\n\n\tif filename == \"-\" {\n\t\tin = os.Stdin\n\t\tf.ShowDiff = false\n\t} else {\n\t\tin, err = os.Open(filename)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to open %s: %s\", filename, err)\n\t\t}\n\t}\n\n\tinSrc, err := io.ReadAll(in)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read %s: %s\", filename, err)","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/formatter.go#L103-L139","documentation":"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.","triggerScenarios":"Calling formatFile/processFile (via 'packer fmt') on a file whose extension is not a recognized HCL2 template or var-file extension.","commonSituations":"Running 'packer fmt' on JSON templates (.json), shell scripts, or arbitrary files passed by glob; renamed template files missing the .pkr.hcl suffix.","solutions":["Rename the file with a recognized extension, e.g. 'mytemplate.pkr.hcl'","For variable files use '*.pkrvars.hcl'","If formatting JSON templates is intended, use a JSON formatter instead — packer fmt only handles HCL2"],"exampleFix":"// before\npacker fmt template.txt\n// after\nmv template.txt template.pkr.hcl\npacker fmt template.pkr.hcl","handlingStrategy":"validation","validationCode":"func isHclTemplate(p string) bool {\n  return strings.HasSuffix(p, \".pkr.hcl\") || strings.HasSuffix(p, \".pkr.hcl2\") || strings.HasSuffix(p, \".pkrvars.hcl\")\n}","typeGuard":null,"tryCatchPattern":"if err := f.FormatFile(path); err != nil {\n  if strings.Contains(err.Error(), \"is not a HCL file\") { /* skip or rename */ }\n}","preventionTips":["Always name templates with the .pkr.hcl suffix","Exclude non-HCL files from fmt globs","Convert JSON templates to HCL2 before formatting"],"tags":["formatter","hcl2","file-extension","fmt"],"backgroundTag":"not-an-hcl-file","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"}