Skip to content

Commit

Permalink
fix(unstable/compile): handle byonm import in sub dir (#24755)
Browse files Browse the repository at this point in the history
Regression in 1.45.0 caused by storing relative paths instead of
absolute paths in the binary.

Closes #24654
  • Loading branch information
dsherret authored Jul 26, 2024
1 parent ed2bf8c commit 06b6352
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
38 changes: 30 additions & 8 deletions ext/node_resolver/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1370,15 +1370,37 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> {
&self,
file_path: &Path,
) -> Result<Option<PackageJsonRc>, 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<Option<PathBuf>, 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));
Expand Down
28 changes: 28 additions & 0 deletions tests/specs/compile/byonm_main_sub_dir/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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"
}]
}
3 changes: 3 additions & 0 deletions tests/specs/compile/byonm_main_sub_dir/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"unstable": ["byonm"]
}
1 change: 1 addition & 0 deletions tests/specs/compile/byonm_main_sub_dir/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3
11 changes: 11 additions & 0 deletions tests/specs/compile/byonm_main_sub_dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "package",
"version": "1.0.0",
"description": "",
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@denotest/add": "*"
}
}
2 changes: 2 additions & 0 deletions tests/specs/compile/byonm_main_sub_dir/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { add } from "@denotest/add";
console.log(add(1, 2));

0 comments on commit 06b6352

Please sign in to comment.