GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: safely remove the last line from dotenv · nodejs/node@d894cb7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_dotenv.cc
  • test
    • fixtures/dotenv
      • no-final-newline-single-quotes.env
      • no-final-newline.env
    • parallel
      • test-dotenv-edge-cases.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_dotenv.cc‎

Copy file name to clipboardExpand all lines: src/node_dotenv.cc
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ void Dotenv::ParseContent(const std::string_view input) {
182182
}
183183

184184
store_.insert_or_assign(std::string(key), multi_line_value);
185-
content.remove_prefix(content.find('\n', closing_quote + 1));
185+
auto newline = content.find('\n', closing_quote + 1);
186+
if (newline != std::string_view::npos) {
187+
content.remove_prefix(newline);
188+
}
186189
continue;
187190
}
188191
}
@@ -210,7 +213,10 @@ void Dotenv::ParseContent(const std::string_view input) {
210213
store_.insert_or_assign(std::string(key), value);
211214
// Select the first newline after the closing quotation mark
212215
// since there could be newline characters inside the value.
213-
content.remove_prefix(content.find('\n', closing_quote + 1));
216+
auto newline = content.find('\n', closing_quote + 1);
217+
if (newline != std::string_view::npos) {
218+
content.remove_prefix(newline);
219+
}
214220
}
215221
} else {
216222
// Regular key value pair.
Collapse file

‎test/fixtures/dotenv/no-final-newline-single-quotes.env‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC='basic'
Collapse file

‎test/fixtures/dotenv/no-final-newline.env‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BASIC="basic"
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-dotenv-edge-cases.js
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const fixtures = require('../common/fixtures');
88

99
const validEnvFilePath = '../fixtures/dotenv/valid.env';
1010
const nodeOptionsEnvFilePath = '../fixtures/dotenv/node-options.env';
11+
const noFinalNewlineEnvFilePath = '../fixtures/dotenv/no-final-newline.env';
12+
const noFinalNewlineSingleQuotesEnvFilePath = '../fixtures/dotenv/no-final-newline-single-quotes.env';
1113

1214
describe('.env supports edge cases', () => {
1315
it('supports multiple declarations, including optional ones', async () => {
@@ -148,4 +150,24 @@ describe('.env supports edge cases', () => {
148150
assert.strictEqual(child.stderr, '');
149151
assert.strictEqual(child.code, 0);
150152
});
153+
154+
it('should handle file without a final newline', async () => {
155+
const code = `
156+
require('assert').strictEqual(process.env.BASIC, 'basic');
157+
`.trim();
158+
const child = await common.spawnPromisified(
159+
process.execPath,
160+
[ `--env-file=${path.resolve(__dirname, noFinalNewlineEnvFilePath)}`, '--eval', code ],
161+
);
162+
163+
const SingleQuotesChild = await common.spawnPromisified(
164+
process.execPath,
165+
[ `--env-file=${path.resolve(__dirname, noFinalNewlineSingleQuotesEnvFilePath)}`, '--eval', code ],
166+
);
167+
168+
assert.strictEqual(child.stderr, '');
169+
assert.strictEqual(child.code, 0);
170+
assert.strictEqual(SingleQuotesChild.stderr, '');
171+
assert.strictEqual(SingleQuotesChild.code, 0);
172+
});
151173
});

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.