Skip to content

Commit

Permalink
src: allow absolute paths for --env-file
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 18, 2023
1 parent 3af6585 commit 430d035
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>

#include <string>
#include <tuple>
Expand Down Expand Up @@ -844,10 +845,16 @@ static ExitCode InitializeNodeWithArgsInternal(
auto file_path = node::Dotenv::GetPathFromArgs(*argv);

if (file_path.has_value()) {
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
std::string path = cwd + kPathSeparator + file_path.value();
CHECK(!per_process::v8_initialized);
per_process::dotenv_file.ParsePath(path);
auto filesystem_path = std::filesystem::path(*file_path);

if (filesystem_path.is_absolute()) {
per_process::dotenv_file.ParsePath(*file_path);
} else {
auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv));
std::string path = cwd + kPathSeparator + file_path.value();
per_process::dotenv_file.ParsePath(path);
}
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
}

Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-dotenv-edge-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

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';
const relativePath = '../fixtures/dotenv/node-options.env';
const absolutePath = path.join(__dirname, relativePath);

describe('.env supports edge cases', () => {

Expand Down Expand Up @@ -35,4 +37,17 @@ describe('.env supports edge cases', () => {
assert.strictEqual(child.code, 0);
});

it('should support absolute paths', async () => {
const code = `
require('assert').strictEqual(process.env.CUSTOM_VARIABLE, 'hello-world');
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${absolutePath}`, '--eval', code ],
{ cwd: __dirname },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});

});

0 comments on commit 430d035

Please sign in to comment.