craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.22k stars 626 forks source link

Failed to parse dotenv file. Encountered an unexpected escape sequence at ["Foo\ Bar"]. #10274

Closed Bilge closed 2 years ago

Bilge commented 2 years ago

Description

./craft setup produces an invalid dotenv file if the expression contains a backslash.

So far as I can tell, the setup enters what I would consider to be the "correct" entry in the dotenv file, that is: DB_DATABASE="Foo\ Bar". At least, this would be correct for Symfony\Dotenv, but it seems this project is using a different Dotenv\Dotenv which fails to parse this.

$ ./craft setup
PHP Fatal error:  Uncaught Dotenv\Exception\InvalidFileException: Failed to parse dotenv file. Encountered an unexpected escape sequence at ["Foo\ Bar"]. in vendor\vlucas\phpdotenv\src\Parser\Parser.php:30
Stack trace:
#0 vendor\graham-campbell\result-type\src\Error.php(118): Dotenv\Parser\Parser::Dotenv\Parser\{closure}('Encountered an ...')
#1 vendor\vlucas\phpdotenv\src\Parser\Parser.php(31): GrahamCampbell\ResultType\Error->mapError(Object(Closure))
#2 vendor\vlucas\phpdotenv\src\Dotenv.php(222): Dotenv\Parser\Parser->parse('# The environme...')
#3 vendor\vlucas\phpdotenv\src\Dotenv.php(237): Dotenv\Dotenv->load()
#4 bootstrap.php(15): Dotenv\Dotenv->safeLoad()
#5 craft(8): require('C:\\Users\\Bilge\\...')
#6 {main}
  thrown in vendor\vlucas\phpdotenv\src\Parser\Parser.php on line 30

Since this library appears to be attempting its own backslash unescaping, for reasons that remain unclear, perhaps craft should actually write DB_DATABASE="Foo\\ Bar".

Steps to reproduce

  1. ./craft setup
  2. At the database name prompt for a Postgres database, enter Foo\ Bar as the database name.

Additional info

brandonkelly commented 2 years ago

You should be able to just enter Foo Bar without the \. Have you tried that?

Bilge commented 2 years ago

@brandonkelly You definitely cannot because the Postgres driver will not receive the part after the space as the database name otherwise.

Testing database credentials ... failed: SQLSTATE[08006] [7] missing "=" after "Bar" in connection info string

brandonkelly commented 2 years ago

Gotcha, thanks. Able to reproduce and fixed for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to "dev-develop as 3.7.26" and run composer update.

brandonkelly commented 2 years ago

Craft 3.7.27 is out now with that fix.

peirix commented 11 months ago

Just encountered this problem again now with Craft 3.8.16 with a smtp password in my .env file that contains \

Failed at ["\1a:~123@tQ+Y;9S<#CB"]

peirix commented 11 months ago

And just realised that it needs to be escaped. So just escaping that backslash did the trick.

brandonkelly commented 11 months ago

@peirix Glad you got it sorted!