{"record":{"id":"67e0417bab000ec7","repo":"hashicorp/packer","slug":"failed-to-open-s-s","errorCode":null,"errorMessage":"failed to open %s: %s","messagePattern":"failed to open (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hcl2template/formatter.go","lineNumber":133,"sourceCode":"\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)\n\t}\n\n\t_, diags := f.parser.ParseHCL(inSrc, filename)\n\tif diags.HasErrors() {\n\t\treturn nil, multierror.Append(nil, diags.Errs()...)\n\t}\n\n\toutSrc := hclwrite.Format(inSrc)\n\n\tif bytes.Equal(inSrc, outSrc) {\n\t\tif filename == \"-\" {\n\t\t\t_, _ = f.Output.Write(outSrc)","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/hcl2template/formatter.go#L115-L151","documentation":"processFile opens the target file with os.Open before parsing; if the open fails (missing file, permission denied, path is a directory), it wraps the OS error as 'failed to open %s: %s'. This happens before any HCL parsing occurs.","triggerScenarios":"formatFile/processFile invoked with a filename that does not exist, lacks read permission, or is a directory (and filename is not '-', which uses stdin).","commonSituations":"Typo'd paths on 'packer fmt'; globbing patterns that matched directories; files deleted between discovery and formatting; restrictive permissions after checkout on Windows/CI.","solutions":["Check the path exists with 'ls <file>' and fix typos","Verify read permissions for the current user","Ensure the argument is a regular file, not a directory","If the path came from a glob, tighten the pattern to files only"],"exampleFix":"// before\npacker fmt ./templates/   // directory\n// after\npacker fmt ./templates/*.pkr.hcl","handlingStrategy":"validation","validationCode":"if info, err := os.Stat(path); err != nil || info.IsDir() { /* resolve before calling packer fmt */ }","typeGuard":null,"tryCatchPattern":"if err := f.FormatFile(path); err != nil {\n  var pe *fs.PathError\n  if errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrNotExist) { /* handle missing file */ }\n}","preventionTips":["Verify file existence and permissions before formatting","Glob for files, not directories","Avoid formatting paths on flaky network mounts"],"tags":["filesystem","io","formatter","file-open"],"backgroundTag":"file-open-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"}