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

Envconfig not respecting Prefix #148

Open
jakubdyszkiewicz opened this issue Jul 23, 2019 · 8 comments
Open

Envconfig not respecting Prefix #148

jakubdyszkiewicz opened this issue Jul 23, 2019 · 8 comments

Comments

@jakubdyszkiewicz
Copy link

Consider such struct

type Something struct {
	User string `envconfig:"user"`
}
something := Something{}
err := envconfig.Process("my_app", &something)

I'd expect that if MY_APP_USER env is not set, the something.User will be empty, but that's not the case. Due to this line https://github.com/kelseyhightower/envconfig/blob/master/envconfig.go#L99 the Alt is set to user. Process method https://github.com/kelseyhightower/envconfig/blob/master/envconfig.go#L195 first try to find by Key (which is MY_APP_USER), but then by Alt, so when MY_APP_USER is not available, then the USER is looked up. On MacOS USER this is set to your username, so instead of empty string I see my username.

Is this expected behaviour?

@TonyPythoneer
Copy link

Hi @jakubdyszkiewicz
I never encounter this issue. Could you show your code execute result and terminal when extracting the environment variable?

In general, it's not possible.

@jakubdyszkiewicz
Copy link
Author

Well... it is possible. Take a look:

Complete sample code:

package main

import (
	"fmt"
	"github.com/kelseyhightower/envconfig"
)

type Something struct {
	User string `envconfig:"user"`
}


func main() {
	something := Something{}
	err := envconfig.Process("my_app", &something)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Value is: %v", something.User)
}

terminal output

jakub.dyszkiewicz in /Users/jakub.dyszkiewicz/dev/sample-go
❯❯❯ echo $MY_APP_USER


jakub.dyszkiewicz in /Users/jakub.dyszkiewicz/dev/sample-go
❯❯❯ echo $USER
jakub.dyszkiewicz

jakub.dyszkiewicz in /Users/jakub.dyszkiewicz/dev/sample-go
❯❯❯ ./sample-go
Value is: jakub.dyszkiewicz

jakub.dyszkiewicz in /Users/jakub.dyszkiewicz/dev/sample-go
❯❯❯ set -gx MY_APP_USER other_user

jakub.dyszkiewicz in /Users/jakub.dyszkiewicz/dev/sample-go
❯❯❯ ./sample-go
Value is: other_user

@TonyPythoneer
Copy link

TonyPythoneer commented Aug 18, 2019

It's wired.
Could you push a PR with your case in unittest makes CI job help validate?

It's because CI is the role of neutral referee. If we have any issue happens in personal computer, the best solution is to add more test for this repo.

@aqtodd
Copy link

aqtodd commented Oct 2, 2019

@jakubdyszkiewicz I ran into a similar issue and while investigating I found this is specifically triggered when using the envconfig struct tag to specify the environment variable name. When using this tag it will fall back to the environment value specified even when using a prefix.

I believe this is the intended behavior based on this note in the README:

If envconfig can't find an environment variable in the form PREFIX_MYVAR, and there is a struct tag defined, it will try to populate your variable with an environment variable that directly matches the envconfig tag in your struct definition:

@aqtodd
Copy link

aqtodd commented Oct 2, 2019

Here's a playground snippet demonstrating the difference: https://play.golang.org/p/KssTfMgXED1


import (
	"fmt"
	"os"
	"github.com/kelseyhightower/envconfig"
)

type ImplicitTest struct {
	User string
}

type ExplicitTest struct {
	User string `envconfig:"USER"`
}

func main() {
	os.Setenv("USER", "test")

	implicitTest := ImplicitTest{}
	envconfig.MustProcess("my_app", &implicitTest)
	fmt.Printf("The value of ImplicitTest.User: \"%s\"\n", implicitTest.User)
	
	explicitTest := ExplicitTest{}
	envconfig.MustProcess("my_app", &explicitTest)
	fmt.Printf("The value of ExplicitTest.User: \"%s\"\n", explicitTest.User)
}

output:

The value of ImplicitTest.User: ""
The value of ExplicitTest.User: "test"

@limoli
Copy link

limoli commented Nov 27, 2019

Same problem here.

@pr0n1x
Copy link

pr0n1x commented Jul 10, 2024

Hi. I think this PR is the complete solution.
#214

pr0n1x added a commit to pr0n1x/envconfig that referenced this issue Jul 16, 2024
…fix and partially saved backward compatibility by using new tag no_pfx
@pr0n1x
Copy link

pr0n1x commented Jul 16, 2024

I realized that there is no way to fix this problem without partial backward compatibility break. This wrong (IMHO) behavior is the author's decision (it's obvious if you look at the unit tests).
Using new 'no_pfx' tag is trade-off but quite fit solution. See second commit in my PR #214

pr0n1x added a commit to pr0n1x/envconfig that referenced this issue Jul 16, 2024
with respecting of env-prefix and partially saved backward compatibility by using new tag no_pfx
pr0n1x added a commit to pr0n1x/envconfig that referenced this issue Jul 16, 2024
with respecting of env-prefix and partially saved backward compatibility by using new tag no_pfx
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

4 participants