Skip to content

Commit 851f313

Browse files
pd4d10targos
authored andcommitted
module: add warnings for experimental flags
PR-URL: #30617 Fixes: #30600 Reviewed-By: Guy Bedford <[email protected]>
1 parent 55a270b commit 851f313

13 files changed

+133
-8
lines changed

lib/internal/modules/cjs/loader.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const {
4343
rekeySourceMap
4444
} = require('internal/source_map/source_map_cache');
4545
const { pathToFileURL, fileURLToPath, URL } = require('internal/url');
46-
const { deprecate } = require('internal/util');
46+
const { deprecate, emitExperimentalWarning } = require('internal/util');
4747
const vm = require('vm');
4848
const assert = require('internal/assert');
4949
const fs = require('fs');
@@ -584,17 +584,21 @@ function resolveExportsTarget(pkgPath, target, subpath, basePath, mappingKey) {
584584
if (experimentalConditionalExports &&
585585
ObjectPrototypeHasOwnProperty(target, 'require')) {
586586
try {
587-
return resolveExportsTarget(pkgPath, target.require, subpath,
588-
basePath, mappingKey);
587+
const result = resolveExportsTarget(pkgPath, target.require, subpath,
588+
basePath, mappingKey);
589+
emitExperimentalWarning('Conditional exports');
590+
return result;
589591
} catch (e) {
590592
if (e.code !== 'MODULE_NOT_FOUND') throw e;
591593
}
592594
}
593595
if (experimentalConditionalExports &&
594596
ObjectPrototypeHasOwnProperty(target, 'node')) {
595597
try {
596-
return resolveExportsTarget(pkgPath, target.node, subpath,
597-
basePath, mappingKey);
598+
const result = resolveExportsTarget(pkgPath, target.node, subpath,
599+
basePath, mappingKey);
600+
emitExperimentalWarning('Conditional exports');
601+
return result;
598602
} catch (e) {
599603
if (e.code !== 'MODULE_NOT_FOUND') throw e;
600604
}
@@ -697,6 +701,7 @@ Module._findPath = function(request, paths, isMain) {
697701

698702
const selfFilename = trySelf(paths, exts, isMain, trailingSlash, request);
699703
if (selfFilename) {
704+
emitExperimentalWarning('Package name self resolution');
700705
Module._pathCache[cacheKey] = selfFilename;
701706
return selfFilename;
702707
}

lib/internal/modules/esm/translators.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const createDynamicModule = require(
2222
const fs = require('fs');
2323
const { fileURLToPath, URL } = require('url');
2424
const { debuglog } = require('internal/util/debuglog');
25-
const { promisify } = require('internal/util');
25+
const { promisify, emitExperimentalWarning } = require('internal/util');
2626
const {
2727
ERR_INVALID_URL,
2828
ERR_INVALID_URL_SCHEME,
@@ -133,6 +133,7 @@ translators.set('builtin', async function builtinStrategy(url) {
133133

134134
// Strategy for loading a JSON file
135135
translators.set('json', async function jsonStrategy(url) {
136+
emitExperimentalWarning('Importing JSON modules');
136137
debug(`Translating JSONModule ${url}`);
137138
debug(`Loading JSONModule ${url}`);
138139
const pathname = url.startsWith('file:') ? fileURLToPath(url) : null;
@@ -187,6 +188,7 @@ translators.set('json', async function jsonStrategy(url) {
187188

188189
// Strategy for loading a wasm module
189190
translators.set('wasm', async function(url) {
191+
emitExperimentalWarning('Importing Web Assembly modules');
190192
const buffer = await getSource(url);
191193
debug(`Translating WASMModule ${url}`);
192194
let compiled;

src/module_wrap.cc

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "util-inl.h"
88
#include "node_contextify.h"
99
#include "node_watchdog.h"
10+
#include "node_process.h"
1011

1112
#include <sys/stat.h> // S_IFDIR
1213

@@ -962,6 +963,7 @@ Maybe<URL> ResolveExportsTarget(Environment* env,
962963
Maybe<URL> resolved = ResolveExportsTarget(env, pjson_url,
963964
conditionalTarget, subpath, pkg_subpath, base, false);
964965
if (!resolved.IsNothing()) {
966+
ProcessEmitExperimentalWarning(env, "Conditional exports");
965967
return resolved;
966968
}
967969
}
@@ -1267,6 +1269,7 @@ Maybe<URL> PackageResolve(Environment* env,
12671269

12681270
Maybe<URL> self_url = ResolveSelf(env, specifier, base);
12691271
if (self_url.IsJust()) {
1272+
ProcessEmitExperimentalWarning(env, "Package name self resolution");
12701273
return self_url;
12711274
}
12721275

src/node_process.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ v8::Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
2727
const char* code = nullptr);
2828

2929
v8::Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...);
30+
v8::Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
31+
const char* warning);
3032
v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
3133
const char* warning,
3234
const char* deprecation_code);

src/node_process_events.cc

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <cstdarg>
2+
#include <set>
23

34
#include "env-inl.h"
45
#include "node_process.h"
@@ -95,6 +96,21 @@ Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
9596
return ProcessEmitWarningGeneric(env, warning);
9697
}
9798

99+
100+
std::set<std::string> experimental_warnings;
101+
102+
Maybe<bool> ProcessEmitExperimentalWarning(Environment* env,
103+
const char* warning) {
104+
if (experimental_warnings.find(warning) != experimental_warnings.end())
105+
return Nothing<bool>();
106+
107+
experimental_warnings.insert(warning);
108+
std::string message(warning);
109+
message.append(
110+
" is an experimental feature. This feature could change at any time");
111+
return ProcessEmitWarningGeneric(env, message.c_str(), "ExperimentalWarning");
112+
}
113+
98114
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
99115
const char* warning,
100116
const char* deprecation_code) {

test/common/fixtures.mjs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable node-core/require-common-first, node-core/required-modules */
2+
import fixtures from './fixtures.js';
3+
4+
const {
5+
fixturesDir,
6+
path,
7+
readSync,
8+
readKey,
9+
} = fixtures;
10+
11+
export {
12+
fixturesDir,
13+
path,
14+
readSync,
15+
readKey,
16+
};

test/es-module/test-esm-exports.mjs

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Flags: --experimental-modules
22
import { mustCall } from '../common/index.mjs';
3+
import { path } from '../common/fixtures.mjs';
34
import { ok, deepStrictEqual, strictEqual } from 'assert';
5+
import { spawn } from 'child_process';
46

57
import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';
68
import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
@@ -149,3 +151,33 @@ function assertIncludes(actual, expected) {
149151
ok(actual.toString().indexOf(expected) !== -1,
150152
`${JSON.stringify(actual)} includes ${JSON.stringify(expected)}`);
151153
}
154+
155+
// Test warning message
156+
[
157+
[
158+
'--experimental-conditional-exports',
159+
'/es-modules/conditional-exports.js',
160+
'Conditional exports',
161+
],
162+
[
163+
'--experimental-resolve-self',
164+
'/node_modules/pkgexports/resolve-self.js',
165+
'Package name self resolution',
166+
],
167+
].forEach(([flag, file, message]) => {
168+
const child = spawn(process.execPath, [flag, path(file)]);
169+
170+
let stderr = '';
171+
child.stderr.setEncoding('utf8');
172+
child.stderr.on('data', (data) => {
173+
stderr += data;
174+
});
175+
child.on('close', (code, signal) => {
176+
strictEqual(code, 0);
177+
strictEqual(signal, null);
178+
ok(stderr.toString().includes(
179+
`ExperimentalWarning: ${message} is an experimental feature. ` +
180+
'This feature could change at any time'
181+
));
182+
});
183+
});

test/es-module/test-esm-json.mjs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
// Flags: --experimental-json-modules
22
import '../common/index.mjs';
3-
import { strictEqual } from 'assert';
3+
import { path } from '../common/fixtures.mjs';
4+
import { strictEqual, ok } from 'assert';
5+
import { spawn } from 'child_process';
46

57
import secret from '../fixtures/experimental.json';
68

79
strictEqual(secret.ofLife, 42);
10+
11+
// Test warning message
12+
const child = spawn(process.execPath, [
13+
'--experimental-json-modules',
14+
path('/es-modules/json-modules.mjs')
15+
]);
16+
17+
let stderr = '';
18+
child.stderr.setEncoding('utf8');
19+
child.stderr.on('data', (data) => {
20+
stderr += data;
21+
});
22+
child.on('close', (code, signal) => {
23+
strictEqual(code, 0);
24+
strictEqual(signal, null);
25+
ok(stderr.toString().includes(
26+
'ExperimentalWarning: Importing JSON modules is an experimental feature. ' +
27+
'This feature could change at any time'
28+
));
29+
});

test/es-module/test-esm-wasm.mjs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Flags: --experimental-wasm-modules
22
import '../common/index.mjs';
3+
import { path } from '../common/fixtures.mjs';
34
import { add, addImported } from '../fixtures/es-modules/simple.wasm';
45
import { state } from '../fixtures/es-modules/wasm-dep.mjs';
5-
import { strictEqual } from 'assert';
6+
import { strictEqual, ok } from 'assert';
7+
import { spawn } from 'child_process';
68

79
strictEqual(state, 'WASM Start Executed');
810

@@ -13,3 +15,23 @@ strictEqual(addImported(0), 42);
1315
strictEqual(state, 'WASM JS Function Executed');
1416

1517
strictEqual(addImported(1), 43);
18+
19+
// Test warning message
20+
const child = spawn(process.execPath, [
21+
'--experimental-wasm-modules',
22+
path('/es-modules/wasm-modules.mjs')
23+
]);
24+
25+
let stderr = '';
26+
child.stderr.setEncoding('utf8');
27+
child.stderr.on('data', (data) => {
28+
stderr += data;
29+
});
30+
child.on('close', (code, signal) => {
31+
strictEqual(code, 0);
32+
strictEqual(signal, null);
33+
ok(stderr.toString().includes(
34+
'ExperimentalWarning: Importing Web Assembly modules is ' +
35+
'an experimental feature. This feature could change at any time'
36+
));
37+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('pkgexports/condition')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import secret from '../experimental.json';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { add, addImported } from './simple.wasm';
2+
import { state } from './wasm-dep.mjs';

test/fixtures/node_modules/pkgexports/resolve-self.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)