From: Dmitry Volyntsev Date: Wed, 24 Jan 2024 00:33:52 +0000 (-0800) Subject: Change: imported modules are not resolved relative to current dir. X-Git-Tag: 0.8.3~10 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=8fd9bd0930990eb2d4f6b5046328415b6cb715e5;p=njs.git 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. --- diff --git a/src/njs_module.c b/src/njs_module.c index 37620007..61989985 100644 --- a/src/njs_module.c +++ b/src/njs_module.c @@ -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; } diff --git a/test/js/import_chain.t.js b/test/js/import_chain.t.js index 70d8c26d..8efb6c39 100644 --- a/test/js/import_chain.t.js +++ b/test/js/import_chain.t.js @@ -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 index 6a9f3cc4..00000000 --- a/test/js/import_relative_path.t.js +++ /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"); diff --git a/test/shell_test.exp b/test/shell_test.exp index 336c3d3f..140db33f 100644 --- a/test/shell_test.exp +++ b/test/shell_test.exp @@ -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