Skip to content

Commit

Permalink
feat: prefer denort binary in target/ directory when available
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
irbull committed Nov 25, 2024
1 parent 12b3772 commit 57a4b18
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::env;
use std::env::current_exe;
use std::ffi::OsString;
use std::fs;
Expand All @@ -15,6 +16,7 @@ use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::ops::Range;
use std::path::Component;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -454,7 +456,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
//
// Phase 2 of the 'min sized' deno compile RFC talks
// about adding this as a flag.
if let Some(path) = std::env::var_os("DENORT_BIN") {
if let Some(path) = get_dev_binary_path() {
return std::fs::read(&path).with_context(|| {
format!("Could not find denort at '{}'", path.to_string_lossy())
});
Expand Down Expand Up @@ -904,6 +906,46 @@ impl<'a> DenoCompileBinaryWriter<'a> {
}
}

fn get_denort(deno_exe: PathBuf) -> Option<OsString> {
let mut denort = deno_exe;
denort.set_file_name("denort");
denort.exists().then(|| denort.into_os_string())
}

fn is_in_target(path: &Path) -> bool {
path
.components()
.any(|component| component == Component::Normal("target".as_ref()))
}

fn try_get_denort(path: &Path) -> Option<OsString> {
if is_in_target(path) {
get_denort(path.to_path_buf())
} else {
None
}
}

fn get_dev_binary_path() -> Option<OsString> {
env::var_os("DENORT_BIN").or_else(|| {
env::current_exe().ok().and_then(|exec_path| {
if is_in_target(&exec_path) {
try_get_denort(&exec_path)
} else if exec_path.is_symlink() {
fs::read_link(&exec_path).ok().and_then(|target_path| {
if is_in_target(&target_path) {
try_get_denort(&target_path)
} else {
None
}
})
} else {
None
}
})
})
}

/// This function returns the environment variables specified
/// in the passed environment file.
fn get_file_env_vars(
Expand Down

0 comments on commit 57a4b18

Please sign in to comment.