From: Dmitry Volyntsev Date: Wed, 30 Sep 2026 06:12:30 +0000 (-0700) Subject: VM: report import of a module removed after failed compilation X-Git-Tag: 1.0.2~3 X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=e5728cba528ca75993491b2d6d8bb22fb28e732d;p=njs.git VM: report import of a module removed after failed compilation A module whose compilation fails is removed from the cache, but another module compiled earlier in the same cycle may still import it. Executing such an import returned an error without setting an exception, so the shell reported an unrelated TypeError. Throw "Cannot load module" instead, and add a shell test for a cyclic dependency of a module that fails to compile. --- diff --git a/src/njs_vmcode.c b/src/njs_vmcode.c index d09450b1..088959df 100644 --- a/src/njs_vmcode.c +++ b/src/njs_vmcode.c @@ -2688,6 +2688,10 @@ njs_vmcode_import(njs_vm_t *vm, njs_mod_t *module, njs_value_t *retval) m = njs_module_find(vm, &module->name, 0); if (njs_slow_path(m == NULL)) { + if (!njs_is_valid(&vm->exception)) { + njs_error(vm, "Cannot load module \"%V\"", &module->name); + } + return NJS_ERROR; } diff --git a/test/js/module/cyclic_compile_exception.js b/test/js/module/cyclic_compile_exception.js new file mode 100644 index 00000000..c44721a1 --- /dev/null +++ b/test/js/module/cyclic_compile_exception.js @@ -0,0 +1,3 @@ +import dep from './cyclic_compile_exception_dep.js'; +export default dep; +@ diff --git a/test/js/module/cyclic_compile_exception_dep.js b/test/js/module/cyclic_compile_exception_dep.js new file mode 100644 index 00000000..7caab285 --- /dev/null +++ b/test/js/module/cyclic_compile_exception_dep.js @@ -0,0 +1,2 @@ +import parent from './cyclic_compile_exception.js'; +export default 'dep'; diff --git a/test/shell_test.exp b/test/shell_test.exp index 889b5d56..5c7f195b 100644 --- a/test/shell_test.exp +++ b/test/shell_test.exp @@ -457,6 +457,15 @@ njs_test { "'name'\r\n>> "} } "-n njs" +njs_test { + {"import broken from './test/js/module/cyclic_compile_exception.js';\r\n" + "SyntaxError:*\r\n*>> "} + {"import dep from './test/js/module/cyclic_compile_exception_dep.js';\r\n" + "Error: Cannot load module*cyclic_compile_exception.js*\r\n*>> "} + {"1 + 1\r\n" + "2\r\n>> "} +} "-n njs" + # CLI OPTIONS # help