Skip to content

Commit b79afcf

Browse files
src: allow empty --experimental-config-file
1 parent 7a937d1 commit b79afcf

18 files changed

+256
-182
lines changed

doc/api/cli.md

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

1018-
### `--experimental-config-file=config`
1018+
### `--experimental-config-file=path`, `--experimental-config-file`
10191019

10201020
<!-- YAML
10211021
added:
@@ -1026,6 +1026,12 @@ added:
10261026
> Stability: 1.0 - Early development
10271027
10281028
If present, Node.js will look for a configuration file at the specified path.
1029+
If the path is not specified, Node.js will look for a `node.config.json` file
1030+
in the current working directory.
1031+
To specify a custom path, use the `--experimental-config-file=path` form.
1032+
The space-separated `--experimental-config-file path` form is not supported.
1033+
The alias `--experimental-default-config-file` is equivalent to
1034+
`--experimental-config-file` without an argument.
10291035
Node.js will read the configuration file and apply the settings. The
10301036
configuration file should be a JSON file with the following structure. `vX.Y.Z`
10311037
in the `$schema` must be replaced with the version of Node.js you are using.
@@ -1132,9 +1138,10 @@ added:
11321138

11331139
> Stability: 1.0 - Early development
11341140
1135-
If the `--experimental-default-config-file` flag is present, Node.js will look for a
1141+
This flag is an alias for `--experimental-config-file` without an argument.
1142+
If present, Node.js will look for a
11361143
`node.config.json` file in the current working directory and load it as a
1137-
as configuration file.
1144+
configuration file.
11381145

11391146
### `--experimental-eventsource`
11401147

