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

config file not found #390

Open
gaoshengL opened this issue Sep 22, 2017 · 34 comments · Fixed by #722
Open

config file not found #390

gaoshengL opened this issue Sep 22, 2017 · 34 comments · Fixed by #722

Comments

@gaoshengL
Copy link

hello,i code the example:
viper.SetConfigType("yaml")
//viper.SetConfigFile("/etc/config.yaml")
viper.SetConfigName("config.yaml")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
or
viper.SetConfigType("yaml")
viper.SetConfigFile("config.yaml")
//viper.SetConfigName("config.yaml")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
i'm sure that i have config.yaml in /etc dirctionary,but the outputs of these code are:
Config File "config.yaml" Not Found in "[/etc]"
open config.yaml: no such file or directory
what's wrong?

@ryanthedev
Copy link

I'm not sure but maybe try

viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
  fmt.Println(err)
}

or try

viper.SetConfigFile("config.yaml")
viper.AddConfigPath("/etc")
err := viper.ReadInConfig()
if err != nil {
   fmt.Println(err)
}

@nad2000
Copy link

nad2000 commented Oct 11, 2017

I confirm the issue with "not found" configuration. The proposed solution doesn't solve it.

@nad2000
Copy link

nad2000 commented Oct 11, 2017

It looks that config name should be without any extension (viper.SetConfigName(...)) while the actual file should have any supported extension. Take a look into

viper/viper.go

Line 1536 in d9cca5e

