craftcms/craft

.env file is required in any environment

ostark opened this issue · 4 comments

This call (new Dotenv\Dotenv($root))->load() fails when no .env file is present. Here is the exception:

Uncaught Dotenv\Exception\InvalidPathException: Unable to read the environment file

It's used in web/index.php and in the craft binary. There are two workarounds I can think of:

  1. Catch the Exception, like Lumen does
  2. Load the .env file only under specific condition, like getenv('CRAFT_ENV') != 'production'

@ostark, I went to setup the Docker setup mentioned in #5 and noticed the same issue.

My concern is portability between environments, say for example that the local development is done with Docker, you would set the environment variables using a file like .env but would not require the .env file inside of the container as you could just load it from a docker-compose.yml using env_file" .env.

However, if you develop locally in Docker and then push to a PaaS like FortRabbit or Heroku, the .env is not really an option as the environment variables are set in the application using a GUI or CLI.

It would be nice to find a way to wrap the DotEnv load in a try catch and find another way to check the environment variables and simply log that "we are not using the .env/could not find the file".

On second thought, maybe it would be a good idea for Craft itself to check for the minimally required environment variables needed to boot the application and then throw a Server Requirement Failed exception...

Also, I think @brandonkelly might ask for this to be moved to the https://github.com/craftcms/cms repo.

Just updated web/index.php to check if a .env file exists before loading Dotenv.

@brandonkelly web/index.php should cover most cases. However, I discovered that issue by using the cli. Anyways, thx.

@ostark fixed for console requests, too.