Skip to content

Commit c80239f

Browse files
committed
Fix createRequire so it works for local node_modules.
For most uses of `require()` in emscripten this does not matter since we load mostly system libraries. However for the `ws` module it is needed. This bug was being masked by the fact that we were setting `NODE_PATH` in our socket test running. This is no longer needed now that we run (non-parallel) tests in the emscripten tree (in out/test). This is followup to #23265 which itself was an attempt to revert 23169. Fixes: #23503
1 parent a8385bf commit c80239f

File tree

5 files changed

+11
-12
lines changed

5 files changed

+11
-12
lines changed

src/shell.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ if (ENVIRONMENT_IS_NODE) {
105105
// We need to use `createRequire()` to construct the require()` function.
106106
const { createRequire } = await import('module');
107107
/** @suppress{duplicate} */
108-
var require = createRequire('/');
108+
var require = createRequire(import.meta.url);
109109
#endif
110110

111111
#if PTHREADS || WASM_WORKERS

test/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ def verify_es5(self, filename):
13341334
self.fail('es-check failed to verify ES5 output compliance')
13351335

13361336
# Build JavaScript code from source code
1337-
def build(self, filename, libraries=None, includes=None, force_c=False, output_suffix='.js', emcc_args=None, output_basename=None):
1337+
def build(self, filename, libraries=None, includes=None, force_c=False, emcc_args=None, output_basename=None, output_suffix=None):
13381338
if not os.path.exists(filename):
13391339
filename = test_file(filename)
13401340
compiler = [compiler_for(filename, force_c)]
@@ -1343,6 +1343,9 @@ def build(self, filename, libraries=None, includes=None, force_c=False, output_s
13431343
assert shared.suffix(filename) != '.c', 'force_c is not needed for source files ending in .c'
13441344
compiler.append('-xc')
13451345

1346+
if not output_suffix:
1347+
output_suffix = '.mjs' if emcc_args and '-sEXPORT_ES6' in emcc_args else '.js'
1348+
13461349
if output_basename:
13471350
output = output_basename + output_suffix
13481351
else:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1542
1+
1543
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3210
1+
3222

test/test_sockets.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ def __enter__(self):
117117
npm_checked = True
118118

119119
# compile the server
120-
proc = run_process([EMCC, '-Werror', test_file(self.filename), '-o', 'server.js', '-DSOCKK=%d' % self.listen_port] + self.args)
120+
suffix = '.mjs' if '-sEXPORT_ES6' in self.args else '.js'
121+
proc = run_process([EMCC, '-Werror', test_file(self.filename), '-o', 'server' + suffix, '-DSOCKK=%d' % self.listen_port] + self.args)
121122
print('Socket server build: out:', proc.stdout or '', '/ err:', proc.stderr or '')
122123

123-
process = Popen(config.NODE_JS + ['server.js'])
124+
process = Popen(config.NODE_JS + ['server' + suffix])
124125
self.processes.append(process)
125126
return self
126127

@@ -166,11 +167,6 @@ def setUpClass(cls):
166167
print('Running the socket tests. Make sure the browser allows popups from localhost.')
167168
print()
168169

169-
# Use emscripten root for node module lookup. This is needed because the unit tests each
170-
# run with CWD set to a temporary directory outside the emscripten tree.
171-
print('Setting NODE_PATH=' + path_from_root('node_modules'))
172-
os.environ['NODE_PATH'] = path_from_root('node_modules')
173-
174170
# Note: in the WebsockifyServerHarness and CompiledServerHarness tests below, explicitly use
175171
# consecutive server listen ports, because server teardown might not occur deterministically
176172
# (python dtor time) and is a bit racy.
@@ -283,7 +279,7 @@ def test_enet(self):
283279
@crossplatform
284280
@parameterized({
285281
'native': [WebsockifyServerHarness, 59160, ['-DTEST_DGRAM=0']],
286-
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0']],
282+
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0', '-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')]],
287283
'udp': [CompiledServerHarness, 59164, ['-DTEST_DGRAM=1']],
288284
'pthread': [CompiledServerHarness, 59166, ['-pthread', '-sPROXY_TO_PTHREAD']],
289285
})

0 commit comments

Comments
 (0)