diff --git a/ext/node_resolver/resolution.rs b/ext/node_resolver/resolution.rs index 25316c385e..f4b2e8056f 100644 --- a/ext/node_resolver/resolution.rs +++ b/ext/node_resolver/resolution.rs @@ -1370,15 +1370,37 @@ impl NodeResolver { &self, file_path: &Path, ) -> Result, ClosestPkgJsonError> { + // we use this for deno compile using byonm because the script paths + // won't be in virtual file system, but the package.json paths will be + fn canonicalize_first_ancestor_exists( + dir_path: &Path, + env: &dyn NodeResolverEnv, + ) -> Result, std::io::Error> { + for ancestor in dir_path.ancestors() { + match env.realpath_sync(ancestor) { + Ok(dir_path) => return Ok(Some(dir_path)), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { + // keep searching + } + Err(err) => return Err(err), + } + } + Ok(None) + } + let parent_dir = file_path.parent().unwrap(); - let current_dir = - strip_unc_prefix(self.env.realpath_sync(parent_dir).map_err( - |source| CanonicalizingPkgJsonDirError { - dir_path: parent_dir.to_path_buf(), - source, - }, - )?); - for current_dir in current_dir.ancestors() { + let Some(start_dir) = canonicalize_first_ancestor_exists( + parent_dir, &self.env, + ) + .map_err(|source| CanonicalizingPkgJsonDirError { + dir_path: parent_dir.to_path_buf(), + source, + })? + else { + return Ok(None); + }; + let start_dir = strip_unc_prefix(start_dir); + for current_dir in start_dir.ancestors() { let package_json_path = current_dir.join("package.json"); if let Some(pkg_json) = self.load_package_json(&package_json_path)? { return Ok(Some(pkg_json)); diff --git a/tests/specs/compile/byonm_main_sub_dir/__test__.jsonc b/tests/specs/compile/byonm_main_sub_dir/__test__.jsonc new file mode 100644 index 0000000000..aa225ddd13 --- /dev/null +++ b/tests/specs/compile/byonm_main_sub_dir/__test__.jsonc @@ -0,0 +1,28 @@ +{ + "tempDir": true, + "envs": { + "DENO_FUTURE": "1" + }, + "steps": [{ + "args": "install", + "output": "[WILDCARD]" + }, { + "if": "unix", + "args": "compile --output main src/main.ts", + "output": "[WILDCARD]" + }, { + "if": "unix", + "commandName": "./main", + "args": [], + "output": "main.out" + }, { + "if": "windows", + "args": "compile --output main.exe src/main.ts", + "output": "[WILDCARD]" + }, { + "if": "windows", + "commandName": "./main.exe", + "args": [], + "output": "main.out" + }] +} diff --git a/tests/specs/compile/byonm_main_sub_dir/deno.json b/tests/specs/compile/byonm_main_sub_dir/deno.json new file mode 100644 index 0000000000..6134d86d1c --- /dev/null +++ b/tests/specs/compile/byonm_main_sub_dir/deno.json @@ -0,0 +1,3 @@ +{ + "unstable": ["byonm"] +} diff --git a/tests/specs/compile/byonm_main_sub_dir/main.out b/tests/specs/compile/byonm_main_sub_dir/main.out new file mode 100644 index 0000000000..00750edc07 --- /dev/null +++ b/tests/specs/compile/byonm_main_sub_dir/main.out @@ -0,0 +1 @@ +3 diff --git a/tests/specs/compile/byonm_main_sub_dir/package.json b/tests/specs/compile/byonm_main_sub_dir/package.json new file mode 100644 index 0000000000..af0075a93a --- /dev/null +++ b/tests/specs/compile/byonm_main_sub_dir/package.json @@ -0,0 +1,11 @@ +{ + "name": "package", + "version": "1.0.0", + "description": "", + "keywords": [], + "author": "", + "license": "MIT", + "dependencies": { + "@denotest/add": "*" + } +} diff --git a/tests/specs/compile/byonm_main_sub_dir/src/main.ts b/tests/specs/compile/byonm_main_sub_dir/src/main.ts new file mode 100644 index 0000000000..b75bbc03ed --- /dev/null +++ b/tests/specs/compile/byonm_main_sub_dir/src/main.ts @@ -0,0 +1,2 @@ +import { add } from "@denotest/add"; +console.log(add(1, 2));