Skip to content

Commit

Permalink
Move r_home_setup() to harp
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Feb 24, 2025
1 parent a9008ac commit 3997849
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion crates/ark/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use crossbeam::channel::Receiver;
use crossbeam::channel::Sender;
use crossbeam::select;
use harp::command::r_command;
use harp::command::r_home_setup;
use harp::environment::r_ns_env;
use harp::environment::Environment;
use harp::environment::R_ENVS;
Expand Down Expand Up @@ -123,7 +124,6 @@ use crate::strings::lines;
use crate::sys::console::console_to_utf8;
use crate::ui::UiCommMessage;
use crate::ui::UiCommSender;
use crate::version::r_home_setup;

static RE_DEBUG_PROMPT: Lazy<Regex> = Lazy::new(|| Regex::new(r"Browse\[\d+\]").unwrap());

Expand Down
27 changes: 1 addition & 26 deletions crates/ark/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::PathBuf;

use anyhow::Context;
use harp::command::r_command;
use harp::command::r_command_from_path;
use harp::command::r_home_setup;
use harp::object::RObject;
use itertools::Itertools;
use libr::SEXP;
Expand All @@ -31,31 +31,6 @@ pub struct RVersion {
pub r_home: String,
}

pub(crate) fn r_home_setup() -> PathBuf {
match std::env::var("R_HOME") {
Ok(home) => {
// Get `R_HOME` from env var, typically set by Positron / CI / kernel specification
PathBuf::from(home)
},
Err(_) => {
// Get `R_HOME` from `PATH`, via `R`
let Ok(result) = r_command_from_path(|command| {
command.arg("RHOME");
}) else {
panic!("Can't find R or `R_HOME`");
};

let r_home = String::from_utf8(result.stdout).unwrap();
let r_home = r_home.trim();

// Now set `R_HOME`. From now on, `r_command()` can be used to
// run exactly the same R as is running in Ark.
unsafe { std::env::set_var("R_HOME", r_home) };
PathBuf::from(r_home)
},
}
}

pub fn detect_r() -> anyhow::Result<RVersion> {
let r_home: String = r_home_setup().to_string_lossy().to_string();

Expand Down
29 changes: 28 additions & 1 deletion crates/harp/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::sys::command::COMMAND_R_NAMES;
/// - For Windows, this looks at `R` (`R.exe`) and `R.bat` (for rig compatibility)
///
/// The executable name is joined to the path in `R_HOME`. If not set, this is a
/// panic. Use `r_command_from_path()` to exectute R from `PATH` instead.
/// panic. Use `r_home_setup()` to set `R_HOME` from the R on the `PATH` or use
/// `r_command_from_path()` to exectute R from `PATH` directly.
///
/// Returns the `Ok()` value of the first success, or the `Err()` value of the
/// last failure if all locations fail.
Expand All @@ -39,6 +40,32 @@ where
r_command_from_locs(locations, build)
}

/// Use this before calling `r_command()` to ensure that `R_HOME` is set consistently
pub fn r_home_setup() -> PathBuf {
match std::env::var("R_HOME") {
Ok(home) => {
// Get `R_HOME` from env var, typically set by Positron / CI / kernel specification
PathBuf::from(home)
},
Err(_) => {
// Get `R_HOME` from `PATH`, via `R`
let Ok(result) = r_command_from_path(|command| {
command.arg("RHOME");
}) else {
panic!("Can't find R or `R_HOME`");
};

let r_home = String::from_utf8(result.stdout).unwrap();
let r_home = r_home.trim();

// Now set `R_HOME`. From now on, `r_command()` can be used to
// run exactly the same R as is running in Ark.
unsafe { std::env::set_var("R_HOME", r_home) };
PathBuf::from(r_home)
},
}
}

/// Execute a `Command` for R found on the `PATH`
///
/// This is like `r_command()` but doesn't assume `R_HOME` is defined.
Expand Down

0 comments on commit 3997849

Please sign in to comment.