Skip to content

Commit 57a4b18

Browse files
committed
feat: prefer denort binary in target/ directory when available
Enhances the deno compile workflow for denort development by automatically checking for a denort binary in the same directory as the deno binary, provided both are located within a target/ directory. If found, this denort binary will be used. Key points: - The DENORT_BIN environment variable remains supported for explicitly specifying a custom denort binary path. - Includes additional logic to verify if the deno binary is a symlink pointing to an executable in the target/ directory.
1 parent 12b3772 commit 57a4b18

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

cli/standalone/binary.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::borrow::Cow;
44
use std::collections::BTreeMap;
55
use std::collections::HashMap;
66
use std::collections::VecDeque;
7+
use std::env;
78
use std::env::current_exe;
89
use std::ffi::OsString;
910
use std::fs;
@@ -15,6 +16,7 @@ use std::io::Seek;
1516
use std::io::SeekFrom;
1617
use std::io::Write;
1718
use std::ops::Range;
19+
use std::path::Component;
1820
use std::path::Path;
1921
use std::path::PathBuf;
2022
use std::process::Command;
@@ -454,7 +456,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
454456
//
455457
// Phase 2 of the 'min sized' deno compile RFC talks
456458
// about adding this as a flag.
457-
if let Some(path) = std::env::var_os("DENORT_BIN") {
459+
if let Some(path) = get_dev_binary_path() {
458460
return std::fs::read(&path).with_context(|| {
459461
format!("Could not find denort at '{}'", path.to_string_lossy())
460462
});
@@ -904,6 +906,46 @@ impl<'a> DenoCompileBinaryWriter<'a> {
904906
}
905907
}
906908

909+
fn get_denort(deno_exe: PathBuf) -> Option<OsString> {
910+
let mut denort = deno_exe;
911+
denort.set_file_name("denort");
912+
denort.exists().then(|| denort.into_os_string())
913+
}
914+
915+
fn is_in_target(path: &Path) -> bool {
916+
path
917+
.components()
918+
.any(|component| component == Component::Normal("target".as_ref()))
919+
}
920+
921+
fn try_get_denort(path: &Path) -> Option<OsString> {
922+
if is_in_target(path) {
923+
get_denort(path.to_path_buf())
924+
} else {
925+
None
926+
}
927+
}
928+
929+
fn get_dev_binary_path() -> Option<OsString> {
930+
env::var_os("DENORT_BIN").or_else(|| {
931+
env::current_exe().ok().and_then(|exec_path| {
932+
if is_in_target(&exec_path) {
933+
try_get_denort(&exec_path)
934+
} else if exec_path.is_symlink() {
935+
fs::read_link(&exec_path).ok().and_then(|target_path| {
936+
if is_in_target(&target_path) {
937+
try_get_denort(&target_path)
938+
} else {
939+
None
940+
}
941+
})
942+
} else {
943+
None
944+
}
945+
})
946+
})
947+
}
948+
907949
/// This function returns the environment variables specified
908950
/// in the passed environment file.
909951
fn get_file_env_vars(

0 commit comments

Comments
 (0)