func (v *Viper) searchInPath(in string) (filename string) {

@mfridman
Copy link

@nad2000 is correct. searchInPath concatenates search path, file name and extension. Which appears to be the intended behaviour. filepath.Join(in, v.configName+"."+ext)

So viper will search, your filepaths + filename + extension:

/Users/example/go/src/github.com/your-app/config.json
/Users/example/go/src/github.com/your-app/config.toml
/Users/example/go/src/github.com/your-app/config.yaml
/Users/example/go/src/github.com/your-app/config.yml
/Users/example/go/src/github.com/your-app/config.properties
/Users/example/go/src/github.com/your-app/config.props
/Users/example/go/src/github.com/your-app/config.prop
/Users/example/go/src/github.com/your-app/config.hcl

Instead, what you could do is specify the file type yourself via:
viper.SetConfigType("yaml")

and pass in the absolute path to your config file via:
viper.SetConfigFile("/etc/configs/config")

or if the config file will always be in the same directory as main application:
viper.SetConfigFile("config")

@rayascott-zz
Copy link

How much time did I waste on this!? It should be documented clearly that specifying what data serialization format your config file uses, will modify the name that is searched for by appending an extension to the file name.

@ufoscout
Copy link

ufoscout commented Jan 30, 2018

Message removed... I messed up with the folder names... 😥

@mfridman
Copy link

Instead of using AddConfigPath try the following:

// set the config file type
viper.SetConfigType("yaml")

// specify where the config file is
viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter

// read in config file and check your errors
if err := viper.ReadInConfig(); err != nil {
  // handle errors
}

// confirm where the file has been read in from
fmt.Println(viper.ConfigFileUsed())

@Vikash082
Copy link

This looks real issue as it always require complete path with file name for search. AddConfigPath() doesn't work in the case where config file is in other directory. @mfridman @nad2000

@zsrinivas
Copy link

zsrinivas commented May 21, 2018

for me SetConfigName and AddConfigPath without SetConfigFile works

my conf file is at /secrets/app.yaml

	Config.SetConfigType("yaml")
	Config.AddConfigPath("/secrets/")
	Config.SetConfigName("app")
	err := Config.ReadInConfig()

this is because if you set ConfigFile it takes the file as the absolute path instead of searching for that file in the list of config paths

viper/viper.go

Lines 1521 to 1534 in d9cca5e

func (v *Viper) getConfigFile() (string, error) {
// if explicitly set, then use it
if v.configFile != "" {
return v.configFile, nil
}
cf, err := v.findConfigFile()
if err != nil {
return "", err
}
v.configFile = cf
return v.getConfigFile()
}

@daixiang0
Copy link

any update now?

@Double1996
Copy link

Double1996 commented Jul 14, 2019

Instead of using AddConfigPath try the following:

// set the config file type
viper.SetConfigType("yaml")

// specify where the config file is
viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter

// read in config file and check your errors
if err := viper.ReadInConfig(); err != nil {
  // handle errors
}

// confirm where the file has been read in from
fmt.Println(viper.ConfigFileUsed())

Thanks!! I solve my issue with this reply

@MatthewDudley
Copy link

MatthewDudley commented Jul 22, 2019

Instead of using AddConfigPath try the following:

// set the config file type
viper.SetConfigType("yaml")

// specify where the config file is
viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter

// read in config file and check your errors
if err := viper.ReadInConfig(); err != nil {
  // handle errors
}

// confirm where the file has been read in from
fmt.Println(viper.ConfigFileUsed())

Thanks!! I solve my issue with this reply

Neither Get nor SetConfigFile is working for me. I keep getting 'no such file'

@jayvib
Copy link

jayvib commented Aug 21, 2019

Instead of using AddConfigPath try the following:

// set the config file type
viper.SetConfigType("yaml")

// specify where the config file is
viper.SetConfigFile("./config/config") // or viper.SetConfigFile("config/config"), doesn't matter

// read in config file and check your errors
if err := viper.ReadInConfig(); err != nil {
  // handle errors
}

// confirm where the file has been read in from
fmt.Println(viper.ConfigFileUsed())

Thanks!! I solve my issue with this reply

Neither Get nor SetConfigFile is working for me. I keep getting 'no such file'

Use this instead....

viper.SetConfigType("yaml")
viper.SetConfigName("configName")
viper.AddConfigPath("/path/to/config/file")
err := viper.ReadInConfig()
if err != nil {
// Handle Error
...
}

@aiyogg
Copy link

aiyogg commented Sep 10, 2019

It's looks like viper.AddConfigPath(".") dosn't work correct now, must be specified a absolut path...

@ninetynineteen
Copy link

It's looks like viper.AddConfigPath(".") dosn't work correct now, must be specified a absolut path...

But it doesn't work

@ninetynineteen
Copy link

The same problem happen to me. I use following method solve it.

viper.SetConfigType("yaml")
viper.SetConfigFile("/path/to/config/file/configName")
err := viper.ReadInConfig()
if err != nil {
// Handle Error
...
}

@steveperjesi
Copy link

You can add multiple viper.AddConfigPath()lines based on commented code, https://github.com/spf13/viper/blob/master/viper.go#L411

viper.AddConfigPath("./")
viper.AddConfigPath("../")

@pedromss
Copy link
Contributor

@sagikazarmark this should be reopened as I mistakenly marked #722 as solving #390 . Thanks to @andig (#722 (comment)) for bringing this to attention.

@sagikazarmark sagikazarmark reopened this Jan 10, 2020
@sagikazarmark
Copy link
Collaborator

Unfortunately, I can't really tell anymore what this issue is about, but I get the feeling it's rather a documentation problem than an actual bug in the code.

@pedromss
Copy link
Contributor

Re-reading this thread it seems the problem might be in the AddConfigPath as this comment #390 (comment) seems to suggest

@KhoaND290391
Copy link

KhoaND290391 commented Feb 6, 2020

Assume your configs file in /home/ubuntu/go/bin/myapp/configs/conf.env
Your code should provide :
if run from /home/ubuntu/go/bin/myapp/configs/ then set viper.AddConfigPath(".")
if run from /home/ubuntu/go/bin/myapp/ then set viper.AddConfigPath("./configs")
if run from /home/ubuntu/go/bin/ then set viper.AddConfigPath("./myapp/configs")
if run from root of Ubuntu machine (such as make it as a service)
then set viper.AddConfigPath("/home/ubuntu/go/bin/myapp/configs/")

@neverovski
Copy link

It doesn't work for production version

@neverovski
Copy link

Works for me in production:

_, filePath, _, _ := runtime.Caller(0)
configFile := filePath[:len(filePath)-9]
viper.SetConfigFile(configFile + string(filepath.Separator) + ".env")
err := viper.ReadInConfig()

@hoppfrosch
Copy link

hoppfrosch commented May 15, 2020

Just had following problem:(using an existing configfile "myConfig.json")

v := viper.New()
v.SetConfigName("myConfig.json")
v.SetConfigType("json")
v.AddConfigPath(".")

err := v.ReadInConfig()
if err != nil {
	log.Panicf("Fatal error config file: %s", err)
}

Without "SetConfigType" the error is thrown ...

v := viper.New()
v.SetConfigName("myConfig.json")
// v.SetConfigType("json")   => Omitting setting config type, leads to error condition, unless config name contains extension
v.AddConfigPath(".")

err := v.ReadInConfig()
if err != nil {
	log.Panicf("Fatal error config file: %s", err)
}

Running: go version go1.14.2 windows/amd64

saltbo pushed a commit to saltbo/viper that referenced this issue Aug 7, 2020
saltbo added a commit to saltbo/viper that referenced this issue Aug 7, 2020
@jckling
Copy link

jckling commented Oct 29, 2020

Worked in Win10 (using GoLand)!! 😃

// Set the file name of the configurations file
viper.SetConfigName("config")

// Set the configuration file type
viper.SetConfigType("yaml")

// Set the path to look for the configurations file
viper.AddConfigPath("./config")

// Find and read the config file
err := viper.ReadInConfig()
if err != nil {
	fmt.Printf("Error reading config file, %s", err)
	return err
}

BTW viper.SetConfigName("config.yml") did not work.

@theycallmeloki
Copy link

Can confirm viper.SetConfigFile does not work, the way to fix it was to add the directory the configuration would be present in using AddConfigPath

tugberkugurlu added a commit to tugberkugurlu/go-bloggy that referenced this issue Dec 23, 2020
need to set up in this way, see:
spf13/viper#390 (comment)

also, only works for the layout for now, need to get other pages working with this config as well.

when it's local, this also enables static file server.
tugberkugurlu added a commit to tugberkugurlu/go-bloggy that referenced this issue Dec 24, 2020
* example secret usage

* there is no need fot the go dependencies section

* temp: ls and tree

* install aws sdk

* temp: fail the build on purpose

* make the upload assets job depend on build

* switch to Go 1.15.6

* Revert "temp: fail the build on purpose"

This reverts commit bf12a3b.

* ls s3 bucket

* dryrun aws s3 sync

* seperate it into multiline correctly

see https://stackoverflow.com/a/62044610/463785

* multi-line didn't seem to work

see https://github.com/tugberkugurlu/go-bloggy/pull/5/checks?check_run_id=1597645520

* remove dryrun

* set up yaml config for assets

need to set up in this way, see:
spf13/viper#390 (comment)

also, only works for the layout for now, need to get other pages working with this config as well.

when it's local, this also enables static file server.

* push page data to the pages with a container

so that I can push the config to the pages

* use AssetsUrl in appropirate places

* use lowercase file path for css

* set correct mime types

see aws/aws-cli#1249 (comment)

it seems like following ones are set to `application/octet-stream`

 - icons.woff2
 - brand-icons.woff2
 - brand-icons.woff
 - icons.woff

* cat /etc/mime.types

* Ubuntu 18.04.1 LTS doesn't seem to use ~/.mime.types

see aws/aws-cli#1249 (comment)

* use sudo correctly with pipe

see https://unix.stackexchange.com/a/106666

* lowercase the image urls

* support for ci config folder

* set ci/config.yaml

* Upload CI config.yml for Assets

* docker build

see https://github.com/marketplace/actions/build-and-push-docker-images

also https://docs.github.com/en/free-pro-team@latest/actions/guides/storing-workflow-data-as-artifacts

* Download artifact to a specific directory

* enable docker push

* seperate docker deploy into its own workflow

so that we can only run that for master builds.
tstrijdhorst pushed a commit to tstrijdhorst/tflow that referenced this issue Jan 5, 2022
@margaaareti
Copy link

How we handled this fckn problem? I m trying for 2 day . . .

@twobit214
Copy link

twobit214 commented Feb 17, 2022

This was very frustrating to deal with. For me, I finally figured out that you cannot use SetConfigFile() in conjunction with anything else. It can only be the path to the config file. If you use this method, it will ignore any paths and ONLY use the value passed to SetConfigFile()

In this example, my file is named fileNameNoExtension.yaml on disk.

      configFileName := "fileNameNoExtension"
        mydir = "/a/dir"
	viper.AddConfigPath(mydir)
	viper.SetConfigType("yaml")
	viper.SetConfigName(configFileName)
	//viper.SetConfigFile()

if I uncomment the last line, all previous lines are ignored and the config file fails to resolve.

@mkdior
Copy link

mkdior commented Jul 18, 2022

Can successfully read my configuration after setting the ConfigType as suggested by the first answer.

var (
    _, b, _, _ = runtime.Caller(0)
    root       = filepath.Join(filepath.Dir(b), "../..")
)

func main() {
    viper.AddConfigPath(root)
    viper.SetConfigType("yaml") // This work
    viper.SetConfigName("my-config-file")
    _ = viper.ReadInConfig()
    ...    
var (
    _, b, _, _ = runtime.Caller(0)
    root       = filepath.Join(filepath.Dir(b), "../..")
)

func main() {
    viper.AddConfigPath(root)
    viper.SetConfigName("my-config-file.yaml") // This doesn't work
    _ = viper.ReadInConfig()
    ...    

@gwvandesteeg
Copy link

you need to ensure you only use one of the two approaches, either specify the file by the full config file name or let viper figure it out.

viper.SetConfigFile("full-name-of-file-including-path-and-extension-if-any")

OR

viper.AddConfigPath("search-path-1") // repeat as needed to add places to look for the config file
viper.SetConfigType("yaml") // or whatever supported format the file is in, if not specified then it'll try all it supports
viper.SetConfigName("name-of-the-config-file-which-has-an-extension-but-without-the-extension")

So to read in the config file in YAML format placed at /etc/example/configfile.yaml, you would use either of the below.

viper.SetConfigFile("/etc/example/configfile.yaml")

OR

viper.AddConfigPath("/etc/example")
viper.SetConfigType("yaml")
viper.SetConfigName("configfile")

Here's a gist for some examples https://gist.github.com/gwvandesteeg/939c05ac34518d65c49a94b2dbdd7cec

@let4be
Copy link

let4be commented Jan 24, 2023

Still is broken. Wtf...

@yuzhouu
Copy link

yuzhouu commented Mar 30, 2023

it seems like doc is wrong

viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name

For me

this one worked

viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(...)
viper.AddConfigPath(...)

this not

viper.SetConfigName("config.yaml")
viper.AddConfigPath(...)
viper.AddConfigPath(...)

@meanii
Copy link

meanii commented Aug 8, 2023

I'm having the same issue, i tried all the methods above but none of them worked for me

viper.SetConfigType("yaml")
viper.AddConfigPath("../configs")
viper.SetConfigName("config")

@EmreCogac
Copy link

Update your env AddConfigPath with ../env or ../../env

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

Successfully merging a pull request may close this issue.