nodejs / nodejs/node

--env-file does not support inner quotes (does not behave like dotenv)

未关闭
#54,134 20 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

confirmed-bug dotenv
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v20.16.0

Platform
Darwin <redacted> 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:14:38 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6020 arm64
Subsystem

No response

What steps will reproduce the bug?

inspired by this comment: https://github.com/nodejs/node/pull/50814#issuecomment-1817949911

  1. create an .env file:

    INNER_QUOTES="1: foo'bar"baz`qux"
    INNER_QUOTES_WITH_NEWLINE="2: foo bar\ni am "on" newline, 'yo'"
    
  2. test with dotenv:

    // envtest-dotenv.js
    require("dotenv").config();
    console.log(process.env.INNER_QUOTES);
    console.log(process.env.INNER_QUOTES_WITH_NEWLINE);
    

    $ node envtest-dotenv.js

  3. test with --env-file

    // envtest.js
    console.log(process.envconsole.log(process.env.INNER_QUOTES);
    console.log(process.env.INNER_QUOTES_WITH_NEWLINE);
    

    $ node --env-file=.env envtest.js

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

outputs should be the same.

// dotenv output:

1: foo'bar"baz`qux
2: foo bar
i am "on" newline, 'yo'
What do you see instead?

output are different, node native terminates at the first occurrence of the double quote:

// --env-file output
1: foo'bar
2: foo bar
i am

compare that to dotenv:

1: foo'bar"baz`qux
2: foo bar
i am "on" newline, 'yo'
Additional information
  • this makes it impossible to have env-variables that contain all three quotes (", ' and `) and newlines.
  • There is no alternative to inner quotes, because escaping quotes have been rejected https://github.com/nodejs/node/pull/50814
  • It's very problematic in many cases since depending on the quotes you are using, you need to use different outer quotes
  • i noticed that because i am writing some tooling that creates those .env files based on some other configuration. I just used double quotes as outer quotes and noticed that it fails when the string contains double quotes
  • be aware that dotenv also has a bug, when your string contains an actual newline (not "\n") AND has inner quotes, it will cut the string off at the line break. It works if the string does not contain inner quotes. It also works if you instead use "\n" instead of an actual line break. The output will still be the same and will have a real line break in it (as long as you put outer quotes around the string. without quotes, the \n will be literally in the output as "\n")

And

  • since the format is so problematic, maybe its time for a better format?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,使用 node --env-file=.env envtest.js 重现 .env 示例,并将输出与 dotenv 进行比较。跟踪 --env-file 解析的入口点和现有测试,然后让原生解析器按照预期输出一致地保留内部引号和换行符,并为这些情况添加覆盖测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nodejs
领域
cli
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。