dotenv-hs icon indicating copy to clipboard operation
dotenv-hs copied to clipboard

parseFile doesn't seem to escape characters as expected

Open Martinsos opened this issue 2 years ago • 3 comments

Here is an .env file I used to illustrate the potential issue:

TEST1="a\nb"
TEST2='a\nb'
TEST3=a\nb
TEST4="a\\nb"

Now, when I run parseFile "testenv" in ghci, I get the following output: image

Formatted:

[ ("TEST1","anb"),
  ("TEST2","anb"),
  ("TEST3","anb"),
  ("TEST4","a\\nb"),
]

This is not the output I would expect though.

Output I would expect is the following:

[ ("TEST1","a\nb"),
  ("TEST2","a\nb"),
  ("TEST3","a\nb"),
  ("TEST4","a\\nb")  -- OK I am not 100% sure what to expect here, this might be ok.
]

Is my expectation wrong here, or is this potentially a bug in the Dotenv?

I am using dotenv ^>= 0.10.0.

Thanks!

Martinsos avatar Nov 28 '23 16:11 Martinsos

Hi @Martinsos Thanks for reporting that. I think what you mention makes sense but I need to take a look at the parser to clarify if this is expected and what would be the correct way to handle breaking lines.

CristhianMotoche avatar Dec 11 '23 15:12 CristhianMotoche

Thanks @CristhianMotoche !

I don't care much about the "\n" case, but "\n" being parsed as "n" is quite tricky, it makes it impossible to enter a string with a newline inside the env (and for example, Google gives you PEM key as a secret and it has newlines!). The only solution we found so far was to encode it with base64, putting it in .env like that, and then decoding it after being red for dotenv.

Let me know if I can help in some way!

Martinsos avatar Dec 18 '23 17:12 Martinsos

Hey @Martinsos I was able to reproduce the issue. Indeed, it seems something wrong in the parser. FWIW, I think you can add new lines instead of the \n character to represent new lines. For example:

Given this .env:

BAR="a
b"

parseFile will return:

ghci> parseFile ".env"
[("BAR","a\nb")]

I hope that works as a workaround until we find a solution for this issue.

CristhianMotoche avatar Jan 22 '24 02:01 CristhianMotoche