Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add environment variables specification #52735

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

IlyasShabi
Copy link
Contributor

@IlyasShabi IlyasShabi commented Apr 28, 2024

Following #49148
Add documentation for environment variables specifications

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. process Issues and PRs related to the process subsystem. labels Apr 28, 2024
@anonrig anonrig requested a review from GeoffreyBooth April 28, 2024 15:44

6. **Multiline values**:

* Values enclosed in double, single, or backtick quotes that span multiple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Values enclosed in double, single, or backtick quotes that span multiple
* Values enclosed in quotes described above that span multiple

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may also make sense to mention that unlike shells " and ' behave identially. In shells typically ' doesn't interpolate anything, " allows inline interpolation and backticks execute a command in a subshell.

```cjs
const assert = require('node:assert');
const process = require('node:process');
assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, 'double\nquoted');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to escape all \ characters

Suggested change
assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, 'double\nquoted');
assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, 'double\\nquoted');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@GeoffreyBooth GeoffreyBooth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two general concerns with this PR:

  1. This is a lot of text to introduce and then need to maintain; we’re essentially defining our own specification here. Is this specified somewhere already that we can just link to?

  2. What happens when we want to add a new feature that’s not specified, like string interpolation? We just update this spec? Would that be a breaking change?


```bash
# COMMENTS=work
INLINE_COMMENTS=inline comments # work
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the space after comments part of the value of INLINE_COMMENTS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeoffreyBooth No, we trim spaces in both the key and the value

This section describes how the environment variables file parser reads a file
and sets up the environment variables in Node.js.

1. **Basic parsing**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs generally avoid bold. Rather than this being a numbered list you could make subheadings:

Suggested change
1. **Basic parsing**:
#### Basic parsing

assert.strictEqual(process.env.INLINE_COMMENTS, 'inline comments');
```

3. **Whitespace handling**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would list this second, above comments.

Comment on lines 2505 to 2506
* Any `export` keyword before a key is ignored, allowing compatibility with
shell scripts.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially read this as saying that any lines beginning with export get ignored, not that just the export keyword alone is ignored.

Suggested change
* Any `export` keyword before a key is ignored, allowing compatibility with
shell scripts.
* Any `export` keyword before a key is ignored, allowing compatibility with
shell scripts. The line is parsed as if `export` wasn't there.

@anonrig
Copy link
Member

anonrig commented May 23, 2024

@IlyasShabi are you interested in continuing on this? this is the only missing PR from making dotenv stable.

@IlyasShabi
Copy link
Contributor Author

@anonrig sorry for the delay I was on holidays, yes will work on it this weekend.

@IlyasShabi
Copy link
Contributor Author

@GeoffreyBooth AFAIK, the only documentation for parsing env files is from the dotenv package https://github.com/motdotla/dotenv

For adding new features we should update this document. If the changes are breaking, we need to document them clearly.
Do you have some suggestions ?

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@anonrig
Copy link
Member

anonrig commented May 26, 2024

@redyetidev your comment is not really helpful or contain any actionable item. please provide direct suggestions.

@anonrig
Copy link
Member

anonrig commented May 26, 2024

I have two general concerns with this PR:

  1. This is a lot of text to introduce and then need to maintain; we’re essentially defining our own specification here. Is this specified somewhere already that we can just link to?

  2. What happens when we want to add a new feature that’s not specified, like string interpolation? We just update this spec? Would that be a breaking change?

@GeoffreyBooth there is no direct specification for dotenv other than the documentation. i believe this is the first technical specification of it. i'd like to move with this specification. do you have any blockers?

@avivkeller
Copy link
Member

@redyetidev your comment is not really helpful or contain any actionable item. please provide direct suggestions.

Sorry! I'll do a review instead. I didn't mean for it to come off as unhelpful.

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

I'm not a core collaborator, so these are only my suggestions (and are non-blocking)

@@ -2309,6 +2309,227 @@ import { loadEnvFile } from 'node:process';
loadEnvFile();
```

### Environment variables file parser specification
Copy link
Member

@avivkeller avivkeller May 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this to Environment Variable File Parsing Specification, or something similar?

IMO the same should use a -ing word

@@ -2309,6 +2309,227 @@ import { loadEnvFile } from 'node:process';
loadEnvFile();
```

### Environment variables file parser specification

This section describes how the environment variables file parser reads a file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, maybe replace references to a 'environment variables file parser' to just the 'parser', as it is already noted in the title.

doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Show resolved Hide resolved
#### Comments

* Lines starting with `#` are treated as comments and ignored.
* Inline comments (starting with `#` after a value) are ignored, and do not
Copy link
Member

@avivkeller avivkeller May 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are not inline comments, they are just comments

"A normal" // comment
"An inline" /* comment */ "can have more values afterward"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^^

doc/api/process.md Show resolved Hide resolved
@IlyasShabi
Copy link
Contributor Author

@anonrig IMO this PR is ready to land

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nitpicks, but otherwise LGTM

(I'm not a core collaborator, so this is non-blocking)

doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Show resolved Hide resolved
doc/api/process.md Outdated Show resolved Hide resolved
@anonrig
Copy link
Member

anonrig commented Jun 9, 2024

hey @IlyasShabi can you apply/address the reviews?

doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Outdated Show resolved Hide resolved
doc/api/process.md Outdated Show resolved Hide resolved
@avivkeller avivkeller added the dotenv Issues and PRs related to .env file parsing label Jun 19, 2024
Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few notes, but as a triage member, they are non-blocking

Comment on lines +2314 to +2315
This section describes how environment variables file parser reads a file
and sets up the environment variables in Node.js.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be reworded to:
This section describes how environment variables are read and parsed from a file.

This section describes how environment variables file parser reads a file
and sets up the environment variables in Node.js.
While `.env` files descend from shell scripts that export environment
variables, there are important distinctions in how they handle quoting,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, instead of "they" vs "us", you can write it in way that speaks in general

and sets up the environment variables in Node.js.
While `.env` files descend from shell scripts that export environment
variables, there are important distinctions in how they handle quoting,
spacing, and escaping values.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, you should list these items like so:
... various ____, such as ...

doc/api/process.md Show resolved Hide resolved

#### Basic parsing

* The parser processes input until it finds a newline, splitting each part into
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to use active sentence style, so wording like "Input data is processed ..." is preferred


```bash
# COMMENTS=work
INLINE_COMMENTS=inline comments # work
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto


#### Empty values

* Variables with no value assigned (or example, a key followed by `=`) are set to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"for" examples


#### Empty values

* Variables with no value assigned (or example, a key followed by `=`) are set to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"for" example ...


#### Quoted values

* Single (`'`), double (`"`), and backtick (`` ` ``) quotes are recognized.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't be a bullet

Comment on lines +2493 to +2499
const assert = require('node:assert');
const process = require('node:process');
assert.strictEqual(process.env.EXPAND_NEWLINES, 'expand\nnew\nlines');
assert.strictEqual(process.env.DONT_EXPAND_UNQUOTED,
'dontexpand\\nnewlines');
assert.strictEqual(process.env.DONT_EXPAND_SQUOTED,
'dontexpand\\nnewlines');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, assertions shouldn't be used in the example, but rather just a comment explaining the item's value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. dotenv Issues and PRs related to .env file parsing process Issues and PRs related to the process subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants