{"record":{"id":"d58c10e3a21b3737","repo":"nats-io/nats-server","slug":"error-opening-config-file-v","errorCode":null,"errorMessage":"error opening config file: %v","messagePattern":"error opening config file: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conf/parse.go","lineNumber":92,"sourceCode":"\t\treturn nil, err\n\t}\n\treturn p.mapping, nil\n}\n\n// ParseWithChecks is equivalent to Parse but runs in pedantic mode.\nfunc ParseWithChecks(data string) (map[string]any, error) {\n\tp, err := parse(data, \"\", true)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn p.mapping, nil\n}\n\n// ParseFile is a helper to open file, etc. and parse the contents.\nfunc ParseFile(fp string) (map[string]any, error) {\n\tdata, err := os.ReadFile(fp)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error opening config file: %v\", err)\n\t}\n\n\tp, err := parse(string(data), fp, false)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn p.mapping, nil\n}\n\n// ParseFileWithChecks is equivalent to ParseFile but runs in pedantic mode.\nfunc ParseFileWithChecks(fp string) (map[string]any, error) {\n\tdata, err := os.ReadFile(fp)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tp, err := parse(string(data), fp, true)\n\tif err != nil {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/conf/parse.go#L74-L110","documentation":"conf.ParseFile reads the config file with os.ReadFile and wraps any read failure in \"error opening config file: %v\". The library throws it whenever the file cannot be opened — missing path, permission problems, or a directory instead of a file. It is the first error the parser can produce and the underlying OS error is included in the message.","triggerScenarios":"Calling conf.ParseFile(path) (or starting the NATS server with -c) with a path that does not exist, a path without read permission, or a directory path; includes resolving to missing files.","commonSituations":"Typo in the -c flag path; running the server as a different user (systemd service) lacking read access; relative path resolved from the wrong working directory; mounted config not yet present in a container.","solutions":["Verify the file path exists: ls -l <path>, and use an absolute path","Fix file permissions so the server's user can read the config","Check that the path points to a file, not a directory","If running under systemd/container, confirm the config is mounted and the WorkingDirectory is correct"],"exampleFix":"// before\nconf, err := conf.ParseFile(\"nats.conf\")\n// after\nif _, statErr := os.Stat(\"/etc/nats/nats.conf\"); statErr != nil {\n    log.Fatalf(\"config missing: %v\", statErr)\n}\nconf, err := conf.ParseFile(\"/etc/nats/nats.conf\")","handlingStrategy":"try-catch","validationCode":"if fp == \"\" {\n    return errors.New(\"config path required\")\n}\nif fi, err := os.Stat(fp); err != nil {\n    return fmt.Errorf(\"config not accessible: %w\", err)\n} else if fi.IsDir() {\n    return fmt.Errorf(\"%s is a directory\", fp)\n}","typeGuard":null,"tryCatchPattern":"cfg, err := conf.ParseFile(fp)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"error opening config file\") {\n        log.Fatalf(\"cannot read config %q: %v — check path, permissions and working directory\", fp, err)\n    }\n    return err\n}","preventionTips":["Use absolute config paths in service units and containers","Ensure the runtime user has read permission on the config","Mount/verify configs before server start (readiness check)","Wrap ParseFile errors to distinguish I/O failures from parse failures"],"tags":["config","file-io","startup","nats-server"],"backgroundTag":"config-file-not-found","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}