From cfae3316ae3b1757431e3b139a95d0ad6006554e Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Tue, 12 Dec 2023 16:26:40 -0500 Subject: [PATCH] expand $USER in `user_data_dir()` path. closes #1513 --- R/miniconda.R | 4 ++-- R/pyenv.R | 2 +- R/utils.R | 42 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/R/miniconda.R b/R/miniconda.R index 051b2deac..fcb429f9a 100644 --- a/R/miniconda.R +++ b/R/miniconda.R @@ -283,7 +283,7 @@ miniconda_path_default <- function() { } # otherwise, use rappdirs default - root <- normalizePath(rappdirs::user_data_dir(), winslash = "/", mustWork = FALSE) + root <- normalizePath(user_data_dir(), winslash = "/", mustWork = FALSE) file.path(root, "r-miniconda") } @@ -314,7 +314,7 @@ miniconda_envpath <- function(env = NULL, path = miniconda_path()) { } miniconda_meta_path <- function() { - root <- rappdirs::user_data_dir("r-reticulate") + root <- user_data_dir("r-reticulate") file.path(root, "miniconda.json") } diff --git a/R/pyenv.R b/R/pyenv.R index 2cb78febc..42bdb1da1 100644 --- a/R/pyenv.R +++ b/R/pyenv.R @@ -1,6 +1,6 @@ pyenv_root <- function() { - root <- rappdirs::user_data_dir("r-reticulate") + root <- user_data_dir("r-reticulate") dir.create(root, showWarnings = FALSE, recursive = TRUE) norm <- normalizePath(root, winslash = "/", mustWork = TRUE) file.path(norm, "pyenv") diff --git a/R/utils.R b/R/utils.R index a8118b0af..5c5bca471 100644 --- a/R/utils.R +++ b/R/utils.R @@ -640,7 +640,43 @@ maybe_shQuote <- function(x) { rm_all_reticulate_state <- function() { - unlink(rappdirs::user_data_dir("r-reticulate", NULL), recursive = TRUE, force = TRUE) - unlink(rappdirs::user_data_dir("r-miniconda", NULL), recursive = TRUE, force = TRUE) - unlink(rappdirs::user_data_dir("r-miniconda-arm64", NULL), recursive = TRUE, force = TRUE) + unlink(user_data_dir("r-reticulate", NULL), recursive = TRUE, force = TRUE) + unlink(user_data_dir("r-miniconda", NULL), recursive = TRUE, force = TRUE) + unlink(user_data_dir("r-miniconda-arm64", NULL), recursive = TRUE, force = TRUE) + unlink(miniconda_path_default(), recursive = TRUE, force = TRUE) } + + +user_data_dir <- function(...) { + expand_env_vars(rappdirs::user_data_dir(...)) +} + +expand_env_vars <- function(x) { + # We need to expand some env vars here, until + # Rstudio server is patched. + # https://github.com/rstudio/rstudio-pro/issues/2968 + # The core issue is RStudio Server shell expands some env vars, but + # doesn't propogate those expanded env vars to the user R sessions + # e.g., https://docs.posit.co/ide/server-pro/1.4.1722-1/server-management.html#setting-environment-variables + # suggests adminst set XDG_DATA_HOME=/mnt/storage/$USER + # that is correctly expanded by rstudio server here: + # https://github.com/rstudio/rstudio/blob/55c42e8d9c0df19a6566000f550a0fa6dc519899/src/cpp/core/system/Xdg.cpp#L160-L178 + # but then not propogated to the user R session. + # https://github.com/rstudio/reticulate/issues/1513 + + if(!grepl("$", x, fixed = TRUE)) + return(x) + delayedAssign("info", as.list(Sys.info())) + delayedAssign("HOME", Sys.getenv("HOME") %""% path.expand("~")) + delayedAssign("USER", Sys.getenv("USER") %""% info[["user"]]) + delayedAssign("HOSTNAME", Sys.getenv("HOSTNAME") %""% info[["nodename"]]) + for (name in c("HOME", "USER", "HOSTNAME")) { + if (grepl(name, x, fixed = TRUE)) { + x <- gsub(sprintf("$%s", name), get(name), x, fixed = TRUE) + x <- gsub(sprintf("${%s}", name), get(name), x, fixed = TRUE) + } + } + x +} + +`%""%` <- function(x, y) if(identical(x, "")) y else x