direnv icon indicating copy to clipboard operation
direnv copied to clipboard

dotenv breaks for newlines in single-quoted string values (unlike the real dotenv)

Open dbarnett opened this issue 4 months ago • 2 comments

Describe the bug If a .env file contains a single-quoted string value with line breaks in it, the dotenv tool (and bash/zsh) can process the literal line break and load the value fine, but direnv's dotenv command chokes parsing the line after it.

To Reproduce

$ echo "X='abc
123'" > .env
$ echo "dotenv" > .envrc
$ direnv allow

See the error direnv: error invalid line: 123'.

Expected behavior Should load a variable X containing a multiline value like dotenv does.

Environment

  • OS: macOS Sequoia 15.5
  • Shell: zsh
  • Direnv version 2.37.1

Additional context This is extra frustrating because I can't seem to find ANY way to represent literal newlines in a .env file that both direnv and dotenv will support with the same semantics...

dbarnett avatar Aug 23 '25 04:08 dbarnett

Does this not work for you?

... you can double quote strings and use the \n character:

https://github.com/motdotla/dotenv?tab=readme-ov-file#multiline-values

dancrumb avatar Aug 26 '25 15:08 dancrumb

Sadly no, not for all cases. I'm using single-quoted strings because they're the only way dotenv can properly serialize env values like

{
  "somekey": "a \"b\n\" c"
}

but after a lot of wrestling with it I haven't found a way that both direnv and real dotenv can safely represent it, if direnv won't support the same single-quote semantics like this.

dbarnett avatar Aug 30 '25 17:08 dbarnett