src: do not coerce dotenv paths

PR-URL: https://github.com/nodejs/node/pull/51425
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Tobias Nießen 2024-01-12 16:42:34 +01:00 committed by GitHub
parent cf68d006c0
commit d44814ca68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -869,11 +869,9 @@ static ExitCode InitializeNodeWithArgsInternal(
if (!file_paths.empty()) { if (!file_paths.empty()) {
CHECK(!per_process::v8_initialized); CHECK(!per_process::v8_initialized);
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
for (const auto& file_path : file_paths) { for (const auto& file_path : file_paths) {
std::string path = cwd + kPathSeparator + file_path; bool path_exists = per_process::dotenv_file.ParsePath(file_path);
auto path_exists = per_process::dotenv_file.ParsePath(path);
if (!path_exists) errors->push_back(file_path + ": not found"); if (!path_exists) errors->push_back(file_path + ": not found");
} }

View File

@ -2,6 +2,7 @@
const common = require('../common'); const common = require('../common');
const assert = require('node:assert'); const assert = require('node:assert');
const path = require('node:path');
const { describe, it } = require('node:test'); const { describe, it } = require('node:test');
const validEnvFilePath = '../fixtures/dotenv/valid.env'; const validEnvFilePath = '../fixtures/dotenv/valid.env';
@ -25,6 +26,18 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.code, 0); 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 () => { it('should handle non-existent .env file', async () => {
const code = ` const code = `
require('assert').strictEqual(1, 1) require('assert').strictEqual(1, 1)