-
Notifications
You must be signed in to change notification settings - Fork 583
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
fix(dotenv/types): allow null
for *path
values
#3221
Conversation
Note to self and for a potential follow up PR: After refactoring the new test to be stricter, I realized that there doesn't seem to be a way to load environment variables purely from a file: without granting access to at least some actual environment variables. In my view, this seems like an oversight and could be fixed rather easily by:
|
const testOptions = { | ||
envPath: path.join(testdataDir, "./.env"), | ||
defaultsPath: path.join(testdataDir, "./.env.defaults"), | ||
}; | ||
const testOptions = Object.freeze({ | ||
envPath: path.join(testdataDir, ".env"), | ||
defaultsPath: path.join(testdataDir, ".env.defaults"), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not strictly necessary, but I had to examine the entire test file to be sure that nothing was mutating these properties before I felt confident that I could rely on their values in an additional test. This change will prevent someone else from having to do the same.
@jsejcksn I wonder if supporting |
Hi @kt3k: I think it is useful to support it. It might not seem obvious for typical cases — but why should we arbitrarily limit less common workflows when it's simple to provide granular, programmatic control? For example: a user might generate the configuration from parsed CLI arguments or other code to support branched logic where, in some conditions, the program should not try to read the |
I agree with @kt3k. I think allowing null as
For that particular case: Couldn't |
@timreichen Thanks for the feedback.
Yes, for that particular example the technique you suggested would be a workaround, but it would require more complicated logic compared to simply toggling options on/off. I agree that the purpose of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsejcksn Thanks for clarifying the reasoning. Now this looks good to me.
This PR adds
null
to the union of allowed types for the path-related options in theLoadOptions
type.This change fulfills two objectives:
The current type uses optional properties for these string values. This means that the values can be provided as strings — or not provided at all and will become
undefined
at runtime.Because the functions which use these properties also implement default parameters in the case that they are not defined (e.g.
load
andloadSync
), the values will always bestring
s at the usage sites. In order to prevent usage of each of the default parameter values, one must currently pass the empty string""
. This works, but is a code smell — it does not clearly indicate intent — so includingnull
in the union of allowed types will allow the programmer to explicitly indicate "I don't want to use a path here — not even a default value".A nice side-effect of the use of
null
to opt out of specifying a path is that the program no longer needs read permissions for the default path parameters in that case. If a programmer would like to use this module to load environment variables from only a single file, it allows for stricter permission control by only specifying that one file's path for the read permissions.