Skip to content
Prev 62140 / 63421 Next

Sys.which() caching path to `which`

On Friday, January 12th, 2024 at 16:11, Ivan Krylov <ikrylov at disroot.org> wrote:

            
Then can we revisit my simple fix, which refers to `which` through a
symlink instead of a hard-coded absolute in an R-source file:
From: Harmen Stoppels <me at harmenstoppels.nl>
Date: Wed, 10 Jan 2024 12:40:40 +0100
Subject: [PATCH] base: use a symlink for which instead of hard-coded string

---
 share/make/basepkg.mk                 | 8 ++++----
 src/library/base/R/unix/system.unix.R | 6 +++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/share/make/basepkg.mk b/share/make/basepkg.mk
index c0a69c8a0af..4cf63878709 100644
--- a/share/make/basepkg.mk
+++ b/share/make/basepkg.mk
@@ -72,16 +72,16 @@ mkRbase:
 	  else \
 	    cat $(RSRC) > "$${f}"; \
 	  fi; \
-	  f2=$${TMPDIR:-/tmp}/R2$$$$; \
-	  sed -e "s:@WHICH@:${WHICH}:" "$${f}" > "$${f2}"; \
-	  rm -f "$${f}"; \
-	  $(SHELL) $(top_srcdir)/tools/move-if-change "$${f2}" all.R)
+	  $(SHELL) $(top_srcdir)/tools/move-if-change "$${f}" all.R)
 	@if ! test -f $(top_builddir)/library/$(pkg)/R/$(pkg); then \
 	  $(INSTALL_DATA) all.R $(top_builddir)/library/$(pkg)/R/$(pkg); \
 	else if test all.R -nt $(top_builddir)/library/$(pkg)/R/$(pkg); then \
 	  $(INSTALL_DATA) all.R $(top_builddir)/library/$(pkg)/R/$(pkg); \
 	  fi \
 	fi
+	@if ! test -f $(top_builddir)/library/$(pkg)/R/which; then \
+	  cd $(top_builddir)/library/$(pkg)/R/ && $(LN_S) $(WHICH) which; \
+	fi
 
 mkdesc:
 	@if test -f DESCRIPTION; then \
diff --git a/src/library/base/R/unix/system.unix.R b/src/library/base/R/unix/system.unix.R
index 3bb7d0cb27c..78271c8c12c 100644
--- a/src/library/base/R/unix/system.unix.R
+++ b/src/library/base/R/unix/system.unix.R
@@ -114,9 +114,9 @@ system2 <- function(command, args = character(),
 Sys.which <- function(names)
 {
     res <- character(length(names)); names(res) <- names
-    ## hopefully configure found [/usr]/bin/which
-    which <- "@WHICH@"
-    if (!nzchar(which)) {
+    which <- file.path(R.home(), "library", "base", "R", "which")
+    ## which should be a symlink to the system's which
+    if (!file.exists(which)) {
         warning("'which' was not found on this platform")
         return(res)
     }
Message-ID: <HrQEEygLp_tv6eb1kK4Y7cBF2pUsbZCg1N1dUAgP3nER1bg5ebNG-I4p1ZjojRj-a-0rozKtNHHKQqvdJUm5pPfhPEnTYXhQXf4Jyp9VUK8=@harmenstoppels.nl>
In-Reply-To: <20240112181125.7616fc6e@arachnoid>