godotenv icon indicating copy to clipboard operation
godotenv copied to clipboard

Doesn't support multiline vars

Open esttorhe opened this issue 5 years ago • 12 comments

If I set something like this:

KEY='this is
a multiline
variable'

godotenv returns:

Can't separate key from value

Even though that's a valid variable assignment.

I'm currently at work; will attempt a fix later this week.

esttorhe avatar Dec 18 '18 17:12 esttorhe

While most of the discussion on this is happening in #65 I'd rather talk about it here in case the ultimate solution is a different PR.

Given I use the ruby dotenv implementation as the informal spec a solution should follow the form of https://github.com/bkeepers/dotenv/commit/b3bd6357f80981941d78e51fe86aeaf66cdcc60f and expand newlines within double quotes only.

joho avatar Jan 29 '19 00:01 joho

Thanks for pointing out the spec @joho

I'll try to amend my PR today addressing this

esttorhe avatar Jan 29 '19 08:01 esttorhe

BTW I'm aware the internals need a fair overhaul and if that's a job you don't necessarily want to take on don't feel like you have to,

joho avatar Jan 29 '19 09:01 joho

No need to worry man; I'm glad you took the time to put together this library 😊

I wouldn't mind tweaking the internals; although I would take a jab at it on a different PR

esttorhe avatar Jan 29 '19 09:01 esttorhe

@joho this faf711c01668ace5365389b124816a2e06778197 should handle the multiline «similar» to the spec on dotenv.

Later we can/should change the logic to rely on regular expressions as well.

For now it will look for " as the delimiters for multiline values

esttorhe avatar Jan 29 '19 15:01 esttorhe

Given I use the ruby dotenv implementation as the informal spec a solution should follow the form of bkeepers/dotenv@b3bd635 and expand newlines within double quotes only.

I would like to point out that it's possible to use \n in double quoted strings to have new lines in env vars. This wasn't clear to me when checking the readme and I realised it only when reading the code. It's not as nice as true multiline strings but at least it unblocks me right now :innocent:

dschmidt avatar Aug 21 '19 18:08 dschmidt

Any updates on this? I am still having issues with the following env var:

EXAMPLE="-----BEGIN RSA PRIVATE KEY-----\n
abc\n
123\n
-----END RSA PRIVATE KEY-----"

I am still getting Failed to setup application - cannot continue: Can't separate key from value

inoda avatar Oct 22 '19 16:10 inoda

inoda if you base64encode it for now and store it as that in the .env file, its a happy workaround, then just base64decode it when you need to use the env variable

jeffwalsh avatar Dec 06 '19 00:12 jeffwalsh

@inoda You can probably just remove the actual line breaks (keeping the \n) and it should work.

dschmidt avatar Dec 06 '19 11:12 dschmidt

I haven't yet had a close look at #118 but that may address it

joho avatar Nov 08 '20 01:11 joho

https://github.com/bkeepers/dotenv/pull/423 some changes in the upstream lib to maintain compatibility with

joho avatar Apr 10 '21 03:04 joho

Screen Shot 2021-07-31 at 10 57 50 PM this works for me

thinh090 avatar Jul 31 '21 15:07 thinh090

This is now fixed via #156 (which was an expansion of #118)

Thanks @x1unix for the bulk of the work, and @austinsasko and @coolaj86 for some key suggestions.

Sorry it took so long... go's no longer my daily driver, startup life, parent life, and frankly i've always been bad at "chores"

Release plan is to let it sit on main for a while, see if anyone raises regressions. I might do a prerelease version number, might not. Then will release as a minor version bump.

joho avatar Jan 27 '23 02:01 joho

Pre-release at https://github.com/joho/godotenv/releases/tag/v1.5.0-beta.0

joho avatar Jan 27 '23 02:01 joho

I've received no complaints or bug reports about the pre-release, so I shall act as though absence of evidence is evidence of absence.

Pushing actual release today.

joho avatar Feb 04 '23 00:02 joho

It seems that multiline var reading works very well. But writing back to file NOT working. If you just read some file with multiline var, unmarshal to map and write file back, it will be written \n instead of real newline (byte10). Problem is that godotenv.write is calling 'Marshal', then 'Marshal' calls 'doubleQuoteEscape', and doubleQuoteEscape function doing exactly that for some reason, if c=='\n', toReplace=\n.

asainnp avatar Jan 13 '24 05:01 asainnp