Skip to content

Commit

Permalink
Merge pull request #1514 from rstudio/shell-expand-path-vars
Browse files Browse the repository at this point in the history
Expand `$USER` in `user_data_dir()` path.
  • Loading branch information
t-kalinowski authored Dec 12, 2023
2 parents 5d0a9a5 + c1838cc commit 9bd1ea2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ jobs:

- { os: ubuntu-latest, r: 'oldrel-1', python: '3.9' }
- { os: ubuntu-latest, r: 'oldrel-2', python: '3.9' }
- { os: ubuntu-20.04, r: '3.6', python: '3.8' }
- { os: ubuntu-20.04, r: 'oldrel-3', python: '3.8' }
# - { os: ubuntu-20.04, r: 'oldrel-4', python: '3.8' }
# https://api.r-hub.io/rversions/resolve/oldrel/4
# - { os: ubuntu-latest, r: 'devel' , python: '3.8', http-user-agent: 'release' }

- { os: ubuntu-latest, r: 'release', python: '3.7' }
Expand Down
4 changes: 2 additions & 2 deletions R/miniconda.R
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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", 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 9bd1ea2

Please sign in to comment.