Skip to content

Commit e53ba12

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

File tree

12 files changed

+137
-45
lines changed

12 files changed

+137
-45
lines changed

doc/api/cli.md

Lines changed: 8 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=path`, `--experimental-config-file`
10181018

10191019
<!-- YAML
10201020
added:
@@ -1025,6 +1025,10 @@ 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.
1030+
The alias `--experimental-default-config-file` is equivalent to
1031+
`--experimental-config-file` without an argument.
10281032
Node.js will read the configuration file and apply the settings. The
10291033
configuration file should be a JSON file with the following structure. `vX.Y.Z`
10301034
in the `$schema` must be replaced with the version of Node.js you are using.
@@ -1131,9 +1135,10 @@ added:
11311135

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

11381143
### `--experimental-eventsource`
11391144

doc/api/test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4131,7 +4131,7 @@ Can be used to abort test subtasks when the test has been aborted.
41314131
[`suite()`]: #suitename-options-fn
41324132
[`test()`]: #testname-options-fn
41334133
[code coverage]: #collecting-code-coverage
4134-
[configuration files]: cli.md#--experimental-config-fileconfig
4134+
[configuration files]: cli.md#--experimental-config-filepath---experimental-config-file
41354135
[describe options]: #describename-options-fn
41364136
[it options]: #testname-options-fn
41374137
[running tests from the command line]: #running-tests-from-the-command-line

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 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.
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.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ static ExitCode InitializeNodeWithArgsInternal(
913913
}
914914

915915
std::string node_options_from_config;
916-
if (auto path = per_process::config_reader.GetDataFromArgs(*argv)) {
916+
if (auto path = per_process::config_reader.GetDataFromArgs(argv)) {
917917
switch (per_process::config_reader.ParseConfig(*path)) {
918918
case ParseResult::Valid:
919919
break;

src/node_config_file.cc

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,55 @@
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+
// Check if "--experimental-config-file=" has path arg
13+
inline bool HasEqualsPrefix(std::string_view arg, std::string_view flag) {
14+
return arg.size() > flag.size() + 1 && arg.starts_with(flag) &&
15+
arg[flag.size()] == '=';
16+
}
17+
918
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;
22-
}
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);
19+
std::vector<std::string>* args) {
20+
std::optional<std::string_view> result;
21+
22+
for (size_t i = 0; i < args->size(); ++i) {
23+
std::string& arg = (*args)[i];
24+
25+
if (arg == kConfigFileFlag) {
26+
// Check if next arg is a path
27+
const bool has_next_arg = i + 1 < args->size();
28+
const bool next_is_path =
29+
has_next_arg && !(*args)[i + 1].starts_with("-");
30+
31+
if (next_is_path) {
32+
// --experimental-config-file path
33+
arg = std::string(kConfigFileFlag) + "=" + (*args)[i + 1];
34+
args->erase(args->begin() + static_cast<ptrdiff_t>(i + 1));
35+
result = std::string_view(arg).substr(kConfigFileFlag.size() + 1);
36+
} else {
37+
// --experimental-config-file
38+
arg = std::string(kConfigFileFlag) + "=" +
39+
std::string(kDefaultConfigFileName);
40+
result = kDefaultConfigFileName;
2741
}
28-
} else if (*it == default_file || it->starts_with(default_file)) {
29-
has_default_config_file = true;
42+
} else if (HasEqualsPrefix(arg, kConfigFileFlag)) {
43+
// --experimental-config-file=path
44+
result = std::string_view(arg).substr(kConfigFileFlag.size() + 1);
45+
} else if (arg == kDefaultConfigFileFlag) {
46+
// --experimental-default-config-file
47+
arg = std::string(kConfigFileFlag) + "=" +
48+
std::string(kDefaultConfigFileName);
49+
result = kDefaultConfigFileName;
3050
}
3151
}
3252

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

4056
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
869869
&EnvironmentOptions::optional_env_file);
870870
Implies("--env-file-if-exists", "[has_env_file_string]");
871871
AddOption("--experimental-config-file",
872-
"set config file from supplied file",
873-
&EnvironmentOptions::experimental_config_file_path);
874-
AddOption("--experimental-default-config-file",
875-
"set config file from default config file",
876-
&EnvironmentOptions::experimental_default_config_file);
872+
"set config file path",
873+
&EnvironmentOptions::experimental_config_file_path,
874+
kDisallowedInEnvvar);
875+
AddAlias("--experimental-config-file=", "--experimental-config-file");
876+
AddAlias("--experimental-default-config-file", "--experimental-config-file");
877877
AddOption("--test",
878878
"launch test runner on startup",
879879
&EnvironmentOptions::test_runner,

src/node_options.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class EnvironmentOptions : public Options {
268268
bool report_exclude_env = false;
269269
bool report_exclude_network = false;
270270
std::string experimental_config_file_path;
271-
bool experimental_default_config_file = false;
272271

273272
inline DebugOptions* get_debug_options() { return &debug_options_; }
274273
inline const DebugOptions& debug_options() const { return debug_options_; }

test/parallel/test-cli-node-options-docs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,6 @@ for (const [, envVar, config] of nodeOptionsCC.matchAll(addOptionRE)) {
122122

123123
// add alias handling
124124
manPagesOptions.delete('-trace-events-enabled');
125+
manPagesOptions.delete('-experimental-default-config-file');
125126

126127
assert.strictEqual(manPagesOptions.size, 0, `Man page options not documented: ${[...manPagesOptions]}`);

0 commit comments

Comments
 (0)