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

suggestion: simplify load() from std/dotenv #4578

Open
iuioiua opened this issue Apr 12, 2024 · 5 comments
Open

suggestion: simplify load() from std/dotenv #4578

iuioiua opened this issue Apr 12, 2024 · 5 comments

Comments

@iuioiua
Copy link
Collaborator

iuioiua commented Apr 12, 2024

The design of load() from std/dotenv has been a source of pain for some users (see references). It does much more than it should, such as checking for required environment variables and defining default values. #4019 explored the option of deprecating this API in favor of the new --env CLI flag. IMO, --env is undoubtedly superior to load() for a few reasons. However, there may be cases when a user wants to load a .env file programmatically.

I suggest making the following breaking changes to load() in std/dotenv@1:

  1. Emulate the behavior of --env.
  2. Remove the .env.default and .env.example options.
  3. Throw only when the given .env file exists but is invalid.

References:

@kt3k
Copy link
Member

kt3k commented Apr 12, 2024

IMO, --env is undoubtedly superior to load() for a few reasons.

Can you elaborate on this? --env is still unstable feature, and I don't see much adoption of it. It's still possible that --env could be changed drastically or removed in the future.

Emulate the behavior of --env.

Can you elaborate on this? What's the main differences?

Simplify its behavior to load a .env file given by a path, and remove all options.

Why remove all options? I saw some complaints about handling of .env.example and .env.default (#3516), but didn't see problems with others.

Return true when the given .env is loaded and false if not found.

Sounds like unnecessary breaking change to me.

@iuioiua
Copy link
Collaborator Author

iuioiua commented Apr 12, 2024

Can you elaborate on this? --env is still unstable feature, and I don't see much adoption of it. It's still possible that --env could be changed drastically or removed in the future.

Some aspects of load() have surprising behavior. E.g. see #3561. I feel like I've elaborated on load()'s design a sufficient amount here and in the issues/PRs linked above. The amount of adoption of --env is less obvious than load() as it doesn't live in code. --env can live in deno.json tasks, but even that only shows a part of the use of --env.

Can you elaborate on this? What's the main differences?

Mostly, not throwing when a .env is not found. Though, I'm unsure that doing so is the right way to go.

Why remove all options? I saw some complaints about handling of .env.example and .env.default (#3516), but didn't see problems with others.

It doesn't have to go that far. Perhaps removing .env.example and .env.default functionality is sufficient.

Sounds like unnecessary breaking change to me.

It provides feedback on whether a .env file is detected and used. AFAIK, load() currently provides no such feedback.

@Leokuma
Copy link

Leokuma commented Apr 12, 2024

I agree with items 1 and 4.


  1. Simplify its behavior to load a .env file given by a path, and remove all options.

If we can't reach consensus about this, we could instead keep all options, just remove their defaults ".env.example" and ".env.defaults" so that it's mandatory to specify the filenames if you want to load them.

  1. Return true when the given .env is loaded and false if not found.

Currently it returns the parsed object, which is useful. If we make that change, it will be necessary to call load() and parse() in order to know what was parsed.

One of the reasons why --env adoption could be low is that it's still pretty new, while Dotenv has years of history and many apps were already using Dotenv.

@iuioiua
Copy link
Collaborator Author

iuioiua commented Apr 14, 2024

Ok. I've amended by suggestion. PTAL @Leokuma and @kt3k.

@kt3k
Copy link
Member

kt3k commented Apr 16, 2024

2. sounds good to me.

I still don't get 1. and 3. well.

Can you elaborate on this? What's the main differences?

Mostly, not throwing when a .env is not found. Though, I'm unsure that doing so is the right way to go.

To my understanding --env and load() both don't throw when the .env file not found (but the former warns if it doesn't find it).

Throw only when the given .env file exists but is invalid.

In what cases load stops throwing with this suggestion?

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

3 participants