1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -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 GitHub
parent 23ff0e722e
commit 4535f79a4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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, http_server: true,
exit_code: 1, 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>(); let fs = state.borrow::<FileSystemRc>();
// Guarantee that "from" is absolute. // Guarantee that "from" is absolute.
let from = deno_core::resolve_path( let from = if from.starts_with("file:///") {
&from, Url::parse(&from)?.to_file_path().unwrap()
&(fs.cwd().map_err(AnyError::from)).context("Unable to get CWD")?, } else {
) deno_core::resolve_path(
.unwrap() &from,
.to_file_path() &(fs.cwd().map_err(AnyError::from)).context("Unable to get CWD")?,
.unwrap(); )
.unwrap()
.to_file_path()
.unwrap()
};
ensure_read_permission::<P>(state, &from)?; ensure_read_permission::<P>(state, &from)?;