Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: fix error thrown from require(esm) hitting TLA repeatedly #55520

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,9 @@ E('ERR_PERFORMANCE_MEASURE_INVALID_OPTIONS', '%s', TypeError);
E('ERR_QUIC_CONNECTION_FAILED', 'QUIC connection failed', Error);
E('ERR_QUIC_ENDPOINT_CLOSED', 'QUIC endpoint closed: %s (%d)', Error);
E('ERR_QUIC_OPEN_STREAM_FAILED', 'Failed to open QUIC stream', Error);
E('ERR_REQUIRE_ASYNC_MODULE', 'require() cannot be used on an ESM ' +
'graph with top-level await. Use import() instead. To see where the' +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think referencing the graph is, whilst completely accurate, too esoteric. I expect many users won't even know what a graph is.

Copy link
Member Author

@joyeecheung joyeecheung Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the word "graph" in general or using "graph" to mean a module and its dependencies? I believe graph is a very common term used across different programming languages, including referring to the concept of "modules and its dependencies". There are also many module graph visualization tools out there, and its used extensively in materials such as MDN's guide on ESM and V8's user-facing explainer about ESM. If you Google "javascript module graph" you can find numerous tutorials and blog posts introducing ESM mentioning the concept of module graphs, and I suspect many learned about ESM by reading them so they should already know what this is.

' top-level await comes from, use --experimental-print-required-tla.', Error);
E('ERR_REQUIRE_CYCLE_MODULE', '%s', Error);
E('ERR_REQUIRE_ESM',
function(filename, hasEsmSyntax, parentPath = null, packageJsonPath = null) {
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const { imported_cjs_symbol } = internalBinding('symbols');

const assert = require('internal/assert');
const {
ERR_REQUIRE_ASYNC_MODULE,
ERR_REQUIRE_CYCLE_MODULE,
ERR_REQUIRE_ESM,
ERR_NETWORK_IMPORT_DISALLOWED,
Expand Down Expand Up @@ -302,6 +303,9 @@ class ModuleLoader {
// evaluated at this point.
if (job !== undefined) {
mod[kRequiredModuleSymbol] = job.module;
if (job.module.async) {
throw new ERR_REQUIRE_ASYNC_MODULE();
}
if (job.module.getStatus() !== kEvaluated) {
const parentFilename = urlToFilename(parent?.filename);
let message = `Cannot require() ES Module ${filename} in a cycle.`;
Expand Down
16 changes: 14 additions & 2 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ const resolvedPromise = PromiseResolve();
const {
setHasStartedUserESMExecution,
} = require('internal/modules/helpers');
const { getOptionValue } = require('internal/options');
const noop = FunctionPrototype;

const {
ERR_REQUIRE_ASYNC_MODULE,
} = require('internal/errors').codes;
let hasPausedEntry = false;

const CJSGlobalLike = [
Expand Down Expand Up @@ -362,7 +365,16 @@ class ModuleJobSync extends ModuleJobBase {

runSync() {
// TODO(joyeecheung): add the error decoration logic from the async instantiate.
this.module.instantiateSync();
this.module.async = this.module.instantiateSync();
// If --experimental-print-required-tla is true, proceeds to evaluation even
// if it's async because we want to search for the TLA and help users locate
// them.
// TODO(joyeecheung): track the asynchroniticy using v8::Module::HasTopLevelAwait()
// and we'll be able to throw right after compilation of the modules, using acron
// to find and print the TLA.
if (this.module.async && !getOptionValue('--experimental-print-required-tla')) {
throw new ERR_REQUIRE_ASYNC_MODULE();
}
setHasStartedUserESMExecution();
const namespace = this.module.evaluateSync();
return { __proto__: null, module: this.module, namespace };
Expand Down
12 changes: 3 additions & 9 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::IntegrityLevel;
using v8::Isolate;
using v8::Local;
using v8::MaybeLocal;
Expand Down Expand Up @@ -332,7 +331,6 @@ void ModuleWrap::New(const FunctionCallbackInfo<Value>& args) {

obj->contextify_context_ = contextify_context;

that->SetIntegrityLevel(context, IntegrityLevel::kFrozen);
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
args.GetReturnValue().Set(that);
}

Expand Down Expand Up @@ -635,13 +633,9 @@ void ModuleWrap::InstantiateSync(const FunctionCallbackInfo<Value>& args) {
}
}

// If --experimental-print-required-tla is true, proceeds to evaluation even
// if it's async because we want to search for the TLA and help users locate
// them.
if (module->IsGraphAsync() && !env->options()->print_required_tla) {
THROW_ERR_REQUIRE_ASYNC_MODULE(env);
return;
}
// TODO(joyeecheung): record Module::HasTopLevelAwait() in every ModuleWrap
// and infer the asynchronicity from a module's children during linking.
args.GetReturnValue().Set(module->IsGraphAsync());
}

void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
Expand Down
16 changes: 16 additions & 0 deletions test/es-module/test-require-module-tla-retry-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This tests that after failing to require an ESM that contains TLA,
// retrying with import() still works, and produces consistent results.
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
'use strict';
require('../common');
const assert = require('assert');

assert.throws(() => {
require('../fixtures/es-modules/tla/resolved.mjs');
}, {
code: 'ERR_REQUIRE_ASYNC_MODULE'
});
assert.throws(() => {
require('../fixtures/es-modules/tla/resolved.mjs');
}, {
code: 'ERR_REQUIRE_ASYNC_MODULE'
});
Loading