From 4fda40e179e6ea62c87f2065f789bf26e200e549 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Tue, 22 Oct 2019 14:48:41 +0300 Subject: [PATCH 1/2] repl: add test for dynamic import with relative path --- test/parallel/test-repl-dynamic-import.js | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/parallel/test-repl-dynamic-import.js diff --git a/test/parallel/test-repl-dynamic-import.js b/test/parallel/test-repl-dynamic-import.js new file mode 100644 index 00000000000000..bd2cdc415d9a41 --- /dev/null +++ b/test/parallel/test-repl-dynamic-import.js @@ -0,0 +1,46 @@ +'use strict'; + +// Flags: --experimental-modules + +const common = require('../common'); +const fixtures = require('../common/fixtures'); +const assert = require('assert'); +const net = require('net'); + +if (!common.isMainThread) + common.skip('process.chdir is not available in Workers'); + +process.chdir(fixtures.fixturesDir); +const repl = require('repl'); + +const server = net.createServer((conn) => { + repl.start('', conn).on('exit', () => { + conn.destroy(); + server.close(); + }); +}); + +const host = common.localhostIPv4; +const port = 0; +const options = { host, port }; + +let answer = ''; +let expectedDataEvents = 2; +server.listen(options, function() { + options.port = this.address().port; + const conn = net.connect(options); + conn.setEncoding('utf8'); + conn.on('data', (data) => { + answer += data; + if (--expectedDataEvents === 0) { + conn.write('.exit\n'); + } + }); + conn.write('import("./es-modules/message.mjs").then(console.log)\n'); +}); + +process.on('exit', function() { + assert.strictEqual(/Cannot find module/.test(answer), false); + assert.strictEqual(/Error/.test(answer), false); + assert.strictEqual(/{ message: 'A message' }/.test(answer), true); +}); From 3c510e1e47d60490e4276c9945187143c3b10f83 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Tue, 22 Oct 2019 14:48:53 +0300 Subject: [PATCH 2/2] repl: fix current path for dynamic module imports Adds a trailing slash to the current directory's path that's used as the second argument of `asyncESM.ESMLoader.import(specifier, pwd)`. Without the slash, relative paths will incorrectly resolve from one directory level higher up. --- lib/repl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/repl.js b/lib/repl.js index 88b0f5658489fc..b2b4574b70e85d 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -325,7 +325,7 @@ function REPLServer(prompt, let pwd; try { const { pathToFileURL } = require('url'); - pwd = pathToFileURL(process.cwd()).href; + pwd = pathToFileURL(`${process.cwd()}/`).href; } catch { } while (true) {