1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

fix(npm): do not panic providing file url to require.resolve paths (#20182)

Closes #19922
This commit is contained in:
David Sherret 2023-08-17 10:39:06 -04:00 committed by Divy Srivastava
parent 32947e5ea5
commit 1b4c910075
5 changed files with 42 additions and 7 deletions

View file

@ -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/"),
});

View file

@ -0,0 +1,7 @@
{
"name": "@denotest/example",
"version": "1.0.0",
"dependencies": {
"@denotest/esm-basic": "*"
}
}

View file

@ -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

View file

@ -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);
}

View file

@ -95,13 +95,17 @@ where
{
let fs = state.borrow::<FileSystemRc>();
// 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::<P>(state, &from)?;