You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/**testdata/good_config.toml is a good toml file.Viper sees it initially**/f, err:=os.Open("testdata/good_config.toml")
iferr!=nil {
assert.Fail(suite.T(), err.Error())
}
err=viper.ReadConfig(f)
iferr!=nil {
assert.Fail(suite.T(), err.Error())
}
Expected Behavior
should read the config. When I print the config I expect the contents of the initial file. but I get {}
Actual Behavior
Return an empty config
Steps To Reproduce
No response
Additional Information
Did some initial debugging on this.
At this point all is well.
Here is where things start to hinky
Still dont have a file tto read? Ok says Viper Ill try to find the config file . . . But wait!
Here you can see that configPath has never been set. It still has not set the configFile or pconfigPath despite asking for a file reader that has the name and path of the file. The reult is that it cant find the config file and fails.
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'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:
This seems to come up as a consequence of not specifying the config type. If you read in a file it still needs to be interpreted in some way and since the config type has not been set then getConfigType() resorts to trying to find the file instead and inferring the config type from the file extension. This is (probably) why your working example is successful.
I'll admit it is quirky behavior... perhaps adding a default case here https://github.com/spf13/viper/blob/master/viper.go#L1804 and retur an error would be more suitable since the function assumes that a config type has been set either explicitly using SetConfigType or by using SetConfigFile()+ReadInConfig()
Either way, using SetConfigType should resolve this issue?
Preflight Checklist
Viper Version
1.18.2
Go Version
1.22
Config Source
Files
Format
TOML
Repl.it link
No response
Code reproducing the issue
Expected Behavior
should read the config. When I print the config I expect the contents of the initial file. but I get {}
Actual Behavior
Return an empty config
Steps To Reproduce
No response
Additional Information
Did some initial debugging on this.
At this point all is well.
Here is where things start to hinky
Still dont have a file tto read? Ok says Viper Ill try to find the config file . . . But wait!
Here you can see that configPath has never been set. It still has not set the configFile or pconfigPath despite asking for a file reader that has the name and path of the file. The reult is that it cant find the config file and fails.
BUT!!!
This code works:
So why would I go through all that trouble to use
ReadConfig(f)when I could just go through the same trouble and use ReadInConfig()
?Seems like
ReadConfig(f)
should just ReadConfig fromf
The text was updated successfully, but these errors were encountered: