From bb04e310b34d880fcce413b9d097bab9c28cfd60 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 18 Aug 2023 15:18:13 -0400 Subject: [PATCH 1/2] src: allow absolute paths for `--env-file` --- src/node.cc | 9 +++++++-- src/node_dotenv.cc | 8 ++++---- src/node_dotenv.h | 3 ++- test/parallel/test-dotenv-edge-cases.js | 22 +++++++++++++++++++--- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/node.cc b/src/node.cc index 14e27806a5d3b2..e83fcf9716caa9 100644 --- a/src/node.cc +++ b/src/node.cc @@ -124,6 +124,7 @@ #include #include #include +#include #include #include @@ -858,8 +859,12 @@ static ExitCode InitializeNodeWithArgsInternal( auto cwd = Environment::GetCwd(Environment::GetExecPath(*argv)); for (const auto& file_path : file_paths) { - std::string path = cwd + kPathSeparator + file_path; - per_process::dotenv_file.ParsePath(path); + if (file_path.is_absolute()) { + per_process::dotenv_file.ParsePath(file_path.string()); + } else { + std::string path = cwd + kPathSeparator + file_path.string(); + per_process::dotenv_file.ParsePath(path); + } } per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options); diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 0633ef51269959..b83485ad160522 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -8,20 +8,20 @@ namespace node { using v8::NewStringType; using v8::String; -std::vector Dotenv::GetPathFromArgs( +std::vector Dotenv::GetPathFromArgs( const std::vector& args) { const auto find_match = [](const std::string& arg) { const std::string_view flag = "--env-file"; return strncmp(arg.c_str(), flag.data(), flag.size()) == 0; }; - std::vector paths; + std::vector paths; auto path = std::find_if(args.begin(), args.end(), find_match); while (path != args.end()) { auto equal_char = path->find('='); if (equal_char != std::string::npos) { - paths.push_back(path->substr(equal_char + 1)); + paths.push_back(std::filesystem::path(path->substr(equal_char + 1))); } else { auto next_path = std::next(path); @@ -29,7 +29,7 @@ std::vector Dotenv::GetPathFromArgs( return paths; } - paths.push_back(*next_path); + paths.push_back(std::filesystem::path(*next_path)); } path = std::find_if(++path, args.end(), find_match); diff --git a/src/node_dotenv.h b/src/node_dotenv.h index ee74f9ff84a353..ff2334547159f1 100644 --- a/src/node_dotenv.h +++ b/src/node_dotenv.h @@ -5,6 +5,7 @@ #include "util-inl.h" +#include #include namespace node { @@ -22,7 +23,7 @@ class Dotenv { void AssignNodeOptionsIfAvailable(std::string* node_options); void SetEnvironment(Environment* env); - static std::vector GetPathFromArgs( + static std::vector GetPathFromArgs( const std::vector& args); private: diff --git a/test/parallel/test-dotenv-edge-cases.js b/test/parallel/test-dotenv-edge-cases.js index ae2b3dc2a35f35..fb5d055c5c9020 100644 --- a/test/parallel/test-dotenv-edge-cases.js +++ b/test/parallel/test-dotenv-edge-cases.js @@ -2,10 +2,13 @@ const common = require('../common'); const assert = require('node:assert'); +const path = require('node:path'); +const fixtures = require('../common/fixtures'); const { describe, it } = require('node:test'); -const validEnvFilePath = '../fixtures/dotenv/valid.env'; -const nodeOptionsEnvFilePath = '../fixtures/dotenv/node-options.env'; +const validEnvFilePath = path.relative(__dirname, fixtures.path('dotenv/valid.env')); +const absolutePath = fixtures.path('dotenv/node-options.env'); +const relativePath = path.relative(__dirname, absolutePath); describe('.env supports edge cases', () => { @@ -18,7 +21,7 @@ describe('.env supports edge cases', () => { `.trim(); const child = await common.spawnPromisified( process.execPath, - [ `--env-file=${nodeOptionsEnvFilePath}`, `--env-file=${validEnvFilePath}`, '--eval', code ], + [ `--env-file=${relativePath}`, `--env-file=${validEnvFilePath}`, '--eval', code ], { cwd: __dirname }, ); assert.strictEqual(child.stderr, ''); @@ -51,4 +54,17 @@ describe('.env supports edge cases', () => { assert.strictEqual(child.stderr, ''); 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); + }); }); From 1e0ca25e8cdcdcc72c82e4dd47cdde2670d49241 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 21 Aug 2023 11:29:29 -0400 Subject: [PATCH 2/2] build: add ldlibs for std::filesystem --- common.gypi | 1 + 1 file changed, 1 insertion(+) diff --git a/common.gypi b/common.gypi index 071a27afc2fd59..197ae28c29639c 100644 --- a/common.gypi +++ b/common.gypi @@ -402,6 +402,7 @@ 'cflags_cc': [ '-fno-rtti', '-fno-exceptions', '-std=gnu++17' ], 'defines': [ '__STDC_FORMAT_MACROS' ], 'ldflags': [ '-rdynamic' ], + 'ldlibs': [ '-lstdc++fs' ], 'target_conditions': [ # The 1990s toolchain on SmartOS can't handle thin archives. ['_type=="static_library" and OS=="solaris"', {