Skip to content

Commit

Permalink
src: do not coerce dotenv paths
Browse files Browse the repository at this point in the history
PR-URL: #51425
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
tniessen authored and targos committed Feb 15, 2024
1 parent 7420a7d commit 119e045
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,9 @@ static ExitCode InitializeNodeWithArgsInternal(

if (!file_paths.empty()) {
CHECK(!per_process::v8_initialized);
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));

for (const auto& file_path : file_paths) {
std::string path = cwd + kPathSeparator + file_path;
auto path_exists = per_process::dotenv_file.ParsePath(path);
bool path_exists = per_process::dotenv_file.ParsePath(file_path);

if (!path_exists) errors->push_back(file_path + ": not found");
}
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const common = require('../common');
const assert = require('node:assert');
const path = require('node:path');
const { describe, it } = require('node:test');

const validEnvFilePath = '../fixtures/dotenv/valid.env';
Expand All @@ -25,6 +26,18 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.code, 0);
});

it('supports absolute paths', async () => {
const code = `
require('assert').strictEqual(process.env.BASIC, 'basic');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${path.resolve(__dirname, validEnvFilePath)}`, '--eval', code ],
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

it('should handle non-existent .env file', async () => {
const code = `
require('assert').strictEqual(1, 1)
Expand Down

0 comments on commit 119e045

Please sign in to comment.