]> git.kaiwu.me - njs.git/commitdiff
VM: report import of a module removed after failed compilation
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 30 Sep 2026 06:12:30 +0000 (23:12 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Wed, 30 Sep 2026 18:56:11 +0000 (11:56 -0700)
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.

src/njs_vmcode.c
test/js/module/cyclic_compile_exception.js [new file with mode: 0644]
test/js/module/cyclic_compile_exception_dep.js [new file with mode: 0644]
test/shell_test.exp

index d09450b19483fbd3617241c7bc64f36e0c0d0b86..088959dfe1ce34b83fbb731a8b518dc328c15f2d 100644 (file)
@@ -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 (file)
index 0000000..c44721a
--- /dev/null
@@ -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 (file)
index 0000000..7caab28
--- /dev/null
@@ -0,0 +1,2 @@
+import parent from './cyclic_compile_exception.js';
+export default 'dep';
index 889b5d56e14a71ce17f81879fc7161568735871a..5c7f195b59da96278400e686c43350fd2da345e1 100644 (file)
@@ -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