Skip to content

Commit

Permalink
Fix createRequire so it works for local node_modules.
Browse files Browse the repository at this point in the history
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 emscripten-core#23265 which itself was an attempt to revert 23169.

Fixes: emscripten-core#23503
  • Loading branch information
sbc100 committed Jan 28, 2025
1 parent 4e65d05 commit 3c07854
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ if (ENVIRONMENT_IS_NODE) {
// We need to use `createRequire()` to construct the require()` function.
const { createRequire } = await import('module');
/** @suppress{duplicate} */
var require = createRequire('/');
var require = createRequire(import.meta.url);
#endif

#if PTHREADS || WASM_WORKERS
Expand Down
5 changes: 4 additions & 1 deletion test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ def verify_es5(self, filename):
self.fail('es-check failed to verify ES5 output compliance')

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

if not output_suffix:
output_suffix = '.mjs' if emcc_args and '-sEXPORT_ES6' in emcc_args else '.js'

if output_basename:
output = output_basename + output_suffix
else:
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1539
1540
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3209
3221
12 changes: 4 additions & 8 deletions test/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ def __enter__(self):
npm_checked = True

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

process = Popen(config.NODE_JS + ['server.js'])
process = Popen(config.NODE_JS + ['server' + suffix])
self.processes.append(process)
return self

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

# Use emscripten root for node module lookup. This is needed because the unit tests each
# run with CWD set to a temporary directory outside the emscripten tree.
print('Setting NODE_PATH=' + path_from_root('node_modules'))
os.environ['NODE_PATH'] = path_from_root('node_modules')

# Note: in the WebsockifyServerHarness and CompiledServerHarness tests below, explicitly use
# consecutive server listen ports, because server teardown might not occur deterministically
# (python dtor time) and is a bit racy.
Expand Down Expand Up @@ -283,7 +279,7 @@ def test_enet(self):
@crossplatform
@parameterized({
'native': [WebsockifyServerHarness, 59160, ['-DTEST_DGRAM=0']],
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0']],
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0', '-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')]],
'udp': [CompiledServerHarness, 59164, ['-DTEST_DGRAM=1']],
'pthread': [CompiledServerHarness, 59166, ['-pthread', '-sPROXY_TO_PTHREAD']],
})
Expand Down

0 comments on commit 3c07854

Please sign in to comment.