allan2 / dotenvy

A well-maintained fork of the dotenv crate
MIT License
708 stars 45 forks source link

Checking usage at the compile time #8

Closed sei0o closed 2 years ago

sei0o commented 2 years ago

I often forget to (and make mistypes on) use environment variables I have added or renamed. It would be useful to let dotenvy warn if the programmer used the variables which are not available in .env. For example:

.env:

FOO=false
BAR_VERSION=2.27

A Rust program:

env::var("FOO_VERSION") # => warns because FOO_VERSION does not appear in .env
env::var("FOO") # => false
# could warn about BAR_VERSION as unused

While some frameworks prohibit access to the environment variables that are not in .env for security reasons, there will be cases when one wants to use variables on the operating system and we don't know what will be available in each environment (staging, development, production, etc.), so it doesn't have to emit errors I suppose. What do you think? Is it even possible with the build scripts?

allan2 commented 2 years ago

I often forget to (and make mistypes on) use environment variables I have added or renamed. It would be useful to let dotenvy warn if the programmer used the variables which are not available in .env. For example:

.env:

FOO=false
BAR_VERSION=2.27

A Rust program:

env::var("FOO_VERSION") # => warns because FOO_VERSION does not appear in .env
env::var("FOO") # => false
# could warn about BAR_VERSION as unused

I don't think this is a good idea. It shouldn't matter how an environment variable is defined, whether it is by a dotenv file or by the shell or system.

While some frameworks prohibit access to the environment variables that are not in .env for security reasons Could you provide some examples so I can take a look?

For preventing typos, dotenvy::var("foo") returns a Result. You can handle that however you wish.

For checking if an environment variable is unused, you could load all of your environment variables as Rust variables, perhaps in a Config struct. Then, you'll get warnings for your Rust variables.

sei0o commented 2 years ago

For checking if an environment variable is unused, you could load all of your environment variables as Rust variables, perhaps in a Config struct. Then, you'll get warnings for your Rust variables.

I remembered that I have written that kind of Config struct, and yes, now I realized it also works as an unused checker. Thanks for your reply (and dotenvy maintenance). Closing :)