Skip to content

Commit

Permalink
expand $USER in user_data_dir() path.
Browse files Browse the repository at this point in the history
closes #1513
  • Loading branch information
t-kalinowski committed Dec 12, 2023
1 parent 5d0a9a5 commit cfae331
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions R/miniconda.R
Expand Up @@ -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")

}
Expand Down Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion 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")
Expand Down
42 changes: 39 additions & 3 deletions R/utils.R
Expand Up @@ -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

0 comments on commit cfae331

Please sign in to comment.