Skip to content

Equals Symbol Causes "Failed to parse dotenv file due to an invalid name". #319

Closed

Description

@bljdavidson

How do you escape the = symbol? It's causing the "Failed to parse dotenv file due to an invalid name" error because of it. It's part of a multiline value.

SERVICE_ACCOUNT_JSON = '{
    "type": "service_account",
    "project_id": "project id",
    "private_key": "-----BEGIN PRIVATE KEY-----\nMIIE ..... DgQw==\n-----END PRIVATE KEY-----\n",
    "client_email": "[email protected]"
    "client_id": "1234567890123456789",
    .....
    .....
}',

It is failing at the 2 == symbols at the end of the "private_key" value. If I remove the 2 symbols, it works, but unfortunately, it won't authenticate.

Activity

  1. GrahamCampbell commented on Jan 7, 2019

    @GrahamCampbell
    Collaborator

    I think the colon in the name is the error here surely?

  2. hexus commented on Jan 7, 2019

    @hexus

    Also, the single quotes don't look right.

    I do get some weird behaviour with multiline variables at the moment:

    TEST="test
         test=="test"
         test"

    Parses as:

      ["TEST"]=>
      string(41) "test
                test==
                test""
    

    If I try escaping the quotes:

    TEST="test
         test==\"test\"
         test"

    I get a fatal error:

    Fatal error: Uncaught Dotenv\Exception\InvalidFileException: Failed to parse dotenv file due to a quote parsing error (PREG_BACKTRACK_LIMIT_ERROR). Failed at ["test]. in ..\vlucas\phpdotenv\src\Parser.php:163
    Stack trace:
    #0 ..\vlucas\phpdotenv\src\Regex\Error.php(80): Dotenv\Parser::Dotenv\{closure}('PREG_BACKTRACK_...')
    #1 ..\vlucas\phpdotenv\src\Parser.php(166): Dotenv\Regex\Error->mapError(Object(Closure))
    #2 ..\vlucas\phpdotenv\src\Parser.php(110): Dotenv\Parser::processQuotedValue('"test\n     test...')
    #3 ..\vlucas\phpdotenv\src\Parser.php(28): Dotenv\Parser::sanitiseValue('"test\n     test...')
    #4 ..\vlucas\phpdotenv\src\Loader.php(154): Dotenv\Parser::parse('TEST="test\n    ...')
    #5 ..\vlucas\phpdotenv\src\Parser.php on line 163
    
  3. hexus commented on Jan 7, 2019

    @hexus

    More interestingly, this parses okay but still includes the surrounding quotes (this seems to happen for single-line values without spaces, too):

    TEST="test
         test\"test\"
         test"

    Parses as:

    string(21) ""test
         test"test""
    
  4. bljdavidson commented on Jan 7, 2019

    @bljdavidson
    Author

    I think the colon in the name is the error here surely?

    @GrahamCampbell Sorry this was a typo on my behalf. I've removed that now.

    @hexus I have tried with and without the quotes. I have also tried to escape the = symbol with the backslash but to no avail. As soon as I remove both = symbols, it works but of course it fails to authenticate as they're needed.

    I really appreciate both your help :)

  5. GrahamCampbell commented on Jan 7, 2019

    @GrahamCampbell
    Collaborator

    Right, it looks like the implementation of multiline variables does not support quotes being contained within the string. If we look at the implementation (https://github.com/vlucas/phpdotenv/blob/v3.1.0/src/Lines.php), a multi-line variable's final line is defined to be the first line containing a quote without an equals.

  6. GrahamCampbell commented on Jan 7, 2019

    @GrahamCampbell
    Collaborator

    @bljdavidson the file provided in the OP is still invalid syntax as far as I can see?

  7. GrahamCampbell commented on Jan 7, 2019

    @GrahamCampbell
    Collaborator

    @hexus Thanks for those test cases. I'll incorporate them into the test suite.

  8. GrahamCampbell commented on Jan 7, 2019

    @GrahamCampbell
    Collaborator

    I won't have time to look into fixing this until Sunday I'm afraid. I've just had a go now, and a good solution isn't immediately obvious to me.

  9. bljdavidson commented on Jan 7, 2019

    @bljdavidson
    Author

    @GrahamCampbell Thanks so much for your help. In the mean time, I have tried to make the JSON into a single line and 2 things happen:

    • If I keep the variable name as KEY = VALUE, the value just becomes an empty string.
    • If I change it to KEY: VALUE, I get the same error, failing at the = symbol.

    So I'm not sure if it's only related to just multiline :/.

    P.S. Please forgive me if I've misunderstood anything, as I'm not advanced in PHP :).

    I won't have time to look into fixing this until Sunday I'm afraid. I've just had a go now, and a good solution isn't immediately obvious to me.

    Awesome. Nice work 👍

  10. GrahamCampbell commented on Jan 26, 2019

    @GrahamCampbell
    Collaborator

    @bljdavidson @hexus @Korbeil I've prepared a fix at #322. Please let me know what you think.

  11. GrahamCampbell commented on Jan 26, 2019

    @GrahamCampbell
    Collaborator

    @bljdavidson The following should be the format you want:

    SERVICE_ACCOUNT_JSON="{
        \"type\": \"service_account\",
        \"project_id\": \"project id\",
        \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIE ..... DgQw==\\n-----END PRIVATE KEY-----\\n\",
        \"client_email\": \"[email protected]\"
        \"client_id\": \"1234567890123456789\",
        .....
        .....
    }"
    

    Notice how I've escaped all double quotes and backslashes.

  12. GrahamCampbell commented on Jan 26, 2019

    @GrahamCampbell
    Collaborator

    @hexus Your example of:

    TEST="test
         test\"test\"
         test"
    

    should now parse as:

    test
         test"test"
         test
    
  13. hexus commented on Jan 26, 2019

    @hexus

    Fabulous 🎉

    Thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions