[njs] Change: imported modules are not resolved relative to current dir.

Dmitry Volyntsev xeioex at nginx.com
Wed Jan 24 00:40:17 UTC 2024


details:   https://hg.nginx.org/njs/rev/7eaaa7d57636
branches:  
changeset: 2271:7eaaa7d57636
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Jan 23 16:33:52 2024 -0800
description:
Change: imported modules are not resolved relative to current dir.

Previously, when a module was imported with a relative path
it was looked for first in the directory of the importing context
(global, or a module).

For example when:
    main.js:
        import libs/lib1.js;

    libs/lib1.js:
        import lib2.js;

lib2.js was looked for first in libs/.

Now, it is only looked for in directories speficied with js_path
and a directory of nginx configuration file.

diffstat:

 src/njs_module.c                  |  24 ++++++------------------
 test/js/import_chain.t.js         |   2 +-
 test/js/import_relative_path.t.js |  10 ----------
 test/shell_test.exp               |   4 ++--
 4 files changed, 9 insertions(+), 31 deletions(-)

diffs (120 lines):

diff -r 6485ad23565e -r 7eaaa7d57636 src/njs_module.c
--- a/src/njs_module.c	Tue Jan 23 16:33:29 2024 -0800
+++ b/src/njs_module.c	Tue Jan 23 16:33:52 2024 -0800
@@ -16,8 +16,7 @@ typedef struct {
 } njs_module_info_t;
 
 
-static njs_int_t njs_module_lookup(njs_vm_t *vm, const njs_str_t *cwd,
-    njs_module_info_t *info);
+static njs_int_t njs_module_lookup(njs_vm_t *vm, njs_module_info_t *info);
 static njs_int_t njs_module_path(njs_vm_t *vm, const njs_str_t *dir,
     njs_module_info_t *info);
 static njs_int_t njs_module_read(njs_vm_t *vm, int fd, njs_str_t *body);
@@ -45,7 +44,7 @@ njs_parser_module(njs_parser_t *parser, 
         goto done;
     }
 
-    external = parser;
+    external = NULL;
     loader = njs_default_module_loader;
 
     if (vm->module_loader != NULL) {
@@ -70,7 +69,7 @@ done:
 
 
 static njs_int_t
-njs_module_lookup(njs_vm_t *vm, const njs_str_t *cwd, njs_module_info_t *info)
+njs_module_lookup(njs_vm_t *vm, njs_module_info_t *info)
 {
     njs_int_t   ret;
     njs_str_t   *path;
@@ -80,12 +79,6 @@ njs_module_lookup(njs_vm_t *vm, const nj
         return njs_module_path(vm, NULL, info);
     }
 
-    ret = njs_module_path(vm, cwd, info);
-
-    if (ret != NJS_DECLINED) {
-        return ret;
-    }
-
     if (vm->paths == NULL) {
         return NJS_DECLINED;
     }
@@ -158,7 +151,6 @@ njs_module_path(njs_vm_t *vm, const njs_
         return NJS_DECLINED;
     }
 
-
     info->file.start = (u_char *) &info->path[0];
     info->file.length = njs_strlen(info->file.start);
 
@@ -359,24 +351,20 @@ njs_module_require(njs_vm_t *vm, njs_val
 
 
 static njs_mod_t *
-njs_default_module_loader(njs_vm_t *vm, njs_external_ptr_t external,
+njs_default_module_loader(njs_vm_t *vm, njs_external_ptr_t unused,
     njs_str_t *name)
 {
     u_char             *start;
     njs_int_t          ret;
-    njs_str_t          cwd, text;
+    njs_str_t          text;
     njs_mod_t          *module;
-    njs_parser_t       *prev;
     njs_module_info_t  info;
 
-    prev = external;
-
     njs_memzero(&info, sizeof(njs_module_info_t));
 
     info.name = *name;
-    njs_file_dirname(&prev->lexer->file, &cwd);
 
-    ret = njs_module_lookup(vm, &cwd, &info);
+    ret = njs_module_lookup(vm, &info);
     if (njs_slow_path(ret != NJS_OK)) {
         return NULL;
     }
diff -r 6485ad23565e -r 7eaaa7d57636 test/js/import_chain.t.js
--- a/test/js/import_chain.t.js	Tue Jan 23 16:33:29 2024 -0800
+++ b/test/js/import_chain.t.js	Tue Jan 23 16:33:52 2024 -0800
@@ -1,7 +1,7 @@
 /*---
 includes: []
 flags: []
-paths: [test/js/module/, test/js/module/libs/]
+paths: [test/js/module/, test/js/module/libs/, test/js/module/sub]
 ---*/
 
 import lib2   from 'lib2.js';
diff -r 6485ad23565e -r 7eaaa7d57636 test/js/import_relative_path.t.js
--- a/test/js/import_relative_path.t.js	Tue Jan 23 16:33:29 2024 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-/*---
-includes: []
-flags: []
-paths: [test/js/module/]
----*/
-
-import name from 'name.js';
-import hash from 'libs/hash.js';
-
-assert.sameValue(hash.name, "libs.name");
diff -r 6485ad23565e -r 7eaaa7d57636 test/shell_test.exp
--- a/test/shell_test.exp	Tue Jan 23 16:33:29 2024 -0800
+++ b/test/shell_test.exp	Tue Jan 23 16:33:52 2024 -0800
@@ -563,8 +563,8 @@ njs_test {
 
 # quiet mode
 
-njs_run {"-q" "test/js/import_relative_path.t.js"} \
-        "SyntaxError: Cannot find module \"name.js\" in 7"
+njs_run {"-q" "test/js/import_chain.t.js"} \
+        "SyntaxError: Cannot find module \"lib2.js\" in 7"
 
 # sandboxing
 


More information about the nginx-devel mailing list