src: implicitly enable namespace in config · nodejs/node@920f025 · GitHub
Skip to content

Commit 920f025

Browse files
marco-ippolitoRafaelGSS
authored andcommitted
src: implicitly enable namespace in config
PR-URL: #60798 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 2fa9917 commit 920f025

9 files changed

Lines changed: 120 additions & 4 deletions

File tree

doc/api/cli.md

Lines changed: 34 additions & 1 deletion

doc/api/permissions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ Example `node.config.json`:
175175
}
176176
```
177177

178-
Run with the configuration file:
178+
When the `permission` namespace is present in the configuration file, Node.js
179+
automatically enables the `--permission` flag. Run with:
179180

180181
```console
181-
$ node --permission --experimental-default-config-file app.js
182+
$ node --experimental-default-config-file app.js
182183
```
183184

184185
#### Using the Permission Model with `npx`

src/node_config_file.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
255255
available_namespaces.end());
256256
// Create a set to track unique options
257257
std::unordered_set<std::string> unique_options;
258+
// Namespaces in OPTION_NAMESPACE_LIST
259+
std::unordered_set<std::string> namespaces_with_implicit_flags;
260+
258261
// Iterate through the main object to find all namespaces
259262
for (auto field : main_object) {
260263
std::string_view field_name;
@@ -281,6 +284,15 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
281284
continue;
282285
}
283286

287+
// List of implicit namespace flags
288+
for (auto ns_enum : options_parser::AllNamespaces()) {
289+
std::string ns_str = options_parser::NamespaceEnumToString(ns_enum);
290+
if (!ns_str.empty() && namespace_name == ns_str) {
291+
namespaces_with_implicit_flags.insert(namespace_name);
292+
break;
293+
}
294+
}
295+
284296
// Get the namespace object
285297
simdjson::ondemand::object namespace_object;
286298
auto field_error = field.value().get_object().get(namespace_object);
@@ -302,6 +314,17 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
302314
}
303315
}
304316

317+
// Add implicit flags for namespaces (--test, --permission, --watch)
318+
// These flags are automatically enabled when their namespace is present
319+
for (const auto& ns : namespaces_with_implicit_flags) {
320+
std::string flag = "--" + ns;
321+
std::string no_flag = "--no-" + ns;
322+
// We skip if the user has already set the flag or its negation
323+
if (!unique_options.contains(flag) && !unique_options.contains(no_flag)) {
324+
namespace_options_.push_back(flag);
325+
}
326+
}
327+
305328
return ParseResult::Valid;
306329
}
307330

test/fixtures/options-as-flags/test-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"max-http-header-size": 8192
55
},
66
"test": {
7+
"test": false,
78
"test-isolation": "none"
89
}
910
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"test": {}
2+
"permission": {}
33
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"permission": {
3+
"allow-fs-read": "*"
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"test": {
3+
"test": false,
4+
"test-isolation": "none"
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"watch": {
3+
"watch-preserve-output": true
4+
}
5+
}

test/parallel/test-config-file.js

Lines changed: 42 additions & 0 deletions

0 commit comments

Comments
 (0)