| 1 |
|
| 2 |
the$default_libpaths <- new.env(parent = emptyenv()) |
| 3 |
|
| 4 |
# NOTE: if sandboxing is used then these symbols will be clobbered; |
| 5 |
# save them so we can properly restore them later if so required |
| 6 |
renv_libpaths_init <- function() { |
| 7 |
assign(".libPaths()", .libPaths(), envir = the$default_libpaths) |
| 8 |
assign(".Library", .Library, envir = the$default_libpaths) |
| 9 |
assign(".Library.site", .Library.site, envir = the$default_libpaths) |
| 10 |
} |
| 11 |
|
| 12 |
renv_libpaths_active <- function() { |
| 13 |
.libPaths()[[1L]] |
| 14 |
} |
| 15 |
|
| 16 |
renv_libpaths_all <- function() { |
| 17 |
.libPaths() |
| 18 |
} |
| 19 |
|
| 20 |
renv_libpaths_system <- function() { |
| 21 |
get(".Library", envir = the$default_libpaths) |
| 22 |
} |
| 23 |
|
| 24 |
renv_libpaths_site <- function() { |
| 25 |
get(".Library.site", envir = the$default_libpaths) |
| 26 |
} |
| 27 |
|
| 28 |
renv_libpaths_external <- function(project) { |
| 29 |
projlib <- settings$external.libraries(project = project) |
| 30 |
conflib <- config$external.libraries(project) |
| 31 |
.expand_R_libs_env_var(c(projlib, conflib)) |
| 32 |
} |
| 33 |
|
| 34 |
# on Windows, attempting to use a library path containing |
| 35 |
# characters considered special by cmd.exe will fail. |
| 36 |
# to guard against this, we try to create a junction point |
| 37 |
# from the temporary directory to the target library path |
| 38 |
# |
| 39 |
# https://github.com/rstudio/renv/issues/334 |
| 40 |
renv_libpaths_safe <- function(libpaths) { |
| 41 |
|
| 42 |
if (renv_libpaths_safe_check(libpaths)) |
| 43 |
return(libpaths) |
| 44 |
|
| 45 |
map_chr(libpaths, renv_libpaths_safe_impl) |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
renv_libpaths_safe_check <- function(libpaths) { |
| 50 |
|
| 51 |
# if any of the paths have single quotes, |
| 52 |
# then we need to use a safe path |
| 53 |
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17973 |
| 54 |
if (any(grepl("'", libpaths, fixed = TRUE))) |
| 55 |
return(FALSE) |
| 56 |
|
| 57 |
# on Windows, we need to use safe library paths for R < 4.0.0 |
| 58 |
# https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17709 |
| 59 |
if (renv_platform_windows() && getRversion() < "4.0.0") |
| 60 |
return(FALSE) |
| 61 |
|
| 62 |
# otherwise, we're okay |
| 63 |
return(TRUE) |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
renv_libpaths_safe_impl <- function(libpath) { |
| 68 |
|
| 69 |
# check for an unsafe library path |
| 70 |
unsafe <- |
| 71 |
Encoding(libpath) == "UTF-8" || |
| 72 |
grepl("[&<>^|'\"]", libpath) |
| 73 |
|
| 74 |
# if the path appears safe, use it as-is |
| 75 |
if (!unsafe) |
| 76 |
return(libpath) |
| 77 |
|
| 78 |
# try to form a safe library path |
| 79 |
methods <- c( |
| 80 |
renv_libpaths_safe_tempdir, |
| 81 |
renv_libpaths_safe_userlib |
| 82 |
) |
| 83 |
|
| 84 |
for (method in methods) { |
| 85 |
safelib <- catchall(method(libpath)) |
| 86 |
if (is.character(safelib)) |
| 87 |
return(safelib) |
| 88 |
} |
| 89 |
|
| 90 |
# could not form a safe library path; |
| 91 |
# just use the existing library path as-is |
| 92 |
libpath |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
renv_libpaths_safe_tempdir <- function(libpath) { |
| 97 |
safelib <- tempfile("renv-safelib-") |
| 98 |
|
| 99 |
if (renv_platform_windows()) |
| 100 |
renv_file_junction(libpath, safelib) |
| 101 |
else |
| 102 |
file.symlink(libpath, safelib) |
| 103 |
|
| 104 |
safelib |
| 105 |
} |
| 106 |
|
| 107 |
renv_libpaths_safe_userlib <- function(libpath) { |
| 108 |
|
| 109 |
# form path into user library |
| 110 |
userlib <- renv_libpaths_user()[[1]] |
| 111 |
base <- file.path(userlib, ".renv-links") |
| 112 |
ensure_directory(base) |
| 113 |
|
| 114 |
# create name for actual junction point |
| 115 |
name <- renv_hash_text(libpath) |
| 116 |
safelib <- file.path(base, name) |
| 117 |
|
| 118 |
# if the junction already exists, use it |
| 119 |
if (renv_file_same(libpath, safelib)) |
| 120 |
return(safelib) |
| 121 |
|
| 122 |
# otherwise, try to create it. note that junction |
| 123 |
# points can be removed with a non-recursive unlink |
| 124 |
unlink(safelib) |
| 125 |
|
| 126 |
if (renv_platform_windows()) |
| 127 |
renv_file_junction(libpath, safelib) |
| 128 |
else |
| 129 |
file.symlink(libpath, safelib) |
| 130 |
|
| 131 |
safelib |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
renv_libpaths_set <- function(libpaths) { |
| 136 |
oldlibpaths <- .libPaths() |
| 137 |
safepaths <- renv_libpaths_safe(libpaths) |
| 138 |
.libPaths(safepaths) |
| 139 |
oldlibpaths |
| 140 |
} |
| 141 |
|
| 142 |
renv_libpaths_default <- function() { |
| 143 |
the$default_libpaths$`.libPaths()` |
| 144 |
} |
| 145 |
|
| 146 |
# NOTE: may return more than one library path! |
| 147 |
renv_libpaths_user <- function() { |
| 148 |
|
| 149 |
# if renv is active, the user library will be saved |
| 150 |
envvars <- c("RENV_DEFAULT_R_LIBS_USER", "R_LIBS_USER") |
| 151 |
for (envvar in envvars) { |
| 152 |
|
| 153 |
value <- Sys.getenv(envvar, unset = NA) |
| 154 |
if (is.na(value) || value %in% c("", "<NA>", "NULL")) |
| 155 |
next |
| 156 |
|
| 157 |
parts <- strsplit(value, .Platform$path.sep, fixed = TRUE)[[1L]] |
| 158 |
return(parts) |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
# otherwise, default to active library |
| 163 |
# (shouldn't happen but best be safe) |
| 164 |
renv_libpaths_active() |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
renv_init_libpaths <- function(project) { |
| 169 |
|
| 170 |
projlib <- renv_paths_library(project = project) |
| 171 |
extlib <- renv_libpaths_external(project = project) |
| 172 |
userlib <- if (config$user.library()) |
| 173 |
renv_libpaths_user() |
| 174 |
|
| 175 |
libpaths <- c(projlib, extlib, userlib) |
| 176 |
lapply(libpaths, ensure_directory) |
| 177 |
|
| 178 |
libpaths |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
renv_libpaths_restore <- function() { |
| 183 |
libpaths <- get(".libPaths()", envir = the$default_libpaths) |
| 184 |
renv_libpaths_set(libpaths) |
| 185 |
} |
| 186 |
|
| 187 |
# We need to ensure the system library is included, for cases where users have |
| 188 |
# provided an explicit 'library' argument in calls to functions like |
| 189 |
# 'renv::restore(library = <...>)') |
| 190 |
# |
| 191 |
# https://github.com/rstudio/renv/issues/1544 |
| 192 |
renv_libpaths_resolve <- function(library = NULL) { |
| 193 |
|
| 194 |
if (is.null(library)) |
| 195 |
return(renv_libpaths_all()) |
| 196 |
|
| 197 |
unique(c(library, .Library)) |
| 198 |
|
| 199 |
} |