DEV Community

Cover image for Announcing dotenv-linter: a tool to lint your .env files
Nikita Sobolev for wemake.services

Posted on

Announcing dotenv-linter: a tool to lint your .env files

I happy to announce a simple yet very useful tool to lint your .env files.

GitHub logo wemake-services / dotenv-linter

☺️ Linting dotenv files like a charm!

dotenv-linter

wemake.services test codecov Github Action Python Version Documentation Status


Simple linter for .env files.

dotenv-logo

While .env files are very simple it is required to keep them consistent This tool offers a wide range of consistency rules and best practices.

And it integrates perfectly to any existing workflow.

Read the announcing post.

Installation and usage

pip install dotenv-linter
Enter fullscreen mode Exit fullscreen mode

And then run it:

dotenv-linter .env .env.template
Enter fullscreen mode Exit fullscreen mode

See Usage section for more information.

Examples

There are many things that can go wrong in your .env files:

# Next line has leading space which will be removed:
 SPACED=

# Equal signs should not be spaced:
KEY = VALUE

# Quotes won't be preserved after parsing, do not use them:
SECRET="my value"

# Beware of duplicate keys!
SECRET=Already defined ;(

# Respect the convention, use `UPPER_CASE`:
kebab-case-name=1
snake_case_name=2
Enter fullscreen mode Exit fullscreen mode

And much more! You can find the full list of violations in our docs.

Background

As a part of our "not blaming but fixing" corporate culture we build a lot of tools that prevent us from making the same mistakes over and over again.

dotenv-linter is one of these tools.

Some time ago we had several problems with .env files:

  • Some developers used CONSTANT_CASE for variable names and some developers used snake_case for that. While this is not a technical issue, but it is not very practical to mix these two cases and then think: what case I have used for this particular variable? Consistency is important!
  • We also had a problem with quotes and extra spaces. Some developers used KEY=VALUE and some used KEY = "VALUE" while in fact, these two examples will resolve in exactly the same thing - we prefer to have one-- and preferably only one --obvious way to do it. So, we now stick to KEY=VALUE notation
  • We also once had a duplicate key that ruined my day. I have spent several hours debugging my app because of this simple issue. That was a turning point for me and I have decided: let's automate it!

Installation

You can install it via pip (or any other similar tool):

$ pip install dotenv-linter
Enter fullscreen mode Exit fullscreen mode

Why pip? Because python is present almost on all Linux setups. And we try to make this tool as portable as possible.

Usage

Usage is really simple:

$ dotenv-linter path/to/your/.env even/multiple/files/are/fine/.env
Enter fullscreen mode Exit fullscreen mode

Real-life examples

If you are interested in how we use it real life applications you can have a look at (and even try!) our django template. Here's the line that invokes it.

We also have a full list of linting rules in our docs, check it out.

Conclusion

I hope this simple tool will save you some time, make your project more consistent, and your life slightly better. Add me on github to stay informed about the tools I am building!

Top comments (5)

Collapse
 
protium profile image
protium

Cool! Have you thought about making a VS Code extension 😃?

Collapse
 
sobolevn profile image
Nikita Sobolev

Nope, but I have now! Do you want to help?

Collapse
 
protium profile image
protium

I will give it a try next week! Technically speaking, if it's possible to install the repo as dependency of the extension (or it could be a requirement) and execute the linter as a python shell call, it should be able to tell the user if the file has some errors and the line numbers? As I did a quick read of the source code it reports the filename and line number.

Thread Thread
 
sobolevn profile image
Nikita Sobolev

Yes, it sounds like it would work.

Collapse
 
sobolevn profile image
Nikita Sobolev

Pretty much the same as with vscode. It is on the roadmap. Contributions are especially welcome.

Feel free to start a project. You will have all my support :)