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

Current directory used for config if no other specified directories exist #73

Closed
didenko opened this issue May 22, 2015 · 2 comments
Closed

Comments

@didenko
Copy link
Collaborator

didenko commented May 22, 2015

It does not seem to be a problem if no directories were added with AddConfigPath - CWD is probably a good assumption.

However, if config directories are misconfigured, or, more importantly, get accidentally erased later, then loading configuration from CWD is a surprise behaviour. I would expect a failure at that point.

Code example:

package main

import (
    "io/ioutil"
    "os"

    "github.com/spf13/viper"
)

const configFile = "surprise_inside"
const configFileFull = configFile + ".toml"

func main() {

    defer os.Remove(configFileFull)

    // Although probably distinct use cases, viper
    // reads the config in cwd regardless of AddConfigPath
    // being commented out or not
    viper.AddConfigPath("this_junk_does_not_exist")

    viper.SetConfigName(configFile)
    viper.SetDefault("key", "Feels good!")

    err := viper.ReadInConfig()
    if err != nil {
        println("Fatal error reading config file - good")
    } else {
        println(viper.GetString("key"))
    }

    println("Writing " + configFileFull)

    err = ioutil.WriteFile(
        configFileFull,
        []byte("key = \"Surprise!\"\n"),
        0600)
    if err != nil {
        println("Fatal error writing config file - bad")
    } else {

        err = viper.ReadInConfig()
        if err != nil {
            println("Fatal error reading config file - bad")
        } else {
            println(viper.GetString("key"))
        }
    }
}
@didenko
Copy link
Collaborator Author

didenko commented May 27, 2015

The two primary scenarios I see are:

  1. A config file not found in specified locations
  2. No locations specified and a config file not found in CWD

It seems to be a separate issue, how to treat the lack of a config file - as an error or as a suppressed condition.

This issue deals with that CWD should not be checked for config file unless either explicitly included in search locations. If no locations specified to begin with it may be a reasonable behaviour to check CWD. Although still a debatable use case it is not worth changing today, but would be great to hear opinions about.

@didenko
Copy link
Collaborator Author

didenko commented Aug 2, 2015

After fixing this issue the code which intentionally or not (like #104) relied on the default search in the current directory will break. To get the pre-fix functionality back in your application, add the following call to the proper viper callee after adding all other search paths to it:

viper.AddConfigPath(".")

anthonyfok added a commit to anthonyfok/hugo that referenced this issue Aug 19, 2015
As of 2015-08-16, Viper no longer searches the CWD
for config file by default to avoid unintended surprises,
but Hugo relies on the original behaviour.

Fixed by calling

    viper.AddConfigPath(".")

at the appropriate place.

See spf13/viper#73 for more information.

Fixes gohugoio#1363
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
As of 2015-08-16, Viper no longer searches the CWD
for config file by default to avoid unintended surprises,
but Hugo relies on the original behaviour.

Fixed by calling

    viper.AddConfigPath(".")

at the appropriate place.

See spf13/viper#73 for more information.

Fixes gohugoio#1363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant