spf13 / viper

Go configuration with fangs
MIT License
26.67k stars 2.01k forks source link

Viper cannot read the configuration file with GBK encoding rules and Chinese comments #1454

Open ZGGSONG opened 1 year ago

ZGGSONG commented 1 year ago

Preflight Checklist

Viper Version

1.13.0

Go Version

1.18.3

Config Source

Files

Format

YAML

Repl.it link

No response

Code reproducing the issue

package main

import (
    "encoding/json"
    "log"
    "os"

    "github.com/spf13/viper"
)

func main() {
    viper.SetConfigName("config")
    viper.SetConfigType("yml")
    viper.AddConfigPath("./config")
    viper.AddConfigPath("../config")
    err := viper.ReadInConfig()
    if err != nil {
        log.Printf("未找到配置,请先添加配置")
        os.Exit(1)
    }
    log.Printf("Loaded Config Success...")

    var config Config
    config.Url = viper.GetString("server.url")
    var outPut, _ = json.Marshal(config)
    log.Println(string(outPut))
}

type Config struct {
    Url string
}

Expected Behavior

The desired effect is to be able to read the configuration file without problems.

Actual Behavior

The reality is that it doesn't read properly under certain circumstances and it bothered me for a long time before I got it fixed.

Steps To Reproduce

  1. Set the profile encoding rule to GBK (or GB2312/GB18030).
  2. Add Chinese comments to the configuration file (e.g.: #测试).
  3. In this case, the configuration file cannot be read.
  4. However, this is not a problem with the utf-8 encoding rules with Chinese comments.

Additional Information

UTF-8

GBK

github-actions[bot] commented 1 year ago

👋 Thanks for reporting!

A maintainer will take a look at your issue shortly. 👀

In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.

⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9

📣 If you've already given us your feedback, you can still help by spreading the news, either by sharing the above link or telling people about this on Twitter:

https://twitter.com/sagikazarmark/status/1306904078967074816

Thank you! ❤️