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

Precedence issue: environment variables with config.set #420

Open
lomboboo opened this issue Oct 25, 2024 · 0 comments
Open

Precedence issue: environment variables with config.set #420

lomboboo opened this issue Oct 25, 2024 · 0 comments

Comments

@lomboboo
Copy link

Hello, first of all thank you for this awesome tool.
I would like to report which seems to me like a bug.

In our setup we use default values, config.set, config.loadFile and environment variables.

During the issue debugging I have discovered that in the scenario when config.set and environment variables are used without config.loadFile we get unexpected results. Consider examples below, which are excerpt from your documentation and the simplified version of our setup:

example-1

process.env.PORT = 8080; // environment variable is set
const config = convict({
  port: {
    default: 3000,
    env: 'PORT'
  }
});
config.load({ port: 9000 });
console.log(config.get('port')); // 8080 from env variable

example-2

process.env.PORT = 8080; // environment variable is set
const config = convict({
  port: {
    default: 3000,
    env: 'PORT',
  },
});
config.set('port', 9000); // config.set without config.loadFile or config.load
console.log(config.get('port')); // 9000 from config.set

example-3

process.env.PORT = 8080; // environment variable is set
const config = convict({
  port: {
    default: 3000,
    env: 'PORT',
  },
});
config.set('port', 9000); // config.set
config.load({ port: 10000 }); // config.load after config.set 
console.log(config.get('port')); // 8080 from env

✅ In example-1 everything works as it should and as per documentation.

❌ In example-2 you can see that using config.set will override environment variable which must have higher precedence as per docs.

✅ In example-3 if we add config.load right after config.set it actually fixes the issue from example-2. It feels like config.load re-instantiates environment variables precedence which config.set doesn't do.

If it is intentional behaviour of the config.set method then it should be clearly stated in the documentation, ideally with the provided example. If not, then I believe this is a bug that should be fixed.

@lomboboo lomboboo changed the title Environment variables with config.set Precedence issue: environment variables with config.set Oct 25, 2024
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