{"record":{"id":"a769a43078e54542","repo":"hashicorp/nomad","slug":"failed-to-open-file-s-w","errorCode":null,"errorMessage":"Failed to open file %s: %w","messagePattern":"Failed to open file (.+?): %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/fmt.go","lineNumber":185,"sourceCode":"\tif len(f.paths) == 0 {\n\t\tf.processFile(stdinPath, f.stdin)\n\t\treturn\n\t}\n\n\tfor _, path := range f.paths {\n\t\tinfo, err := os.Stat(path)\n\t\tif err != nil {\n\t\t\tf.appendError(fmt.Errorf(\"No file or directory at %s\", path))\n\t\t\tcontinue\n\t\t}\n\n\t\tif info.IsDir() {\n\t\t\tf.processDir(path)\n\t\t} else {\n\t\t\tif isNomadFile(info) {\n\t\t\t\tfp, err := os.Open(path)\n\t\t\t\tif err != nil {\n\t\t\t\t\tf.appendError(fmt.Errorf(\"Failed to open file %s: %w\", path, err))\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tf.processFile(path, fp)\n\n\t\t\t\tfp.Close()\n\t\t\t} else {\n\t\t\t\tf.appendError(fmt.Errorf(\"Only .nomad and .hcl files can be processed using nomad fmt\"))\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunc (f *FormatCommand) processDir(path string) {\n\tentries, err := os.ReadDir(path)\n\tif err != nil {\n\t\tf.appendError(fmt.Errorf(\"Failed to list directory %s\", path))","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/fmt.go#L167-L203","documentation":"When nomad fmt encounters a regular file recognized as a Nomad file (isNomadFile), it opens it with os.Open. If the open fails (permission denied, race where the file vanished after stat, too many open files), it reports \"Failed to open file %s: %w\" with the wrapped cause and skips the file.","triggerScenarios":"fmt → os.Open(path) failing after a successful os.Stat: file permissions changed, file deleted between stat and open, or EMFILE when processing huge directory trees.","commonSituations":"Files owned by another user with restrictive modes; files removed concurrently by a build process; recursive fmt over enormous trees hitting the OS file-descriptor limit.","solutions":["Check/fix file permissions (read access for the invoking user)","Re-run fmt; if a race deleted the file, ensure no concurrent process removes files during formatting","For 'too many open files', raise ulimit -n or run fmt on smaller directory subsets"],"exampleFix":"// before\nnomad fmt /etc/nomad.d/  # Failed to open file /etc/nomad.d/jobs.hcl: permission denied\n// after\nsudo chmod o+r /etc/nomad.d/jobs.hcl   # or run nomad fmt as a user with read access","handlingStrategy":"try-catch","validationCode":"f, err := os.OpenFile(path, os.O_RDONLY, 0)\nif err != nil {\n    if os.IsPermission(err) { /* fix perms or skip */ }\n}","typeGuard":null,"tryCatchPattern":"fp, err := os.Open(path)\nif err != nil {\n    if os.IsPermission(err) {\n        // report and skip or escalate permissions\n    } else if errors.Is(err, syscall.EMFILE) {\n        // raise ulimit or batch the directory walk\n    }\n    return err\n}\ndefer fp.Close()","preventionTips":["Run fmt as a user with read access to all target files","Avoid concurrent deletion of files during formatting","Raise the open-file ulimit for large recursive runs"],"tags":["cli","filesystem","permissions","fmt"],"backgroundTag":"file-open-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}