src: support multiple versions in node.config.json

Signed-off-by: Marco Ippolito <marcoippolito54@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66830
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Marco Ippolito authored and Antoine du Hamel committed 2026-11-19 16:44:47 +01:00
1 parent 73b6f41ab5
commit 0c6040b5a4
6 files changed
+1607 -1049

No files matched your search

+38
View File
@@ -1041,6 +1041,44 @@ The configuration file supports namespace-specific options:
* Namespace fields like `test`, `watch`, and `permission` contain configuration specific to that subsystem.
The configuration file can target a specific Node.js major version with
`nodeVersion`:
```json
{
"nodeVersion": 25,
"nodeOptions": {
"watch-path": "src"
}
}
```
To keep multiple version-specific configurations in the same file, use the
`configs` array. Node.js will use the first entry whose `nodeVersion` matches
the current Node.js major version:
```json
{
"$schema": "https://nodejs.org/dist/latest-v26.x/docs/node-config-schema.json",
"configs": [
{
"nodeVersion": 25,
"config": {
"$schema": "https://nodejs.org/dist/latest-v25.x/docs/node-config-schema.json",
"nodeOptions": {
"watch-path": "src"
}
}
}
]
}
```
When `configs` is used, the top level may only contain `$schema` and
`configs`. Each `configs` item must define an integer `nodeVersion` and an
object `config`. A single top-level config does not require `nodeVersion`, but
if present it must match the current Node.js major version.
When a namespace is present in the
configuration file, Node.js automatically enables the corresponding flag
(e.g., `--test`, `--watch`, `--permission`). This allows you to configure
+1058 -1018
View File
File diff suppressed because it is too large. Load diff
+55 -5
View File
@@ -66,9 +66,8 @@ function generateConfigJsonSchema() {
return { __proto__: null, type, description };
}
const schema = {
const configSchema = {
__proto__: null,
$schema: 'https://json-schema.org/draft/2020-12/schema',
additionalProperties: false,
required: [],
properties: {
@@ -83,13 +82,17 @@ function generateConfigJsonSchema() {
properties: { __proto__: null },
type: 'object',
},
nodeVersion: {
__proto__: null,
type: 'integer',
},
__proto__: null,
},
type: 'object',
};
// Get the root properties object for adding namespaces
const rootProperties = schema.properties;
const rootProperties = configSchema.properties;
const nodeOptions = rootProperties.nodeOptions.properties;
// Add env options to nodeOptions (backward compatibility)
@@ -131,7 +134,7 @@ function generateConfigJsonSchema() {
ArrayPrototypeMap(sortedKeys, (key) => [key, nodeOptions[key]]),
);
schema.properties.nodeOptions.properties = sortedProperties;
configSchema.properties.nodeOptions.properties = sortedProperties;
// Also sort the root level properties
const sortedRootKeys = ArrayPrototypeSort(ObjectKeys(rootProperties));
@@ -139,7 +142,54 @@ function generateConfigJsonSchema() {
ArrayPrototypeMap(sortedRootKeys, (key) => [key, rootProperties[key]]),
);
schema.properties = sortedRootProperties;
configSchema.properties = sortedRootProperties;
const schema = {
__proto__: null,
$schema: 'https://json-schema.org/draft/2020-12/schema',
oneOf: [
{ __proto__: null, $ref: '#/$defs/config' },
{
__proto__: null,
type: 'object',
additionalProperties: false,
required: ['configs'],
properties: {
__proto__: null,
$schema: {
__proto__: null,
type: 'string',
},
configs: {
__proto__: null,
type: 'array',
minItems: 1,
items: {
__proto__: null,
type: 'object',
additionalProperties: false,
required: ['nodeVersion', 'config'],
properties: {
__proto__: null,
nodeVersion: {
__proto__: null,
type: 'integer',
},
config: {
__proto__: null,
$ref: '#/$defs/config',
},
},
},
},
},
},
],
$defs: {
__proto__: null,
config: configSchema,
},
};
return schema;
}
+221 -26
View File
@@ -1,7 +1,10 @@
#include "node_config_file.h"
#include "debug_utils-inl.h"
#include "node_version.h"
#include "simdjson.h"
#include <cinttypes>
namespace node {
constexpr std::string_view kConfigFileFlag = "--experimental-config-file";
@@ -224,39 +227,115 @@ ParseResult ConfigReader::ParseOptions(
return ParseResult::Valid;
}
ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
std::string file_content;
// Read the configuration file
int r = ReadFileSync(&file_content, config_path.data());
if (r != 0) {
const char* err = uv_strerror(r);
FPrintF(
stderr, "Cannot read configuration from %s: %s\n", config_path, err);
return ParseResult::FileError;
}
// Parse the configuration file
simdjson::ondemand::parser json_parser;
simdjson::ondemand::document document;
if (json_parser.iterate(file_content).get(document)) {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
ParseResult ConfigReader::ParseNodeVersion(
simdjson::ondemand::value* version_value,
const std::string_view& config_path) {
int64_t version;
if (version_value->get_int64().get(version)) {
FPrintF(stderr,
"\"nodeVersion\" value unexpected for %s "
"(should be an integer)\n",
config_path.data());
return ParseResult::InvalidContent;
}
// Validate config is an object
simdjson::ondemand::object main_object;
auto root_error = document.get_object().get(main_object);
if (root_error) {
if (root_error == simdjson::error_code::INCORRECT_TYPE) {
if (version != NODE_MAJOR_VERSION) {
FPrintF(stderr,
"\"nodeVersion\" %" PRId64
" does not match current Node.js version %d "
"for %s\n",
version,
NODE_MAJOR_VERSION,
config_path.data());
return ParseResult::InvalidContent;
}
return ParseResult::Valid;
}
ParseResult ConfigReader::ParseConfigs(simdjson::ondemand::array* configs,
const std::string_view& config_path) {
size_t index = 0;
for (auto raw_config : *configs) {
simdjson::ondemand::object config_wrapper;
if (raw_config.get_object().get(config_wrapper)) {
FPrintF(stderr,
"Root value unexpected not an object for %s\n\n",
"\"configs[%zu]\" value unexpected for %s "
"(should be an object)\n",
index,
config_path.data());
} else {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
return ParseResult::InvalidContent;
}
return ParseResult::InvalidContent;
simdjson::ondemand::value version_value;
auto version_error =
config_wrapper.find_field_unordered("nodeVersion").get(version_value);
if (version_error == simdjson::NO_SUCH_FIELD) {
FPrintF(stderr,
"\"configs[%zu].nodeVersion\" is required for %s\n",
index,
config_path.data());
return ParseResult::InvalidContent;
}
if (version_error) {
return ParseResult::InvalidContent;
}
int64_t version;
if (version_value.get_int64().get(version)) {
FPrintF(stderr,
"\"configs[%zu].nodeVersion\" value unexpected for %s "
"(should be an integer)\n",
index,
config_path.data());
return ParseResult::InvalidContent;
}
if (version != NODE_MAJOR_VERSION) {
index++;
continue;
}
simdjson::ondemand::value config_value;
auto config_error =
config_wrapper.find_field_unordered("config").get(config_value);
if (config_error == simdjson::NO_SUCH_FIELD) {
FPrintF(stderr,
"\"configs[%zu].config\" is required for %s\n",
index,
config_path.data());
return ParseResult::InvalidContent;
}
if (config_error) {
return ParseResult::InvalidContent;
}
simdjson::ondemand::object selected_config;
if (config_value.get_object().get(selected_config)) {
FPrintF(stderr,
"\"configs[%zu].config\" value unexpected for %s "
"(should be an object)\n",
index,
config_path.data());
return ParseResult::InvalidContent;
}
return ParseConfigObject(&selected_config, config_path, false);
}
FPrintF(stderr,
"No config found for current Node.js version %d in "
"\"configs\" for %s\n",
NODE_MAJOR_VERSION,
config_path.data());
return ParseResult::InvalidContent;
}
ParseResult ConfigReader::ParseConfigObject(
simdjson::ondemand::object* config_object,
const std::string_view& config_path,
bool allow_version_selection) {
// Get all available namespaces for validation
std::vector<std::string> available_namespaces =
options_parser::MapAvailableNamespaces();
@@ -272,7 +351,7 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
std::unordered_set<std::string> namespaces_with_implicit_flags;
// Iterate through the main object to find all namespaces
for (auto field : main_object) {
for (auto field : *config_object) {
std::string_view field_name;
if (field.unescaped_key().get(field_name)) {
return ParseResult::InvalidContent;
@@ -280,6 +359,47 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
std::string namespace_name(field_name);
if (namespace_name == "$schema") {
continue;
}
if (namespace_name == "nodeVersion") {
simdjson::ondemand::value version_value;
if (field.value().get(version_value)) {
return ParseResult::InvalidContent;
}
ParseResult result = ParseNodeVersion(&version_value, config_path);
if (result != ParseResult::Valid) {
return result;
}
continue;
}
if (namespace_name == "configs") {
if (!allow_version_selection) {
FPrintF(stderr,
"\"configs\" is not allowed inside a versioned config "
"for %s\n",
config_path.data());
return ParseResult::InvalidContent;
}
simdjson::ondemand::array configs;
auto field_error = field.value().get_array().get(configs);
if (field_error) {
FPrintF(stderr,
"\"configs\" value unexpected for %s "
"(should be an array)\n",
config_path.data());
return ParseResult::InvalidContent;
}
ParseResult result = ParseConfigs(&configs, config_path);
if (result != ParseResult::Valid) {
return result;
}
continue;
}
// TODO(@marco-ippolito): Remove warning for testRunner namespace
if (namespace_name == "testRunner") {
FPrintF(stderr,
@@ -345,6 +465,81 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
return ParseResult::Valid;
}
ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
std::string file_content;
// Read the configuration file
int r = ReadFileSync(&file_content, config_path.data());
if (r != 0) {
const char* err = uv_strerror(r);
FPrintF(
stderr, "Cannot read configuration from %s: %s\n", config_path, err);
return ParseResult::FileError;
}
// Parse the configuration file
simdjson::ondemand::parser json_parser;
simdjson::ondemand::document document;
if (json_parser.iterate(file_content).get(document)) {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
return ParseResult::InvalidContent;
}
// Validate config is an object
simdjson::ondemand::object main_object;
auto root_error = document.get_object().get(main_object);
if (root_error) {
if (root_error == simdjson::error_code::INCORRECT_TYPE) {
FPrintF(stderr,
"Root value unexpected not an object for %s\n\n",
config_path.data());
} else {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
}
return ParseResult::InvalidContent;
}
bool has_configs = false;
bool has_other_fields = false;
for (auto field : main_object) {
std::string_view field_name;
if (field.unescaped_key().get(field_name)) {
return ParseResult::InvalidContent;
}
if (field_name == "$schema") {
continue;
}
if (field_name == "configs") {
has_configs = true;
} else {
has_other_fields = true;
}
}
if (has_configs && has_other_fields) {
FPrintF(stderr,
"\"configs\" cannot be mixed with other configuration fields "
"for %s\n",
config_path.data());
return ParseResult::InvalidContent;
}
simdjson::ondemand::parser config_parser;
simdjson::ondemand::document config_document;
if (config_parser.iterate(file_content).get(config_document)) {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
return ParseResult::InvalidContent;
}
simdjson::ondemand::object config_object;
if (config_document.get_object().get(config_object)) {
return ParseResult::InvalidContent;
}
return ParseConfigObject(&config_object, config_path, true);
}
std::string ConfigReader::GetNodeOptions() {
std::string acc = "";
const size_t total_options = node_options_.size();
+8
View File
@@ -39,6 +39,14 @@ class ConfigReader {
size_t GetFlagsSize();
private:
ParseResult ParseConfigObject(simdjson::ondemand::object* config_object,
const std::string_view& config_path,
bool allow_version_selection);
ParseResult ParseNodeVersion(simdjson::ondemand::value* version_value,
const std::string_view& config_path);
ParseResult ParseConfigs(simdjson::ondemand::array* configs,
const std::string_view& config_path);
// Parse options for a specific namespace (including nodeOptions for backward
// compatibility)
ParseResult ParseOptions(simdjson::ondemand::object* options_object,
+227
View File
@@ -55,6 +55,233 @@ test('should handle empty object json', async () => {
assert.strictEqual(result.code, 0);
});
describe('runtime version checks', () => {
const currentMajor = Number(process.versions.node.split('.')[0]);
async function runConfig(config, filename = 'version-config.json') {
tmpdir.refresh();
const configPath = join(tmpdir.path, filename);
writeFileSync(configPath, JSON.stringify(config));
return spawnPromisified(process.execPath, [
'--no-warnings',
`--experimental-config-file=${configPath}`,
'-p', 'http.maxHeaderSize',
]);
}
it('should accept a top-level config without nodeVersion', async () => {
const result = await runConfig({
nodeOptions: { 'max-http-header-size': 10 },
}, 'top-level-without-version.json');
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, '10\n');
assert.strictEqual(result.code, 0);
});
it('should accept a config file matching the current Node.js version', async () => {
const result = await runConfig({
nodeVersion: currentMajor,
nodeOptions: { 'max-http-header-size': 10 },
}, 'matching-version.json');
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, '10\n');
assert.strictEqual(result.code, 0);
});
it('should reject a config file targeting another Node.js version', async () => {
const result = await runConfig({
nodeVersion: currentMajor + 1,
nodeOptions: { 'max-http-header-size': 10 },
}, 'mismatching-version.json');
assert.match(result.stderr, /"nodeVersion" \d+ does not match current Node\.js version \d+/);
assert.strictEqual(result.stdout, '');
assert.strictEqual(result.code, 9);
});
it('should select a matching config from configs', async () => {
const result = await runConfig({
configs: [
{
nodeVersion: currentMajor + 1,
config: {
nodeOptions: { 'max-http-header-size': 20 },
},
},
{
nodeVersion: currentMajor,
config: {
nodeOptions: { 'max-http-header-size': 10 },
},
},
],
}, 'versioned-configs.json');
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, '10\n');
assert.strictEqual(result.code, 0);
});
it('should reject configs without an entry for the current version', async () => {
const result = await runConfig({
configs: [
{
nodeVersion: currentMajor + 1,
config: {
nodeOptions: { 'max-http-header-size': 10 },
},
},
],
}, 'missing-versioned-config.json');
assert.match(result.stderr, /No config found for current Node\.js version \d+ in "configs"/);
assert.strictEqual(result.stdout, '');
assert.strictEqual(result.code, 9);
});
it('should ignore invalid config payloads for non-matching versions', async () => {
const result = await runConfig({
configs: [
{
nodeVersion: currentMajor + 1,
config: false,
},
{
nodeVersion: currentMajor,
config: {
nodeOptions: { 'max-http-header-size': 10 },
},
},
],
}, 'ignored-non-matching-config.json');
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, '10\n');
assert.strictEqual(result.code, 0);
});
it('should use the first matching config from configs', async () => {
const result = await runConfig({
configs: [
{
nodeVersion: currentMajor,
config: {
nodeOptions: { 'max-http-header-size': 10 },
},
},
{
nodeVersion: currentMajor,
config: {
nodeOptions: { 'max-http-header-size': 20 },
},
},
],
}, 'first-matching-versioned-config.json');
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, '10\n');
assert.strictEqual(result.code, 0);
});
it('should allow $schema with configs', async () => {
const result = await runConfig({
$schema: 'https://nodejs.org/dist/vX.Y.Z/docs/node-config-schema.json',
configs: [
{
nodeVersion: currentMajor,
config: {
$schema: 'https://nodejs.org/dist/vX.Y.Z/docs/node-config-schema.json',
nodeOptions: { 'max-http-header-size': 10 },
},
},
],
}, 'schema-with-versioned-config.json');
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, '10\n');
assert.strictEqual(result.code, 0);
});
for (const { name, config, error } of [
{
name: 'configs is empty',
config: { configs: [] },
error: /No config found for current Node\.js version \d+ in "configs"/,
},
{
name: 'configs is not an array',
config: { configs: {} },
error: /"configs" value unexpected .* \(should be an array\)/,
},
{
name: 'configs contains a non-object entry',
config: { configs: [false] },
error: /"configs\[0\]" value unexpected .* \(should be an object\)/,
},
{
name: 'configs entry is missing nodeVersion',
config: { configs: [{ config: {} }] },
error: /"configs\[0\]\.nodeVersion" is required/,
},
{
name: 'configs entry has a non-integer nodeVersion',
config: { configs: [{ nodeVersion: `${currentMajor}`, config: {} }] },
error: /"configs\[0\]\.nodeVersion" value unexpected .* \(should be an integer\)/,
},
{
name: 'matching configs entry is missing config',
config: { configs: [{ nodeVersion: currentMajor }] },
error: /"configs\[0\]\.config" is required/,
},
{
name: 'matching configs entry has a non-object config',
config: { configs: [{ nodeVersion: currentMajor, config: false }] },
error: /"configs\[0\]\.config" value unexpected .* \(should be an object\)/,
},
{
name: 'configs is mixed with preceding config fields',
config: {
nodeOptions: { 'max-http-header-size': 10 },
configs: [{ nodeVersion: currentMajor, config: {} }],
},
error: /"configs" cannot be mixed with other configuration fields/,
},
{
name: 'configs is mixed with following config fields',
config: {
configs: [{ nodeVersion: currentMajor, config: {} }],
nodeOptions: { 'max-http-header-size': 10 },
},
error: /"configs" cannot be mixed with other configuration fields/,
},
{
name: 'configs is nested inside a selected config',
config: {
configs: [{
nodeVersion: currentMajor,
config: { configs: [] },
}],
},
error: /"configs" is not allowed inside a versioned config/,
},
{
name: 'selected config targets another version',
config: {
configs: [{
nodeVersion: currentMajor,
config: {
nodeVersion: currentMajor + 1,
nodeOptions: { 'max-http-header-size': 10 },
},
}],
},
error: /"nodeVersion" \d+ does not match current Node\.js version \d+/,
},
]) {
it(`should reject when ${name}`, async () => {
const result = await runConfig(config);
assert.match(result.stderr, error);
assert.strictEqual(result.stdout, '');
assert.strictEqual(result.code, 9);
});
}
});
test('should parse boolean flag', onlyWithAmaroAndNodeOptions, async () => {
const result = await spawnPromisified(process.execPath, [
`--experimental-config-file=${fixtures.path('rc/transform-types.json')}`,