Skip to content

Commit 28b5b9a

Browse files
joyeecheungruyadorno
authored andcommitted
module: allow ESM that failed to be required to be re-imported
When a ESM module cannot be loaded by require due to the presence of TLA, its module status would be stopped at kInstantiated. In this case, when it's imported again, we should allow it to be evaluated asynchronously, as it's also a common pattern for users to retry with dynamic import when require fails. PR-URL: #55502 Backport-PR-URL: #55217Fixes: #55500 Refs: #52697 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent 6ac3400 commit 28b5b9a

File tree

8 files changed

+152
-4
lines changed

8 files changed

+152
-4
lines changed

‎lib/internal/modules/esm/module_job.js‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) =>{
2222
debug=fn;
2323
});
2424

25-
const{ ModuleWrap,kEvaluated}=internalBinding('module_wrap');
25+
const{ ModuleWrap,kInstantiated}=internalBinding('module_wrap');
2626
const{
2727
privateSymbols: {
2828
entry_point_module_private_symbol,
@@ -354,10 +354,26 @@ class ModuleJobSync extends ModuleJobBase{
354354
}
355355

356356
asyncrun(){
357+
// This path is hit by a require'd module that is imported again.
357358
conststatus=this.module.getStatus();
358-
assert(status===kEvaluated,
359-
`A require()-d module that is imported again must be evaluated. Status = ${status}`);
360-
return{__proto__: null,module: this.module};
359+
if(status>kInstantiated){
360+
if(this.evaluationPromise){
361+
awaitthis.evaluationPromise;
362+
}
363+
return{__proto__: null,module: this.module};
364+
}elseif(status===kInstantiated){
365+
// The evaluation may have been canceled because instantiateSync() detected TLA first.
366+
// But when it is imported again, it's fine to re-evaluate it asynchronously.
367+
consttimeout=-1;
368+
constbreakOnSigint=false;
369+
this.evaluationPromise=this.module.evaluate(timeout,breakOnSigint);
370+
awaitthis.evaluationPromise;
371+
this.evaluationPromise=undefined;
372+
return{__proto__: null,module: this.module};
373+
}
374+
375+
assert.fail('Unexpected status of a module that is imported again after being required. '+
376+
`Status = ${status}`);
361377
}
362378

363379
runSync(){
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This tests that after failing to require an ESM that contains TLA,
2+
// retrying with import() still works, and produces consistent results.
3+
'use strict';
4+
constcommon=require('../common');
5+
constassert=require('assert');
6+
7+
const{ exportedReject }=require('../fixtures/es-modules/tla/export-promise.mjs');
8+
9+
assert.throws(()=>{
10+
require('../fixtures/es-modules/tla/await-export-promise.mjs');
11+
},{
12+
code: 'ERR_REQUIRE_ASYNC_MODULE'
13+
});
14+
15+
constinterval=setInterval(()=>{},1000);// Keep the test running, because await alone doesn't.
16+
consterr=newError('rejected');
17+
18+
constp1=import('../fixtures/es-modules/tla/await-export-promise.mjs')
19+
.then(common.mustNotCall(),common.mustCall((e)=>{
20+
assert.strictEqual(e,err);
21+
}));
22+
23+
constp2=import('../fixtures/es-modules/tla/await-export-promise.mjs')
24+
.then(common.mustNotCall(),common.mustCall((e)=>{
25+
assert.strictEqual(e,err);
26+
}));
27+
28+
constp3=import('../fixtures/es-modules/tla/await-export-promise.mjs')
29+
.then(common.mustNotCall(),common.mustCall((e)=>{
30+
assert.strictEqual(e,err);
31+
}));
32+
33+
exportedReject(err);
34+
35+
Promise.all([p1,p2,p3]).then(common.mustCall(()=>{clearInterval(interval);}));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This tests that after failing to require an ESM that contains TLA,
2+
// retrying with import() still works, and produces consistent results.
3+
'use strict';
4+
constcommon=require('../common');
5+
constassert=require('assert');
6+
7+
const{ exportedResolve }=require('../fixtures/es-modules/tla/export-promise.mjs');
8+
9+
assert.throws(()=>{
10+
require('../fixtures/es-modules/tla/await-export-promise.mjs');
11+
},{
12+
code: 'ERR_REQUIRE_ASYNC_MODULE'
13+
});
14+
15+
constinterval=setInterval(()=>{},1000);// Keep the test running, because await alone doesn't.
16+
constvalue={hello: 'world'};
17+
18+
constp1=import('../fixtures/es-modules/tla/await-export-promise.mjs').then(common.mustCall((ns)=>{
19+
assert.strictEqual(ns.default,value);
20+
}),common.mustNotCall());
21+
22+
constp2=import('../fixtures/es-modules/tla/await-export-promise.mjs').then(common.mustCall((ns)=>{
23+
assert.strictEqual(ns.default,value);
24+
}),common.mustNotCall());
25+
26+
constp3=import('../fixtures/es-modules/tla/await-export-promise.mjs').then(common.mustCall((ns)=>{
27+
assert.strictEqual(ns.default,value);
28+
}),common.mustNotCall());
29+
30+
exportedResolve(value);
31+
32+
Promise.all([p1,p2,p3]).then(common.mustCall(()=>{clearInterval(interval);}));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// This tests that after loading a ESM with import() and then retrying
2+
// with require(), it errors as expected, and produces consistent results.
3+
'use strict';
4+
constcommon=require('../common');
5+
constassert=require('assert');
6+
7+
letns;
8+
asyncfunctiontest(){
9+
constnewNs=awaitimport('../fixtures/es-modules/tla/export-async.mjs');
10+
if(ns===undefined){
11+
ns=newNs;
12+
}else{
13+
// Check that re-evalaution is returning the same namespace.
14+
assert.strictEqual(ns,newNs);
15+
}
16+
assert.strictEqual(ns.hello,'world');
17+
18+
assert.throws(()=>{
19+
require('../fixtures/es-modules/tla/export-async.mjs');
20+
},{
21+
code: 'ERR_REQUIRE_ASYNC_MODULE'
22+
});
23+
}
24+
25+
// Run the test twice to check consistency after caching.
26+
test().then(common.mustCall(test)).catch(common.mustNotCall());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This tests that after failing to require an ESM that contains TLA,
2+
// retrying with import() still works, and produces consistent results.
3+
'use strict';
4+
constcommon=require('../common');
5+
constassert=require('assert');
6+
7+
letns;
8+
asyncfunctiontest(){
9+
assert.throws(()=>{
10+
require('../fixtures/es-modules/tla/export-async.mjs');
11+
},{
12+
code: 'ERR_REQUIRE_ASYNC_MODULE'
13+
});
14+
constnewNs=awaitimport('../fixtures/es-modules/tla/export-async.mjs');
15+
if(ns===undefined){
16+
ns=newNs;
17+
}else{
18+
// Check that re-evalaution is returning the same namespace.
19+
assert.strictEqual(ns,newNs);
20+
}
21+
assert.strictEqual(ns.hello,'world');
22+
}
23+
24+
// Run the test twice to check consistency after caching.
25+
test().then(common.mustCall(test)).catch(common.mustNotCall());
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
importpromisefrom'./export-promise.mjs';
2+
letresult;
3+
result=awaitpromise;
4+
exportdefaultresult;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lethello=awaitPromise.resolve('world');
2+
export{hello};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
letexportedResolve;
2+
letexportedReject;
3+
constpromise=newPromise((resolve,reject)=>{
4+
exportedResolve=resolve;
5+
exportedReject=reject;
6+
});
7+
exportdefaultpromise;
8+
export{exportedResolve,exportedReject};

0 commit comments

Comments
(0)