posit-dev / posit-dev/positron

Ark: Should we ensure subprocesses run the started version of R?

Open
#2,147 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: kernels lang: r
Dominant language
TypeScript
Stars
4.3k
Forks
183
Avg merge
1d 11h
Merged PRs (30d)
206

Description

(At first I thought yes, but I ended up convincing myself that the current behaviour is best.)

Positron has the ability to start a version of R that's not the default on the PATH (RStudio only offers that feature on Windows). However subprocesses created by calling back into R will still reach into the default R version reachable from PATH.

To reproduce, start a version of R that's not the default, for example with 4.1.2 running and 4.3.1 default:

system("R --version")
#> WARNING: ignoring environment value of R_HOME
#> R version 4.3.1 (2023-06-16) ...

I thought we could add $R_HOME/bin to PATH to ensure these calls hit the correct R version:

modified   crates/ark/src/interface.rs
@@ -179,6 +179,11 @@ pub fn start_r(
     let libraries = RLibraries::from_r_home_path(&r_home);
     libraries.initialize_pre_setup_r();
 
+    let r_home_path = r_home.display();
+    let path = std::env::var("PATH").unwrap();
+    let path = format!("{r_home_path}/bin:{path}");
+    std::env::set_var("PATH", path);
+
     crate::sys::interface::setup_r(args);
 
     libraries.initialize_post_setup_r();

But after more research I don't think it's needed because it's deviating from normal R behaviour. First note the warning about ignored R_HOME above. It's from the R wrapper script. See R.sh.in (with R_HOME_DIR hardcoded at build time):

if test -n "${R_HOME}" && \
   test "${R_HOME}" != "${R_HOME_DIR}"; then
  echo "WARNING: ignoring environment value of R_HOME"
fi

The wrapper script could start the R from R_HOME but chooses not to and warn instead.

Furthermore the R subprocess calls are careful to pick the R_HOME version of R. For instance in install.packages():

cmd0 <- file.path(R.home("bin"), "R")
args0 <- c("CMD", "INSTALL")

Same for callr which starts the correct R version:

callr::r(function() getRversion())
#> [1] ‘4.1.2’

Finally, rig doesn't do anything to correct that either. Even after running rig system make-orthogonal, changing the default R version while running R will cause new calls to system("R") to pick up the new default instead of the started version. (cc @gaborcsardi)

So on second thought I don't think we need to do better, it's preferable to stick with the regular behaviour to prevent surprises when running code in a different setup. WDY'allT?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read crates/ark/src/interface.rs and the referenced R wrapper script R.sh.in; compare the reproduction with R's R.home handling and the behavior of install.packages() and callr. Done means deciding and documenting whether Ark should alter PATH, including whether regular R behavior should be retained.

Written by the indexing model from the issue text.

Assessment

Tech stack
r, rust, shell
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.