Skip to content

Support multiple .env files #136

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

Open
AlexSkrypnyk opened this issue Jan 6, 2025 · 5 comments · May be fixed by #137
Open

Support multiple .env files #136

AlexSkrypnyk opened this issue Jan 6, 2025 · 5 comments · May be fixed by #137

Comments

@AlexSkrypnyk
Copy link
Collaborator

126 has added support for reading .env files.
I would like Ahoy to support loading from multiple .env files:

ahoyapi: v2
env: 
  - .env
  - .env.local

Similar approach exists in Docker Compose.

If a file does not exists - it should be skipped and no error would be triggered.

@ocean
Copy link
Member

ocean commented Jan 8, 2025

Sounds fair enough @AlexSkrypnyk

What would your expected behaviour be for variables declared in multiple files, that a variable in a later file (further down the list) takes precedence and overwrites the same variable in the earlier file?

E.g.

ahoyapi: v2
env: 
  - .env
  - .env.local

.env:

VAR=foo

.env.local:

VAR=bar

Ends up with VAR equal to bar inside running Ahoy?

@AlexSkrypnyk
Copy link
Collaborator Author

yes, the later value wins

@jackwrfuller jackwrfuller linked a pull request Jan 13, 2025 that will close this issue
@jackwrfuller
Copy link
Contributor

@AlexSkrypnyk I've had a go implementing this, would you be able to review?

A few notes:

  • As the env field is now a []string, it is necessary to change how an env file is defined in the ahoy yml file to
env:
  - .env

even if there is only one env file.

  • You said: "If a file does not exists - it should be skipped and no error would be triggered." As Ahoy currently works, if an incorrect env filepath is provided, it returns a failure status. As the MR currently stands, I've kept that behaviour as is. Do you envising people using both a dev and production env file and including both so that the ahoy command works across environments automatically?

@ocean
Copy link
Member

ocean commented Jan 30, 2025

@jackwrfuller I understand why you've kept the behaviour for nonexistent .env files the same as it is for Ahoy config files, however I think I agree with Alex in that the behaviour should be that they are ignored if nonexistent, as that would make it more flexible for the example you suggest - people could have multiple ones for different environments and they only override each other if they exist.

Are you able to alter the MR to switch to this behaviour?

@AlexSkrypnyk
Copy link
Collaborator Author

If a file does not exists - it should be skipped and no error would be triggered.
In Docker Compose, they allow to control this with an additional property, converting each entry of env: to a map of these properties.

I think we should avoid such overcomplicated approach and just skip without an error.

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

Successfully merging a pull request may close this issue.

3 participants