Skip to content

Commit

Permalink
module: fix crash on multiline named cjs imports
Browse files Browse the repository at this point in the history
The node process crashes when trying to parse a multiline import
statement for named exports of a CommonJS module:

    TypeError: Cannot read property '0' of null
          at ModuleJob._instantiate (internal/modules/esm/module_job.js:112:77)
          at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
          at async Loader.import (internal/modules/esm/loader.js:165:24)
          at async rejects.name (file:///***/node/test/es-module/test-esm-cjs-named-error.mjs:56:3)
          at async waitForActual (assert.js:721:5)
          at async rejects (assert.js:830:25),

The reason is that the regexp that is currently used to decorate the
original error fails for multi line import statements.

Unfortunately the undecorated error stack only contains the single line
which causes the import to fail:

    file:///***/node/test/fixtures/es-modules/package-cjs-named-error/multi-line.mjs:2
      comeOn,
      ^^^^^^
    SyntaxError: The requested module './fail.cjs' does not provide an export named 'comeOn'
        at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
        at async ModuleJob.run (internal/modules/esm/module_job.js:141:5)
        at async Loader.import (internal/modules/esm/loader.js:165:24)
        at async rejects.name (file:///***/node/test/es-module/test-esm-cjs-named-error.mjs:56:3)
        at async waitForActual (assert.js:721:5)
        at async rejects (assert.js:830:25)

Hence, for multiline import statements we cannot create an equivalent
piece of code that uses default import followed by an object
destructuring assignment.

In any case the node process should definitely not crash. So until we
have a more sophisticated way of extracting the entire problematic
multiline import statement, show the code example only for single-line
imports where the current regexp approach works well.

Refs: nodejs#35259
  • Loading branch information
ctavan committed Sep 20, 2020
1 parent 039c274 commit 6298277
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
21 changes: 14 additions & 7 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,23 @@ class ModuleJob {
await this.loader.resolve(childSpecifier, parentFileUrl);
const format = await this.loader.getFormat(childFileURL);
if (format === 'commonjs') {
const importStatement = splitStack[1];
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
const destructuringAssignment = StringPrototypeReplace(namedImports, /\s+as\s+/g, ': ');
e.message = `The requested module '${childSpecifier}' is expected ` +
'to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default ' +
'export.\n' +
'For example:\n' +
`import pkg from '${childSpecifier}';\n` +
`const ${destructuringAssignment} = pkg;`;
'export.';
// TODO(@ctavan): The original error stack only provides the single
// line which causes the error. For multi-line import statements we
// cannot generate an equivalent object descructuring assignment by
// just parsing the error stack.
const importStatement = splitStack[1];
const oneLineNamedImports = StringPrototypeMatch(importStatement, /{.*}/);
if (oneLineNamedImports) {
const destructuringAssignment =
StringPrototypeReplace(oneLineNamedImports[0], /\s+as\s+/g, ': ');
e.message += '\nFor example:\n' +
`import pkg from '${childSpecifier}';\n` +
`const ${destructuringAssignment} = pkg;`;
}
const newStack = StringPrototypeSplit(e.stack, '\n');
newStack[3] = `SyntaxError: ${e.message}`;
e.stack = ArrayPrototypeJoin(newStack, '\n');
Expand Down
11 changes: 11 additions & 0 deletions test/es-module/test-esm-cjs-named-error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' +
'import pkg from \'./fail.cjs\';\n' +
'const { comeOn } = pkg;';

const expectedWithoutExample = 'The requested module \'./fail.cjs\' is ' +
'expected to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default export.';

const expectedRenamed = 'The requested module \'./fail.cjs\' is expected to ' +
'be of type CommonJS, which does not support named exports. CommonJS ' +
'modules can be imported by importing the default export.\n' +
Expand Down Expand Up @@ -52,6 +56,13 @@ rejects(async () => {
message: expectedRenamed
}, 'should correctly format named imports with renames');

rejects(async () => {
await import(`${fixtureBase}/multi-line.mjs`);
}, {
name: 'SyntaxError',
message: expectedWithoutExample,
}, 'should correctly format named imports across multiple lines');

rejects(async () => {
await import(`${fixtureBase}/json-hack.mjs`);
}, {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/es-modules/package-cjs-named-error/fail.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
comeOn: 'fhqwhgads'
comeOn: 'fhqwhgads',
rightNow: 'abcde',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {
comeOn,
rightNow,
} from './fail.cjs';

0 comments on commit 6298277

Please sign in to comment.