Skip to content

Commit 775234b

Browse files
src: allow empty --experimental-config-file
1 parent f6464c5 commit 775234b

File tree

6 files changed

+56
-12
lines changed

6 files changed

+56
-12
lines changed

doc/api/cli.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ added:
10141014
10151015
Enable experimental import support for `.node` addons.
10161016

1017-
### `--experimental-config-file=config`
1017+
### `--experimental-config-file[=config]`
10181018

10191019
<!-- YAML
10201020
added:
@@ -1025,6 +1025,8 @@ added:
10251025
> Stability: 1.0 - Early development
10261026
10271027
If present, Node.js will look for a configuration file at the specified path.
1028+
If the path is not specified, Node.js will look for a `node.config.json` file
1029+
in the current working directory (equivalent to `--experimental-default-config-file`).
10281030
Node.js will read the configuration file and apply the settings. The
10291031
configuration file should be a JSON file with the following structure. `vX.Y.Z`
10301032
in the `$schema` must be replaced with the version of Node.js you are using.
@@ -1131,9 +1133,10 @@ added:
11311133

11321134
> Stability: 1.0 - Early development
11331135
1134-
If the `--experimental-default-config-file` flag is present, Node.js will look for a
1136+
This flag is an alias for `--experimental-config-file` without an argument.
1137+
If present, Node.js will look for a
11351138
`node.config.json` file in the current working directory and load it as a
1136-
as configuration file.
1139+
configuration file.
11371140

11381141
### `--experimental-eventsource`
11391142

doc/node.1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,10 @@ It is possible to run code containing inline types unless the
602602
.It Fl -experimental-addon-modules
603603
Enable experimental import support for \fB.node\fR addons.
604604
.
605-
.It Fl -experimental-config-file Ns = Ns Ar config
605+
.It Fl -experimental-config-file Ns Op = Ns Ar config
606606
If present, Node.js will look for a configuration file at the specified path.
607+
If the path is not specified, Node.js will look for a \fBnode.config.json\fR file
608+
in the current working directory (equivalent to \fB--experimental-default-config-file\fR).
607609
Node.js will read the configuration file and apply the settings. The
608610
configuration file should be a JSON file with the following structure. \fBvX.Y.Z\fR
609611
in the \fB$schema\fR must be replaced with the version of Node.js you are using.
@@ -689,9 +691,10 @@ Node.js will not sanitize or perform validation on the user-provided configurati
689691
so \fBNEVER\fR use untrusted configuration files.
690692
.
691693
.It Fl -experimental-default-config-file
692-
If the \fB--experimental-default-config-file\fR flag is present, Node.js will look for a
694+
This flag is an alias for \fB--experimental-config-file\fR without an argument.
695+
If present, Node.js will look for a
693696
\fBnode.config.json\fR file in the current working directory and load it as a
694-
as configuration file.
697+
configuration file.
695698
.
696699
.It Fl -experimental-eventsource
697700
Enable exposition of EventSource Web API on the global scope.

lib/internal/process/pre_execution.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ function setupSQLite() {
382382

383383
function initializeConfigFileSupport() {
384384
if (getOptionValue('--experimental-default-config-file') ||
385-
getOptionValue('--experimental-config-file')) {
385+
getOptionValue('--experimental-config-file') ||
386+
getOptionValue('--experimental-config-file=')) {
386387
emitExperimentalWarning('--experimental-config-file');
387388
}
388389
}

src/node_config_file.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ std::optional<std::string_view> ConfigReader::GetDataFromArgs(
1717
for (auto it = args.begin(); it != args.end(); ++it) {
1818
if (*it == flag_path) {
1919
// Case: "--experimental-config-file foo"
20-
if (auto next = std::next(it); next != args.end()) {
20+
if (auto next = std::next(it);
21+
next != args.end() && !next->starts_with("-")) {
2122
return *next;
2223
}
24+
// Case: "--experimental-config-file" without argument, use default
25+
has_default_config_file = true;
2326
} else if (it->starts_with(flag_path)) {
2427
// Case: "--experimental-config-file=foo"
2528
if (it->size() > flag_path.size() && (*it)[flag_path.size()] == '=') {
26-
return std::string_view(*it).substr(flag_path.size() + 1);
29+
std::string_view value =
30+
std::string_view(*it).substr(flag_path.size() + 1);
31+
if (value.empty()) {
32+
has_default_config_file = true;
33+
} else {
34+
return value;
35+
}
2736
}
2837
} else if (*it == default_file || it->starts_with(default_file)) {
2938
has_default_config_file = true;

src/node_options.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,12 +868,14 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
868868
"set environment variables from supplied file",
869869
&EnvironmentOptions::optional_env_file);
870870
Implies("--env-file-if-exists", "[has_env_file_string]");
871-
AddOption("--experimental-config-file",
871+
AddOption("--experimental-config-file=",
872872
"set config file from supplied file",
873873
&EnvironmentOptions::experimental_config_file_path);
874-
AddOption("--experimental-default-config-file",
875-
"set config file from default config file",
874+
AddOption("--experimental-config-file",
875+
"set default config file",
876876
&EnvironmentOptions::experimental_default_config_file);
877+
AddAlias("--experimental-config-file <arg>", "--experimental-config-file=");
878+
AddAlias("--experimental-default-config-file", "--experimental-config-file");
877879
AddOption("--test",
878880
"launch test runner on startup",
879881
&EnvironmentOptions::test_runner,

test/parallel/test-config-file.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,32 @@ test('should use node.config.json as default', onlyIfNodeOptionsSupport, async (
371371
assert.strictEqual(result.code, 0);
372372
});
373373

374+
test('should use node.config.json when --experimental-config-file has no argument', onlyIfNodeOptionsSupport, async () => {
375+
const result = await spawnPromisified(process.execPath, [
376+
'--no-warnings',
377+
'--experimental-config-file',
378+
'-p', 'http.maxHeaderSize',
379+
], {
380+
cwd: fixtures.path('rc/default'),
381+
});
382+
assert.strictEqual(result.stderr, '');
383+
assert.strictEqual(result.stdout, '10\n');
384+
assert.strictEqual(result.code, 0);
385+
});
386+
387+
test('should use node.config.json when --experimental-config-file= has empty argument', onlyIfNodeOptionsSupport, async () => {
388+
const result = await spawnPromisified(process.execPath, [
389+
'--no-warnings',
390+
'--experimental-config-file=',
391+
'-p', 'http.maxHeaderSize',
392+
], {
393+
cwd: fixtures.path('rc/default'),
394+
});
395+
assert.strictEqual(result.stderr, '');
396+
assert.strictEqual(result.stdout, '10\n');
397+
assert.strictEqual(result.code, 0);
398+
});
399+
374400
test('should override node.config.json when specificied', onlyIfNodeOptionsSupport, async () => {
375401
const result = await spawnPromisified(process.execPath, [
376402
'--no-warnings',

0 commit comments

Comments
 (0)