vlucas / phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
BSD 3-Clause "New" or "Revised" License
13.15k stars 626 forks source link

Failed to parse dotenv file due to unexpected whitespace #566

Closed ambrosiora closed 6 months ago

ambrosiora commented 6 months ago

Hi!

This issue is related to #520, and I think the root problem can be AWS fault, BUT, maybe the repo admin can help us to solve this problem here. Or maybe help us to reach out AWS support team.

About 3 days ago, using AWS Codebuild in a code pipeline which builds and deploys my laravel project on AWS ECS, I suddenly faced an error:

#22 8.200 Fatal error: Uncaught Dotenv\Exception\InvalidFileException: Failed to parse dotenv file due to unexpected whitespace. Failed at [--with-curl --with-password-argon2 --with-pdo-pgsql --with-libedit]. in /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Parser.php:119
#22 8.200 Stack trace:
#22 8.200 #0 /var/www/html/vendor/vlucas/phpdotenv/src/Result/Error.php(92): Dotenv\Loader\Parser::Dotenv\Loader\{closure}('unexpected whit...')
#22 8.200 #1 /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Parser.php(122): Dotenv\Result\Error->mapError(Object(Closure))
#22 8.200 #2 [internal function]: Dotenv\Loader\Parser::Dotenv\Loader\{closure}(Array, '-')
#22 8.200 #3 /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Parser.php(125): array_reduce(Array, Object(Closure), Array)
#22 8.200 #4 /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Parser.php(34): Dotenv\Loader\Parser::parseValue('--with-curl --w...')
#22 8.200 #5 /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Loader.php(66): Dotenv\Loader\Parser::parse('PHP_BUILD_CONFI...')
#22 8.200 #6 /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Loader.php(44): Dot in /var/www/html/vendor/vlucas/phpdotenv/src/Loader/Parser.php on line 119
#22 8.202 Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 255
#22 8.205 Plugins have been disabled automatically as you are running as root, this may be the cause of the script failure.
#22 8.205 See also https://getcomposer.org/root
#22 ERROR: process "/bin/sh -c composer install" did not complete successfully: exit code: 255

Nothing on my code was changed. All things was working and now it's not.

After digging a bunch of possible causes, and having 100% sure that my env vars was not the problem, I found out the error cause:

I'm usgin AWS Codebuild and they have a place to add env vars on the console. like this: image

and they have a thing called Buildspec, where you writes down commands to build your project and you can do a command called printenv > .env and then a docker-compose build to create the docker image like this: image

ok my .env file is created on the fly, BUT, AWS inject a bunch of env vars on my .env file, that I never have originally defined on my project and two of these vars is causing the problem:

this not work (the way AWS injected these vars)

PHP_BUILD_CONFIGURE_OPTS=--with-curl --with-password-argon2 --with-pdo-pgsql --with-libedit
PYTHON_CONFIGURE_OPTS=--enable-shared --enable-loadable-sqlite-extensions

Because they are writed this way, and not enclosed by double quotes like this:

If AWS had written the variables this way, it would have worked

PHP_BUILD_CONFIGURE_OPTS="--with-curl --with-password-argon2 --with-pdo-pgsql --with-libedit"
PYTHON_CONFIGURE_OPTS="--enable-shared --enable-loadable-sqlite-extensions"

So, I'm pretty sure that AWS should enclose this values in double quotes, but we have no control on this.

And I'm wondering two possibilities:

What can we do?

Thanks!

GrahamCampbell commented 6 months ago

Thanks for getting in touch.

If AWS had written the variables this way, it would have worked

Yes, that is what is required to fix your issue.