]> git.kaiwu.me - njs.git/commitdiff
Change: imported modules are not resolved relative to current dir.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 24 Jan 2024 00:33:52 +0000 (16:33 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 24 Jan 2024 00:33:52 +0000 (16:33 -0800)
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.

src/njs_module.c
test/js/import_chain.t.js
test/js/import_relative_path.t.js [deleted file]
test/shell_test.exp

index 37620007229402e6b2bf301221334918b4d2ede3..6198998588065cfa3887f9871394fd3b9685b9c7 100644 (file)
@@ -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, njs_str_t *name)
         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 njs_str_t *cwd, njs_module_info_t *info)
         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_str_t *dir, njs_module_info_t *info)
         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_value_t *args, njs_uint_t nargs,
 
 
 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;
     }
index 70d8c26df483ec82e03773c230236b8113625d67..8efb6c39b59ffb593c59c77f65ccc0db97ad137c 100644 (file)
@@ -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 --git a/test/js/import_relative_path.t.js b/test/js/import_relative_path.t.js
deleted file mode 100644 (file)
index 6a9f3cc..0000000
+++ /dev/null
@@ -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");
index 336c3d3f994486c748f46aa54484a2595a0fa159..140db33f248f83504a8664ce9db5f34dcd7ca66a 100644 (file)
@@ -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