Skip to content

Commit 59272da

Browse files
src: swap dotenv and config file parsing order
Signed-off-by: Marco Ippolito <marcoippolito54@gmail.com> PR-URL: #63035 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 25f80fb commit 59272da

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

doc/api/cli.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,12 +1143,12 @@ node --import amaro/strip --watch-path=src --watch-preserve-output --test-isolat
11431143
The priority in configuration is as follows:
11441144

11451145
1. NODE\_OPTIONS and command-line options
1146-
2. Configuration file
1147-
3. Dotenv NODE\_OPTIONS
1146+
2. Dotenv NODE\_OPTIONS
1147+
3. Configuration file
11481148

11491149
Values in the configuration file will not override the values in the environment
1150-
variables and command-line options, but will override the values in the `NODE_OPTIONS`
1151-
env file parsed by the `--env-file` flag.
1150+
variables, command-line options, or the `NODE_OPTIONS` env file parsed by the
1151+
`--env-file` flag.
11521152

11531153
Keys cannot be duplicated within the same or different namespaces.
11541154

src/node.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ static ExitCode InitializeNodeWithArgsInternal(
870870
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
871871

872872
std::string node_options;
873+
std::string node_options_from_dotenv;
873874
auto env_files = node::Dotenv::GetDataFromArgs(*argv);
874875

875876
if (!env_files.empty()) {
@@ -896,7 +897,8 @@ static ExitCode InitializeNodeWithArgsInternal(
896897
}
897898
}
898899

899-
per_process::dotenv_file.AssignNodeOptionsIfAvailable(&node_options);
900+
per_process::dotenv_file.AssignNodeOptionsIfAvailable(
901+
&node_options_from_dotenv);
900902
}
901903

902904
std::string node_options_from_config;
@@ -932,9 +934,10 @@ static ExitCode InitializeNodeWithArgsInternal(
932934
errors->emplace_back("The number of NODE_OPTIONS doesn't match "
933935
"the number of flags in the config file");
934936
}
935-
node_options += node_options_from_config;
936937
}
937938

939+
node_options = node_options_from_config + node_options_from_dotenv;
940+
938941
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
939942
bool should_parse_node_options =
940943
!(flags & ProcessInitializationFlags::kDisableNodeOptionsEnv);

src/node_dotenv.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) const {
356356
auto match = store_.find("NODE_OPTIONS");
357357

358358
if (match != store_.end()) {
359-
*node_options = match->second;
359+
*node_options = " " + match->second;
360360
}
361361
}
362362

test/parallel/test-config-file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,16 @@ test('should throw an error when a flag is declared twice', async () => {
8686
assert.strictEqual(result.code, 9);
8787
});
8888

89-
test('should override env-file', onlyWithAmaroAndNodeOptions, async () => {
89+
test('should not override env-file', onlyWithAmaroAndNodeOptions, async () => {
9090
const result = await spawnPromisified(process.execPath, [
9191
'--no-warnings',
9292
`--experimental-config-file=${fixtures.path('rc/strip-types.json')}`,
9393
'--env-file', fixtures.path('dotenv/node-options-no-tranform.env'),
9494
fixtures.path('typescript/ts/test-typescript.ts'),
9595
]);
96-
assert.strictEqual(result.stderr, '');
97-
assert.match(result.stdout, /Hello, TypeScript!/);
98-
assert.strictEqual(result.code, 0);
96+
assert.match(result.stderr, /SyntaxError/);
97+
assert.strictEqual(result.stdout, '');
98+
assert.strictEqual(result.code, 1);
9999
});
100100

101101
test('should not override NODE_OPTIONS', onlyWithAmaro, async () => {

0 commit comments

Comments
 (0)