{"id":"b2b3f9cd28bd5b03","repo":"spf13/viper","slug":"missing-configuration-for-configpath","errorCode":null,"errorMessage":"missing configuration for 'configPath'","messagePattern":"missing configuration for 'configPath'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"viper.go","lineNumber":1704,"sourceCode":"// WriteConfig writes the current configuration to a file.\nfunc WriteConfig() error { return v.WriteConfig() }\n\n// WriteConfig writes the current configuration to a file.\nfunc (v *Viper) WriteConfig() error {\n\tfilename, err := v.getConfigFile()\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn v.writeConfig(filename, true)\n}\n\n// SafeWriteConfig writes current configuration to file only if the file does not exist.\nfunc SafeWriteConfig() error { return v.SafeWriteConfig() }\n\n// SafeWriteConfig writes current configuration to file only if the file does not exist.\nfunc (v *Viper) SafeWriteConfig() error {\n\tif len(v.configPaths) < 1 {\n\t\treturn errors.New(\"missing configuration for 'configPath'\")\n\t}\n\treturn v.SafeWriteConfigAs(filepath.Join(v.configPaths[0], v.configName+\".\"+v.configType))\n}\n\n// WriteConfigAs writes current configuration to a given filename.\nfunc WriteConfigAs(filename string) error { return v.WriteConfigAs(filename) }\n\n// WriteConfigAs writes current configuration to a given filename.\nfunc (v *Viper) WriteConfigAs(filename string) error {\n\treturn v.writeConfig(filename, true)\n}\n\n// WriteConfigTo writes current configuration to an [io.Writer].\nfunc WriteConfigTo(w io.Writer) error { return v.WriteConfigTo(w) }\n\n// WriteConfigTo writes current configuration to an [io.Writer].\nfunc (v *Viper) WriteConfigTo(w io.Writer) error {\n\tformat := strings.ToLower(v.getConfigType())","sourceCodeStart":1686,"sourceCodeEnd":1722,"githubUrl":"https://github.com/spf13/viper/blob/528f7416c4b56a4948673984b190bf8713f0c3c4/viper.go#L1686-L1722","documentation":"Returned by Viper.SafeWriteConfig (viper.go:1702-1707) when len(v.configPaths) < 1. SafeWriteConfig builds the target path as filepath.Join(configPaths[0], configName+\".\"+configType), so it requires at least one AddConfigPath call. Note AddConfigPath is a logged no-op when a custom Finder is set (viper.go:438-441), so configPaths stays empty in that case.","triggerScenarios":"Calling v.SafeWriteConfig() without any prior v.AddConfigPath(...), or after setting a custom finder (NewWithOptions(WithFinder(f)) / v.SetFinder(f)) which makes AddConfigPath ineffective.","commonSituations":"Migrated to finder-based config discovery but kept SafeWriteConfig in a bootstrap/init path; test helpers that forget to AddConfigPath; init code that calls SafeWriteConfig before any path setup.","solutions":["Call v.AddConfigPath(\"/etc/myapp\") (or similar) before v.SafeWriteConfig().","If you use a custom Finder, switch to v.SafeWriteConfigAs(\"/etc/myapp/config.yaml\") which takes an explicit absolute path and ignores configPaths.","Ensure configType and configName are also set so the joined filename is valid."],"exampleFix":"// before\nv := New()\nv.SetConfigName(\"app\")\nv.SetConfigType(\"yaml\")\n_ = v.SafeWriteConfig() // -> missing configuration for 'configPath'\n\n// after\nv := New()\nv.SetConfigName(\"app\")\nv.SetConfigType(\"yaml\")\nv.AddConfigPath(\"/etc/myapp\")\n_ = v.SafeWriteConfig()\n\n// alt for custom finder users\n_ = v.SafeWriteConfigAs(\"/etc/myapp/app.yaml\")","handlingStrategy":"validation","validationCode":"// configPaths is unexported; the only way to populate it is AddConfigPath.\n// Validate by ensuring you call AddConfigPath, or skip SafeWriteConfig entirely.\nv.AddConfigPath(\"/etc/myapp\") // required before SafeWriteConfig\nv.SetConfigName(\"app\")\nv.SetConfigType(\"yaml\")\n\n// If using a custom Finder (which makes AddConfigPath a no-op), prefer:\n//   err := v.SafeWriteConfigAs(\"/etc/myapp/app.yaml\")","typeGuard":null,"tryCatchPattern":"if err := v.SafeWriteConfig(); err != nil {\n    if strings.Contains(err.Error(), \"missing configuration for 'configPath'\") {\n        // fall back to an explicit path\n        err = v.SafeWriteConfigAs(\"/etc/myapp/app.yaml\")\n    }\n}","preventionTips":["When using a custom Finder, never call SafeWriteConfig; use SafeWriteConfigAs with an absolute path.","Centralize config setup (AddConfigPath + SetConfigName + SetConfigType) in one builder so writes always have a path.","Assert in tests that SafeWriteConfig succeeds after your setup."],"tags":["config-write","setup","paths","finder"],"analyzedSha":"528f7416c4b56a4948673984b190bf8713f0c3c4","analyzedAt":"2026-08-04T21:46:12.352Z","schemaVersion":2}