From 1b4c910075e4d16c0904a27cf0d1cdb9e7ded4a5 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 17 Aug 2023 10:39:06 -0400 Subject: [PATCH] fix(npm): do not panic providing file url to require.resolve paths (#20182) Closes #19922 --- cli/tests/integration/npm_tests.rs | 10 ++++++++++ .../npm/require_resolve_url/package.json | 7 +++++++ .../npm/require_resolve_url/url_paths.out | 2 ++ .../npm/require_resolve_url/url_paths.ts | 12 ++++++++++++ ext/node/ops/require.rs | 18 +++++++++++------- 5 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 cli/tests/testdata/npm/require_resolve_url/package.json create mode 100644 cli/tests/testdata/npm/require_resolve_url/url_paths.out create mode 100644 cli/tests/testdata/npm/require_resolve_url/url_paths.ts diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index fb8d88dcac..f27befe547 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -2155,3 +2155,13 @@ itest!(check_package_file_dts_dmts_dcts { http_server: true, exit_code: 1, }); + +itest!(require_resolve_url_paths { + args: "run -A --quiet --node-modules-dir url_paths.ts", + output: "npm/require_resolve_url/url_paths.out", + envs: env_vars_for_npm_tests_no_sync_download(), + http_server: true, + exit_code: 0, + cwd: Some("npm/require_resolve_url/"), + copy_temp_dir: Some("npm/require_resolve_url/"), +}); diff --git a/cli/tests/testdata/npm/require_resolve_url/package.json b/cli/tests/testdata/npm/require_resolve_url/package.json new file mode 100644 index 0000000000..814c77a5bf --- /dev/null +++ b/cli/tests/testdata/npm/require_resolve_url/package.json @@ -0,0 +1,7 @@ +{ + "name": "@denotest/example", + "version": "1.0.0", + "dependencies": { + "@denotest/esm-basic": "*" + } +} diff --git a/cli/tests/testdata/npm/require_resolve_url/url_paths.out b/cli/tests/testdata/npm/require_resolve_url/url_paths.out new file mode 100644 index 0000000000..404d7dcbac --- /dev/null +++ b/cli/tests/testdata/npm/require_resolve_url/url_paths.out @@ -0,0 +1,2 @@ +file:///[WILDCARD]/npm/require_resolve_url/ +[WILDCARD]require_resolve_url[WILDCARD]node_modules[WILDCARD].deno[WILDCARD]@denotest+esm-basic@1.0.0[WILDCARD]node_modules[WILDCARD]@denotest[WILDCARD]esm-basic[WILDCARD]main.mjs diff --git a/cli/tests/testdata/npm/require_resolve_url/url_paths.ts b/cli/tests/testdata/npm/require_resolve_url/url_paths.ts new file mode 100644 index 0000000000..7826568fe7 --- /dev/null +++ b/cli/tests/testdata/npm/require_resolve_url/url_paths.ts @@ -0,0 +1,12 @@ +import { createRequire } from "node:module"; +const require = createRequire(import.meta.url); + +console.log(getParentUrl()); +console.log(require.resolve("@denotest/esm-basic", { + paths: [getParentUrl()], +})); + +function getParentUrl() { + const fileUrl = import.meta.url; + return fileUrl.substring(0, fileUrl.lastIndexOf("/") + 1); +} diff --git a/ext/node/ops/require.rs b/ext/node/ops/require.rs index f91970eefe..3b77ff571a 100644 --- a/ext/node/ops/require.rs +++ b/ext/node/ops/require.rs @@ -95,13 +95,17 @@ where { let fs = state.borrow::(); // Guarantee that "from" is absolute. - let from = deno_core::resolve_path( - &from, - &(fs.cwd().map_err(AnyError::from)).context("Unable to get CWD")?, - ) - .unwrap() - .to_file_path() - .unwrap(); + let from = if from.starts_with("file:///") { + Url::parse(&from)?.to_file_path().unwrap() + } else { + deno_core::resolve_path( + &from, + &(fs.cwd().map_err(AnyError::from)).context("Unable to get CWD")?, + ) + .unwrap() + .to_file_path() + .unwrap() + }; ensure_read_permission::

(state, &from)?;