doc/api/test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4531,7 +4531,7 @@ test.describe('my suite', (suite) => {
45314531
[`suite()`]: #suitename-options-fn
45324532
[`test()`]: #testname-options-fn
45334533
[code coverage]: #collecting-code-coverage
4534-
[configuration files]: cli.md#--experimental-config-fileconfig
4534+
[configuration files]: cli.md#--experimental-config-filepath---experimental-config-file
45354535
[describe options]: #describename-options-fn
45364536
[it options]: #testname-options-fn
45374537
[module customization hooks]: module.md#customization-hooks

doc/node.1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,12 @@ 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 Ar config , Fl -experimental-config-file , Fl -experimental-default-config-file
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.
609+
To specify a custom path, use the \fB--experimental-config-file=\fR\fBpath\fR form.
610+
The space-separated \fB--experimental-config-file path\fR form is not supported.
607611
Node.js will read the configuration file and apply the settings. The
608612
configuration file should be a JSON file with the following structure. \fBvX.Y.Z\fR
609613
in the \fB$schema\fR must be replaced with the version of Node.js you are using.
@@ -689,9 +693,10 @@ Node.js will not sanitize or perform validation on the user-provided configurati
689693
so \fBNEVER\fR use untrusted configuration files.
690694
.
691695
.It Fl -experimental-default-config-file
692-
If the \fB--experimental-default-config-file\fR flag is present, Node.js will look for a
696+
This flag is an alias for \fB--experimental-config-file\fR without an argument.
697+
If present, Node.js will look for a
693698
\fBnode.config.json\fR file in the current working directory and load it as a
694-
as configuration file.
699+
configuration file.
695700
.
696701
.It Fl -experimental-eventsource
697702
Enable exposition of EventSource Web API on the global scope.

lib/internal/main/watch_mode.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const {
33
ArrayPrototypeForEach,
4-
ArrayPrototypeIncludes,
54
ArrayPrototypeJoin,
65
ArrayPrototypeMap,
76
ArrayPrototypePush,
@@ -67,11 +66,8 @@ for (let i = 0; i < process.execArgv.length; i++) {
6766
}
6867
continue;
6968
}
70-
if (StringPrototypeStartsWith(arg, '--experimental-config-file')) {
71-
if (!ArrayPrototypeIncludes(arg, '=')) {
72-
// Skip the flag and the next argument (the config file path)
73-
i++;
74-
}
69+
if (arg === '--experimental-config-file' ||
70+
StringPrototypeStartsWith(arg, '--experimental-config-file=')) {
7571
continue;
7672
}
7773
if (arg === '--experimental-default-config-file') {

lib/internal/process/pre_execution.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ function setupSQLite() {
387387
}
388388

389389
function initializeConfigFileSupport() {
390-
if (getOptionValue('--experimental-default-config-file') ||
391-
getOptionValue('--experimental-config-file')) {
390+
if (getOptionValue('--experimental-config-file')) {
392391
emitExperimentalWarning('--experimental-config-file');
393392
}
394393
}

lib/internal/test_runner/runner.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ function createTestFileList(patterns, cwd) {
161161

162162
function filterExecArgv(arg, i, arr) {
163163
return !ArrayPrototypeIncludes(kFilterArgs, arg) &&
164-
!ArrayPrototypeSome(kFilterArgValues, (p) => arg === p || (i > 0 && arr[i - 1] === p) || StringPrototypeStartsWith(arg, `${p}=`));
164+
!ArrayPrototypeSome(kFilterArgValues, (p) => {
165+
return arg === p ||
166+
StringPrototypeStartsWith(arg, `${p}=`) ||
167+
(p !== '--experimental-config-file' && i > 0 && arr[i - 1] === p);
168+
});
165169
}
166170

167171
function getRunArgs(path, { forceExit,

src/node.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ static ExitCode InitializeNodeWithArgsInternal(
900900
}
901901

902902
std::string node_options_from_config;
903-
if (auto path = per_process::config_reader.GetDataFromArgs(*argv)) {
903+
if (auto path = per_process::config_reader.GetDataFromArgs(argv)) {
904904
switch (per_process::config_reader.ParseConfig(*path)) {
905905
case ParseResult::Valid:
906906
break;

src/node_config_file.cc

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,50 @@
22
#include "debug_utils-inl.h"
33
#include "simdjson.h"
44

5-
#include <string>
6-
75
namespace node {
86

7+
constexpr std::string_view kConfigFileFlag = "--experimental-config-file";
8+
constexpr std::string_view kDefaultConfigFileFlag =
9+
"--experimental-default-config-file";
10+
constexpr std::string_view kDefaultConfigFileName = "node.config.json";
11+
12+
inline bool HasEqualsPrefix(std::string_view arg, std::string_view flag) {
13+
return arg.size() > flag.size() && arg.starts_with(flag) &&
14+
arg[flag.size()] == '=';
15+
}
16+
917
std::optional<std::string_view> ConfigReader::GetDataFromArgs(
10-
const std::vector<std::string>& args) {
11-
constexpr std::string_view flag_path = "--experimental-config-file";
12-
constexpr std::string_view default_file =
13-
"--experimental-default-config-file";
14-
15-
bool has_default_config_file = false;
16-
17-
for (auto it = args.begin(); it != args.end(); ++it) {
18-
if (*it == flag_path) {
19-
// Case: "--experimental-config-file foo"
20-
if (auto next = std::next(it); next != args.end()) {
21-
return *next;
18+
std::vector<std::string>* args) {
19+
std::optional<std::string_view> result;
20+
21+
for (size_t i = 0; i < args->size(); ++i) {
22+
std::string& arg = (*args)[i];
23+
24+
if (arg == kConfigFileFlag) {
25+
// --experimental-config-file
26+
arg = std::string(kConfigFileFlag) + "=" +
27+
std::string(kDefaultConfigFileName);
28+
result = kDefaultConfigFileName;
29+
} else if (HasEqualsPrefix(arg, kConfigFileFlag)) {
30+
// --experimental-config-file=path
31+
std::string_view path = std::string_view(arg).substr(
32+
kConfigFileFlag.size() + 1);
33+
if (!path.empty()) {
34+
result = path;
2235
}
23-
} else if (it->starts_with(flag_path)) {
24-
// Case: "--experimental-config-file=foo"
25-
if (it->size() > flag_path.size() && (*it)[flag_path.size()] == '=') {
26-
return std::string_view(*it).substr(flag_path.size() + 1);
27-
}
28-
} else if (*it == default_file || it->starts_with(default_file)) {
29-
has_default_config_file = true;
36+
} else if (arg == kDefaultConfigFileFlag) {
37+
// --experimental-default-config-file
38+
arg = std::string(kConfigFileFlag) + "=" +
39+
std::string(kDefaultConfigFileName);
40+
result = kDefaultConfigFileName;
41+
} else if (HasEqualsPrefix(arg, kDefaultConfigFileFlag)) {
42+
// Force the main option parser to report that this alias does not accept
43+
// an explicit argument.
44+
arg = std::string(kConfigFileFlag) + "=";
3045
}
3146
}
3247

33-
if (has_default_config_file) {
34-
return "node.config.json";
35-
}
36-
37-
return std::nullopt;
48+
return result;
3849
}
3950

4051
ParseResult ConfigReader::ProcessOptionValue(

src/node_config_file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ConfigReader {
3030
ParseResult ParseConfig(const std::string_view& config_path);
3131

3232
std::optional<std::string_view> GetDataFromArgs(
33-
const std::vector<std::string>& args);
33+
std::vector<std::string>* args);
3434

3535
std::string GetNodeOptions();
3636
const std::vector<std::string>& GetNamespaceFlags() const;

src/node_options.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -885,11 +885,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
885885
&EnvironmentOptions::optional_env_file);
886886
Implies("--env-file-if-exists", "[has_env_file_string]");
887887
AddOption("--experimental-config-file",
888-
"set config file from supplied file",
889-
&EnvironmentOptions::experimental_config_file_path);
890-
AddOption("--experimental-default-config-file",
891-
"set config file from default config file",
892-
&EnvironmentOptions::experimental_default_config_file);
888+
"set config file path",
889+
&EnvironmentOptions::experimental_config_file_path,
890+
kDisallowedInEnvvar);
891+
AddAlias("--experimental-default-config-file", "--experimental-config-file");
893892
AddOption("--test",
894893
"launch test runner on startup",
895894
&EnvironmentOptions::test_runner,

0 commit comments

Comments
 (0)