summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-11-06 00:58:21 +0100
committerGitHub <noreply@github.com>2022-11-06 00:58:21 +0100
commite7d7da85dff177bbc919cab48643135c68e065bd (patch)
treeee6ddb2abe715b80ad0334750e262ef7145f7975
parent384e53308a7d7e5d503e9cbf9950ab428048ac4c (diff)
fix(npm): fix CJS resolution with local node_modules dir (#16547)
This commit fixes CJS resolution when there's a local "node_modules/" directory. Before this commit relative imports from CJS files where resolved relative to root directory of the package instead of relative to referrer file.
-rw-r--r--cli/tests/integration/npm_tests.rs8
-rw-r--r--cli/tests/testdata/npm/cjs_yargs/main.js20
-rw-r--r--cli/tests/testdata/npm/cjs_yargs/main.out54
-rw-r--r--ext/node/02_require.js9
4 files changed, 91 insertions, 0 deletions
diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs
index 72af72a76..5dae2fd1c 100644
--- a/cli/tests/integration/npm_tests.rs
+++ b/cli/tests/integration/npm_tests.rs
@@ -641,6 +641,14 @@ itest!(node_modules_dir_with_deps {
temp_cwd: true,
});
+itest!(node_modules_dir_yargs {
+ args: "run --allow-read --allow-env --unstable --node-modules-dir $TESTDATA/npm/cjs_yargs/main.js",
+ output: "npm/cjs_yargs/main.out",
+ envs: env_vars(),
+ http_server: true,
+ temp_cwd: true,
+});
+
#[test]
fn node_modules_dir_cache() {
let _server = http_server();
diff --git a/cli/tests/testdata/npm/cjs_yargs/main.js b/cli/tests/testdata/npm/cjs_yargs/main.js
new file mode 100644
index 000000000..832fd053c
--- /dev/null
+++ b/cli/tests/testdata/npm/cjs_yargs/main.js
@@ -0,0 +1,20 @@
+import yargs from "npm:yargs@15.4.1";
+
+const args = yargs(["serve", "8000"])
+ .command("serve [port]", "start the server", (yargs) => {
+ return yargs
+ .positional("port", {
+ describe: "port to bind on",
+ default: 5000,
+ });
+ }, (argv) => {
+ console.info(`start server on :${argv.port}`);
+ })
+ .option("verbose", {
+ alias: "v",
+ type: "boolean",
+ description: "Run with verbose logging",
+ })
+ .argv;
+
+console.log(args);
diff --git a/cli/tests/testdata/npm/cjs_yargs/main.out b/cli/tests/testdata/npm/cjs_yargs/main.out
new file mode 100644
index 000000000..ded92a4da
--- /dev/null
+++ b/cli/tests/testdata/npm/cjs_yargs/main.out
@@ -0,0 +1,54 @@
+Download http://localhost:4545/npm/registry/yargs
+Download http://localhost:4545/npm/registry/cliui
+Download http://localhost:4545/npm/registry/decamelize
+Download http://localhost:4545/npm/registry/find-up
+Download http://localhost:4545/npm/registry/get-caller-file
+Download http://localhost:4545/npm/registry/require-directory
+Download http://localhost:4545/npm/registry/require-main-filename
+Download http://localhost:4545/npm/registry/set-blocking
+Download http://localhost:4545/npm/registry/string-width
+Download http://localhost:4545/npm/registry/which-module
+Download http://localhost:4545/npm/registry/y18n
+Download http://localhost:4545/npm/registry/yargs-parser
+Download http://localhost:4545/npm/registry/strip-ansi
+Download http://localhost:4545/npm/registry/wrap-ansi
+Download http://localhost:4545/npm/registry/locate-path
+Download http://localhost:4545/npm/registry/path-exists
+Download http://localhost:4545/npm/registry/emoji-regex
+Download http://localhost:4545/npm/registry/is-fullwidth-code-point
+Download http://localhost:4545/npm/registry/camelcase
+Download http://localhost:4545/npm/registry/ansi-regex
+Download http://localhost:4545/npm/registry/ansi-styles
+Download http://localhost:4545/npm/registry/p-locate
+Download http://localhost:4545/npm/registry/color-convert
+Download http://localhost:4545/npm/registry/p-limit
+Download http://localhost:4545/npm/registry/color-name
+Download http://localhost:4545/npm/registry/p-try
+Download http://localhost:4545/npm/registry/ansi-regex/ansi-regex-5.0.1.tgz
+Download http://localhost:4545/npm/registry/ansi-styles/ansi-styles-4.3.0.tgz
+Download http://localhost:4545/npm/registry/camelcase/camelcase-5.3.1.tgz
+Download http://localhost:4545/npm/registry/cliui/cliui-6.0.0.tgz
+Download http://localhost:4545/npm/registry/color-convert/color-convert-2.0.1.tgz
+Download http://localhost:4545/npm/registry/color-name/color-name-1.1.4.tgz
+Download http://localhost:4545/npm/registry/decamelize/decamelize-1.2.0.tgz
+Download http://localhost:4545/npm/registry/emoji-regex/emoji-regex-8.0.0.tgz
+Download http://localhost:4545/npm/registry/find-up/find-up-4.1.0.tgz
+Download http://localhost:4545/npm/registry/get-caller-file/get-caller-file-2.0.5.tgz
+Download http://localhost:4545/npm/registry/is-fullwidth-code-point/is-fullwidth-code-point-3.0.0.tgz
+Download http://localhost:4545/npm/registry/locate-path/locate-path-5.0.0.tgz
+Download http://localhost:4545/npm/registry/p-limit/p-limit-2.3.0.tgz
+Download http://localhost:4545/npm/registry/p-locate/p-locate-4.1.0.tgz
+Download http://localhost:4545/npm/registry/p-try/p-try-2.2.0.tgz
+Download http://localhost:4545/npm/registry/path-exists/path-exists-4.0.0.tgz
+Download http://localhost:4545/npm/registry/require-directory/require-directory-2.1.1.tgz
+Download http://localhost:4545/npm/registry/require-main-filename/require-main-filename-2.0.0.tgz
+Download http://localhost:4545/npm/registry/set-blocking/set-blocking-2.0.0.tgz
+Download http://localhost:4545/npm/registry/string-width/string-width-4.2.3.tgz
+Download http://localhost:4545/npm/registry/strip-ansi/strip-ansi-6.0.1.tgz
+Download http://localhost:4545/npm/registry/which-module/which-module-2.0.0.tgz
+Download http://localhost:4545/npm/registry/wrap-ansi/wrap-ansi-6.2.0.tgz
+Download http://localhost:4545/npm/registry/y18n/y18n-4.0.3.tgz
+Download http://localhost:4545/npm/registry/yargs/yargs-15.4.1.tgz
+Download http://localhost:4545/npm/registry/yargs-parser/yargs-parser-18.1.3.tgz
+start server on :8000
+[WILDCARD]
diff --git a/ext/node/02_require.js b/ext/node/02_require.js
index b699db00b..ac34a5fca 100644
--- a/ext/node/02_require.js
+++ b/ext/node/02_require.js
@@ -401,6 +401,15 @@
Module._resolveLookupPaths = function (request, parent) {
const paths = [];
+
+ if (core.ops.op_require_is_request_relative(request) && parent?.filename) {
+ ArrayPrototypePush(
+ paths,
+ core.ops.op_require_path_dirname(parent.filename),
+ );
+ return paths;
+ }
+
if (parent?.filename && parent.filename.length > 0) {
const denoDirPath = core.ops.op_require_resolve_deno_dir(
request,