{"record":{"id":"6365ec127644663d","repo":"flipped-aurora/gin-vue-admin","slug":"fatal-error-config-file-w","errorCode":null,"errorMessage":"fatal error config file: %w","messagePattern":"fatal error config file: %w","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"server/core/viper.go","lineNumber":25,"sourceCode":"\t\"path/filepath\"\n\n\t\"github.com/flipped-aurora/gin-vue-admin/server/core/internal\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/global\"\n\t\"github.com/fsnotify/fsnotify\"\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/spf13/viper\"\n)\n\n// Viper 配置\nfunc Viper() *viper.Viper {\n\tconfig := getConfigPath()\n\n\tv := viper.New()\n\tv.SetConfigFile(config)\n\tv.SetConfigType(\"yaml\")\n\terr := v.ReadInConfig()\n\tif err != nil {\n\t\tpanic(fmt.Errorf(\"fatal error config file: %w\", err))\n\t}\n\tv.WatchConfig()\n\n\tv.OnConfigChange(func(e fsnotify.Event) {\n\t\tfmt.Println(\"config file changed:\", e.Name)\n\t\tif err = v.Unmarshal(&global.GVA_CONFIG); err != nil {\n\t\t\tfmt.Println(err)\n\t\t}\n\t})\n\tif err = v.Unmarshal(&global.GVA_CONFIG); err != nil {\n\t\tpanic(fmt.Errorf(\"fatal error unmarshal config: %w\", err))\n\t}\n\n\t// root 适配性 根据root位置去找到对应迁移位置,保证root路径有效\n\tglobal.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs(\"..\")\n\treturn v\n}\n","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/core/viper.go#L7-L43","documentation":"core.Viper bootstraps the global config with spf13/viper at server startup. If v.ReadInConfig fails (file missing, unreadable, malformed YAML), the function panics with this error because the server cannot start without configuration.","triggerScenarios":"Server startup when config.yaml is absent at the path given to Viper(), unreadable, or contains invalid YAML that viper cannot parse.","commonSituations":"Running the server binary from the wrong directory so the relative config path is wrong; deploying without copying config.yaml; YAML syntax error after editing; file permission issues in containers.","solutions":["Read the wrapped viper error: it distinguishes 'Config File Not Found' from parse errors","Ensure config.yaml exists in the directory the server runs from (or fix the path passed to core.Viper)","Validate YAML syntax after edits before restarting","Check file readability for the process user (especially in Docker/K8s with mounted configs)"],"exampleFix":"// before\ncd /opt && ./server   # config.yaml is in /opt/server\n// after\ncd /opt/server && ./server","handlingStrategy":"validation","validationCode":"if _, err := os.Stat(configPath); err != nil {\n    log.Fatalf(\"config file unreadable before start: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"// recover at main level if desired\ndefer func() {\n    if r := recover(); r != nil {\n        log.Fatalf(\"startup failed: %v\", r)\n    }\n}()","preventionTips":["Always start the server from its working directory or pass an absolute config path","Validate config.yaml with yamllint in the deploy pipeline","Ensure configs are copied/mounted correctly in Docker/K8s"],"tags":["viper","config","panic","startup"],"backgroundTag":"config-file-not-found","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}