[SC-1504] Make .env
loading optional with autoLoadEnv
constructor flag
#189
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Static Code Analysis (readability, compactness):
autoLoadEnv
flag in theNetworks
constructor to control whether environment variables should be automatically loaded viadotenv.config()
.loadEnv()
utility function that loads environment variables, by default from.env
, but accepts standarddotenv.config
options (e.g., custom path or encoding).These changes remove hidden side effects, improve clarity, and preserve backward compatibility.
Dynamic Code Analysis (external APIs, interaction flows):
Previously, environment variables were always loaded implicitly during
Networks
instantiation, which could lead to issues ifprocess.env
was accessed before that (e.g., inhardhat.config.ts
). The new approach allows developers to load environment variables explicitly usingloadEnv(options?)
at the appropriate point in the setup flow.Efficiency (gas costs, computational complexity, memory requirements):
No impact. The change only affects how environment variables are loaded during setup and does not impact runtime or on-chain behavior.
Opinion, trade-offs and other thoughts (optional):
This change slightly increases the API surface but improves flexibility and makes environment handling explicit and predictable. It aligns with library design best practices by avoiding implicit side effects and supporting more advanced
dotenv
usage via options.