flipped-aurora/gin-vue-admin · critical

fatal error config file: %w

Error message

fatal error config file: %w

What it means

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.

Source

Thrown at server/core/viper.go:25

	"path/filepath"

	"github.com/flipped-aurora/gin-vue-admin/server/core/internal"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/fsnotify/fsnotify"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
)

// Viper 配置
func Viper() *viper.Viper {
	config := getConfigPath()

	v := viper.New()
	v.SetConfigFile(config)
	v.SetConfigType("yaml")
	err := v.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("fatal error config file: %w", err))
	}
	v.WatchConfig()

	v.OnConfigChange(func(e fsnotify.Event) {
		fmt.Println("config file changed:", e.Name)
		if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {
			fmt.Println(err)
		}
	})
	if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {
		panic(fmt.Errorf("fatal error unmarshal config: %w", err))
	}

	// root 适配性 根据root位置去找到对应迁移位置,保证root路径有效
	global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
	return v
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Read the wrapped viper error: it distinguishes 'Config File Not Found' from parse errors
  2. Ensure config.yaml exists in the directory the server runs from (or fix the path passed to core.Viper)
  3. Validate YAML syntax after edits before restarting
  4. Check file readability for the process user (especially in Docker/K8s with mounted configs)

Example fix

// before
cd /opt && ./server   # config.yaml is in /opt/server
// after
cd /opt/server && ./server
Defensive patterns

Strategy: validation

Validate before calling

if _, err := os.Stat(configPath); err != nil {
    log.Fatalf("config file unreadable before start: %v", err)
}

Try / catch

// recover at main level if desired
defer func() {
    if r := recover(); r != nil {
        log.Fatalf("startup failed: %v", r)
    }
}()

Prevention

When it happens

Trigger: Server startup when config.yaml is absent at the path given to Viper(), unreadable, or contains invalid YAML that viper cannot parse.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/6365ec127644663d. Report an issue: GitHub.