Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation encourages wrong search order for config files. #1779

Open
7 of 9 tasks
aes opened this issue Mar 12, 2024 · 2 comments
Open
7 of 9 tasks

Documentation encourages wrong search order for config files. #1779

aes opened this issue Mar 12, 2024 · 2 comments
Labels
kind/bug Something isn't working

Comments

@aes
Copy link

aes commented Mar 12, 2024

Preflight Checklist

  • I have searched the issue tracker for an issue that matches the one I want to file, without success.
  • I am not looking for support or already pursued the available support channels without success.
  • I have checked the troubleshooting guide for my problem, without success.

Viper Version

v1.18.2

Go Version

1.21.1

Config Source

Manual set

Format

YAML

Repl.it link

No response

Code reproducing the issue

package main

import (
	"flag"
	"fmt"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

func main() {
	flag.String("source", "", "source of configuration")
	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
	pflag.Parse()
	viper.BindPFlags(pflag.CommandLine)

	viper.SetEnvPrefix("repro")
	viper.BindEnv("source")

	viper.SetDefault("source", "default")

	viper.SetConfigName("config")              // name of config file (without extension)
	viper.SetConfigType("yaml")                // REQUIRED if the config file does not have the extension in the name
	viper.AddConfigPath("/etc/repro/")         // path to look for the config file in
	viper.AddConfigPath("$HOME/.config/repro") // call multiple times to add many search paths
	viper.AddConfigPath(".")                   // optionally look for config in the working directory
	err := viper.ReadInConfig()                // Find and read the config file
	if err != nil {                            // Handle errors reading the config file
		if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
			panic(fmt.Errorf("fatal error config file: %w", err))
		}
	}

	fmt.Println("config source: ", viper.GetString("source"))
}

Expected Behavior

  • flag should override env
  • env should override all files
  • local directory should override user global
  • user global should override system config
  • system config should overide defaults
  • defaults should work

Actual Behavior

Configuration file locations are searched in order specified, which is sane and good, but the documentation suggests the opposite.

Steps To Reproduce

No response

Additional Information

Please reorder these lines.

viper.AddConfigPath("/etc/appname/")   // path to look for the config file in
viper.AddConfigPath("$HOME/.appname")  // call multiple times to add many search paths
viper.AddConfigPath(".")               // optionally look for config in the working directory

Also, consider using "$HOME/.config/appname/" for the user directory one.
(Don't get me started on the single-dash long-args thing, I've given up on golang on that point.)

@aes aes added the kind/bug Something isn't working label Mar 12, 2024
Copy link

👋 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! ❤️

@sagikazarmark
Copy link
Collaborator

@aes I'd be happy to review a PR fixing the order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants