Skip to content

Commit dc712da

Browse files
camillobruniCommit Bot
authored andcommitted
[d8] Print referrer for failing module imports
Improve debugging failing module imports by also printing the module file where the import originated. Bug: v8:10668 Change-Id: I519b15d5da635a15eb2c0f23f18a6c53e358eabd Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2489680 Reviewed-by: Toon Verwaest <verwaest@chromium.org> Commit-Queue: Camillo Bruni <cbruni@chromium.org> Cr-Commit-Position: refs/heads/master@{#70740}
1 parent 3d1d61d commit dc712da

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/d8/d8.cc

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,8 @@ MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
835835

836836
} // anonymous namespace
837837

838-
MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context,
838+
MaybeLocal<Module> Shell::FetchModuleTree(Local<Module> referrer,
839+
Local<Context> context,
839840
const std::string& file_name) {
840841
DCHECK(IsAbsolutePath(file_name));
841842
Isolate* isolate = context->GetIsolate();
@@ -848,8 +849,16 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context,
848849
source_text = ReadFile(isolate, fallback_file_name.c_str());
849850
}
850851
}
852+
853+
ModuleEmbedderData* d = GetModuleDataFromContext(context);
851854
if (source_text.IsEmpty()) {
852-
std::string msg = "d8: Error reading module from " + file_name;
855+
std::string msg = "d8: Error reading module from " + file_name;
856+
if (!referrer.IsEmpty()) {
857+
auto specifier_it =
858+
d->module_to_specifier_map.find(Global<Module>(isolate, referrer));
859+
CHECK(specifier_it != d->module_to_specifier_map.end());
860+
msg += "\n imported by " + specifier_it->second;
861+
}
853862
Throw(isolate, msg.c_str());
854863
return MaybeLocal<Module>();
855864
}
@@ -863,7 +872,6 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context,
863872
return MaybeLocal<Module>();
864873
}
865874

866-
ModuleEmbedderData* d = GetModuleDataFromContext(context);
867875
CHECK(d->specifier_to_module_map
868876
.insert(std::make_pair(file_name, Global<Module>(isolate, module)))
869877
.second);
@@ -878,7 +886,7 @@ MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context,
878886
std::string absolute_path =
879887
NormalizePath(ToSTLString(isolate, name), dir_name);
880888
if (d->specifier_to_module_map.count(absolute_path)) continue;
881-
if (FetchModuleTree(context, absolute_path).IsEmpty()) {
889+
if (FetchModuleTree(module, context, absolute_path).IsEmpty()) {
882890
return MaybeLocal<Module>();
883891
}
884892
}
@@ -1023,7 +1031,8 @@ void Shell::DoHostImportModuleDynamically(void* import_data) {
10231031
auto module_it = d->specifier_to_module_map.find(absolute_path);
10241032
if (module_it != d->specifier_to_module_map.end()) {
10251033
root_module = module_it->second.Get(isolate);
1026-
} else if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
1034+
} else if (!FetchModuleTree(Local<Module>(), realm, absolute_path)
1035+
.ToLocal(&root_module)) {
10271036
CHECK(try_catch.HasCaught());
10281037
resolver->Reject(realm, try_catch.Exception()).ToChecked();
10291038
return;
@@ -1090,7 +1099,8 @@ bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
10901099

10911100
Local<Module> root_module;
10921101

1093-
if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
1102+
if (!FetchModuleTree(Local<Module>(), realm, absolute_path)
1103+
.ToLocal(&root_module)) {
10941104
CHECK(try_catch.HasCaught());
10951105
ReportException(isolate, &try_catch);
10961106
return false;

src/d8/d8.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ class Shell : public i::AllStatic {
608608
v8::MaybeLocal<Value> global_object);
609609
static void DisposeRealm(const v8::FunctionCallbackInfo<v8::Value>& args,
610610
int index);
611-
static MaybeLocal<Module> FetchModuleTree(v8::Local<v8::Context> context,
611+
static MaybeLocal<Module> FetchModuleTree(v8::Local<v8::Module> origin_module,
612+
v8::Local<v8::Context> context,
612613
const std::string& file_name);
613614
static ScriptCompiler::CachedData* LookupCodeCache(Isolate* isolate,
614615
Local<Value> name);

0 commit comments

Comments
 (0)