Skip to content

Commit c69d5c3

Browse files
committed
src: handle empty value without newline at EOF
1 parent a723280 commit c69d5c3

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/node_dotenv.cc

+3
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ void Dotenv::ParseContent(const std::string_view input) {
142142

143143
// SAFETY: Content is guaranteed to have at least one character
144144
if (content.empty()) {
145+
// In case the last line is a single key without value
146+
// Example: KEY= (without a newline at the EOF)
147+
store_.insert_or_assign(std::string(key), "");
145148
break;
146149
}
147150

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BASIC=value
2+
EMPTY=

test/parallel/test-dotenv-edge-cases.js

+17
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,21 @@ describe('.env supports edge cases', () => {
8181
assert.strictEqual(child.stderr, '');
8282
assert.strictEqual(child.code, 0);
8383
});
84+
85+
it('should handle empty value without a newline at the EOF', async () => {
86+
// Ref: https://github.com/nodejs/node/issues/52466
87+
const code = `
88+
process.loadEnvFile('./eof-without-value.env');
89+
require('assert').strictEqual(process.env.BASIC, 'value');
90+
require('assert').strictEqual(process.env.EMPTY, '');
91+
`.trim();
92+
const child = await common.spawnPromisified(
93+
process.execPath,
94+
[ '--eval', code ],
95+
{ cwd: fixtures.path('dotenv') },
96+
);
97+
assert.strictEqual(child.stdout, '');
98+
assert.strictEqual(child.stderr, '');
99+
assert.strictEqual(child.code, 0);
100+
});
84101
});

0 commit comments

Comments
 (0)