-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.6] Catches InvalidFileException when loading environment file with dotenv #23149
Conversation
How do you trigger an InvalidFileException in this case? |
The exception is thrown when a value that contains a space is not enclosed in quotation marks, e.g:
https://github.com/vlucas/phpdotenv/blob/master/src/Loader.php#L220 // Unquoted values cannot contain whitespace
if (preg_match('/\s+/', $value) > 0) {
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
} |
+1 for this. It's hard to debug because current Exception message says nothing about .env file. |
Surely this will make it harder to debug? |
I'm not sure I see the positive gain here. I've added the app_name with unquoted spaces to my .env file, and running
This change would silently ignore the problem instead of telling me. (Or am I missing something since I'm testing this on 5.5?) |
Current behaviour:
Pull Request Behaviour
My Proposed Behaviour
Dumping the exception would result in this:
Very easy to understand and fix. The current behaviour is inconsistent and confusing (see the links above). At the very least the behaviour should be consistent. Personally I think that the request should be terminated and the exception dumped, but if that's not desirable for some reason then this pull request at least makes the behaviour consistent (if @sisve many developers don't interact with Laravel on the command line frequently, they develop in their browser, where the non-descript error is displayed. @ntzm I don't believe so, either of the approaches (pull request, or preferred option) at the very least introduces consistent behaviour and "why is my configuration not being loaded?" is a much better starting point for this issue than the |
Although Laravel does create a The alternative is to keep the current behaviour and add additional behaviour for try {
(new Dotenv($app->environmentPath(), $app->environmentFile()))->load();
} catch (InvalidPathException $e) {
//
} catch (InvalidFileException $e) {
dd('Fatal Error: Values in .env containing spaces must be surrounded by quotes.');
}
I have updated the pull request to use the backwards compatible behaviour — missing |
I don't think this is right. There are way more reasons parsing can fail than just double quotes missing! |
@GrahamCampbell Are you sure? As far as I can tell the only place the |
Would it not be better to dump the exception message then? |
To be honest, this PR feels like a massive hack to me. Surely the correct fix would be to handle exceptions properly that occur at this early stage in the bootstrap process, and present them properly? |
@GrahamCampbell That would be my preference, if there is a way to handle errors correctly then that would be much better. However, as far as I can tell, at this point in the bootstrap process Laravel doesn't have access to anything necessary to error properly (i.e: the exception system depends on valid configuration) so it would require changes to the architecture. I could very well be wrong there, though, are there ways to error properly here? Re: dumping exception message vs. a custom error message, the exception message does not explicitly state the issue is with the That said, anything (whether it's dumping the exception, or a custom error message, or even loading the default configuration) is an improvement over the current non-descript uncaught fatal error that causes so much confusion, so if there's a different approach to this problem that is better than mine then I support it. |
This also seems to occur if you have an error (In my case a call to a undefined function) within |
Dotenv may throw an exception when loading a
.env
file, eitherInvalidPathException
orInvalidFileException
. At presentLoadEnvironmentVariables
is only catchingInvalidPathException
. This means that when you provide an invalid.env
file Laravel outputs an unhelpful error message, e.g:Many are confused by this error: 1, 2, 3, 4, 5, 6.
This change captures the additional error, meaning that when invalid
.env
file is provided (usually because a value containing a space lacks quotation marks) Laravel will fall back to the default application config.I think that when this error occurs Laravel should not continue executing[1] because it is a very difficult situation to debug without an error, and it's unlikely a developer will want their application to run without a
.env
— and if they do, they can create an empty.env
file — but I haven't added it in case there is a use case I am not aware of that necessitates allowing the application run even with an invalid.env
file provided. Any thoughts on terminating execution here?[1] As far as I know the only way to output an error here would be
dd($e)
because the error handling functionality isn't loaded at this point in the bootstrap process.