| 1 |
|
| 2 |
the$root <- NULL |
| 3 |
|
| 4 |
renv_paths_override <- function(name) { |
| 5 |
|
| 6 |
# # check for value from option |
| 7 |
# optname <- paste("renv.paths", name, sep = ".") |
| 8 |
# optval <- getOption(optname) |
| 9 |
# if (!is.null(optval)) |
| 10 |
# return(optval) |
| 11 |
|
| 12 |
# check for value from envvar |
| 13 |
envname <- paste("RENV_PATHS", toupper(name), sep = "_") |
| 14 |
envval <- Sys.getenv(envname, unset = NA) |
| 15 |
if (!is.na(envval)) |
| 16 |
return(envval) |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
renv_paths_common <- function(name, prefixes = NULL, ...) { |
| 21 |
|
| 22 |
# check for single absolute path supplied by user |
| 23 |
# TODO: handle multiple? |
| 24 |
end <- file.path(...) |
| 25 |
if (length(end) == 1 && renv_path_absolute(end)) |
| 26 |
return(end) |
| 27 |
|
| 28 |
# check for path provided via option |
| 29 |
root <- renv_paths_override(name) %||% renv_paths_root(name) |
| 30 |
|
| 31 |
# split path entries containing a separator |
| 32 |
if (name %in% c("cache", "local", "cellar")) { |
| 33 |
pattern <- if (renv_platform_windows()) "[;]" else "[;:]" |
| 34 |
root <- strsplit(root, pattern)[[1L]] |
| 35 |
} |
| 36 |
|
| 37 |
# form rest of path |
| 38 |
prefixed <- if (length(prefixes)) |
| 39 |
file.path(root, paste(prefixes, collapse = "/")) |
| 40 |
else |
| 41 |
root |
| 42 |
|
| 43 |
path <- file.path(prefixed, ...) |
| 44 |
if (length(path)) path else "" |
| 45 |
} |
| 46 |
|
| 47 |
renv_paths_library_root <- function(project) { |
| 48 |
renv_bootstrap_library_root(project) |
| 49 |
} |
| 50 |
|
| 51 |
renv_paths_library <- function(..., project = NULL) { |
| 52 |
project <- renv_project_resolve(project) |
| 53 |
root <- renv_paths_library_root(project) |
| 54 |
file.path(root, renv_platform_prefix(), ...) %||% "" |
| 55 |
} |
| 56 |
|
| 57 |
renv_paths_lockfile <- function(project = NULL) { |
| 58 |
|
| 59 |
# allow override |
| 60 |
# TODO: profiles? |
| 61 |
override <- Sys.getenv("RENV_PATHS_LOCKFILE", unset = NA) |
| 62 |
if (!is.na(override)) { |
| 63 |
last <- substr(override, nchar(override), nchar(override)) |
| 64 |
if (last %in% c("/", "\\")) |
| 65 |
override <- paste0(override, "renv.lock") |
| 66 |
# resolve relative paths against the project directory |
| 67 |
if (!renv_path_absolute(override)) { |
| 68 |
project <- renv_project_resolve(project) |
| 69 |
override <- file.path(project, override) |
| 70 |
} |
| 71 |
return(override) |
| 72 |
} |
| 73 |
|
| 74 |
# otherwise, use default location (location relative to renv folder) |
| 75 |
project <- renv_project_resolve(project) |
| 76 |
renv <- renv_paths_renv(project = project) |
| 77 |
file.path(dirname(renv), "renv.lock") |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
renv_paths_settings <- function(project = NULL) { |
| 82 |
renv_paths_renv("settings.json", project = project) |
| 83 |
} |
| 84 |
|
| 85 |
renv_paths_activate <- function(project = NULL) { |
| 86 |
renv_paths_renv("activate.R", profile = FALSE, project = project) |
| 87 |
} |
| 88 |
|
| 89 |
renv_paths_sandbox <- function(project = NULL) { |
| 90 |
|
| 91 |
# construct a platform prefix |
| 92 |
path <- R() |
| 93 |
hash <- memoize(path, renv_hash_text(path), scope = "renv_paths_sandbox") |
| 94 |
parts <- c(renv_platform_prefix(), substring(hash, 1L, 8L)) |
| 95 |
prefix <- paste(parts, collapse = "/") |
| 96 |
|
| 97 |
# check for override |
| 98 |
root <- Sys.getenv("RENV_PATHS_SANDBOX", unset = NA) |
| 99 |
if (!is.na(root)) |
| 100 |
return(paste(c(root, prefix), collapse = "/")) |
| 101 |
|
| 102 |
# otherwise, build path in user data directory |
| 103 |
userdir <- renv_bootstrap_user_dir() |
| 104 |
paste(c(userdir, "sandbox", prefix), collapse = "/") |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
renv_paths_renv <- function(..., profile = TRUE, project = NULL) { |
| 109 |
renv_bootstrap_paths_renv(..., profile = profile, project = project) |
| 110 |
} |
| 111 |
|
| 112 |
renv_paths_cellar <- function(...) { |
| 113 |
renv_paths_common("cellar", c(), ...) |
| 114 |
} |
| 115 |
|
| 116 |
renv_paths_local <- function(...) { |
| 117 |
renv_paths_common("local", c(), ...) |
| 118 |
} |
| 119 |
|
| 120 |
renv_paths_source <- function(...) { |
| 121 |
renv_paths_common("source", c(), ...) |
| 122 |
} |
| 123 |
|
| 124 |
renv_paths_binary <- function(...) { |
| 125 |
renv_paths_common("binary", c(renv_platform_prefix()), ...) |
| 126 |
} |
| 127 |
|
| 128 |
renv_paths_cache <- function(..., version = NULL) { |
| 129 |
platform <- renv_platform_prefix() |
| 130 |
version <- version %||% renv_cache_version() |
| 131 |
renv_paths_common("cache", c(version, platform), ...) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
renv_paths_rtools <- function() { |
| 136 |
|
| 137 |
root <- renv_paths_override("rtools") |
| 138 |
if (is.null(root)) { |
| 139 |
spec <- renv_rtools_find() |
| 140 |
root <- spec$root |
| 141 |
} |
| 142 |
|
| 143 |
root %||% "" |
| 144 |
} |
| 145 |
|
| 146 |
renv_paths_extsoft <- function(...) { |
| 147 |
renv_paths_common("extsoft", c(), ...) |
| 148 |
} |
| 149 |
|
| 150 |
renv_paths_p3m <- function(...) { |
| 151 |
renv_paths_common("p3m", c(), ...) |
| 152 |
} |
| 153 |
|
| 154 |
renv_paths_index <- function(...) { |
| 155 |
renv_paths_common("index", c(renv_platform_prefix()), ...) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
renv_paths_root <- function(...) { |
| 160 |
root <- renv_paths_override("root") %||% renv_paths_root_default() |
| 161 |
file.path(root, ...) %||% "" |
| 162 |
} |
| 163 |
|
| 164 |
# nocov start |
| 165 |
renv_paths_root_default <- function() { |
| 166 |
|
| 167 |
the$root <- the$root %||% { |
| 168 |
|
| 169 |
# use tempdir for cache when running tests |
| 170 |
# this check is necessary here to support packages which might use renv |
| 171 |
# during testing (and we don't want those to try to use the user dir) |
| 172 |
if (checking()) |
| 173 |
renv_paths_root_default_tempdir() |
| 174 |
else |
| 175 |
renv_paths_root_default_impl() |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
renv_paths_root_default_impl <- function() { |
| 182 |
|
| 183 |
# compute known root directories |
| 184 |
roots <- c( |
| 185 |
renv_paths_root_default_impl_v2(), |
| 186 |
renv_paths_root_default_impl_v1() |
| 187 |
) |
| 188 |
|
| 189 |
# iterate through those roots, finding the first existing |
| 190 |
for (root in roots) |
| 191 |
if (file.exists(root)) |
| 192 |
return(root) |
| 193 |
|
| 194 |
# if none exist, choose the most recent definition |
| 195 |
roots[[1L]] |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
renv_paths_root_default_impl_v2 <- function() { |
| 200 |
|
| 201 |
# try using tools to get the user directory |
| 202 |
tools <- renv_namespace_load("tools") |
| 203 |
if (is.function(tools$R_user_dir)) |
| 204 |
return(tools$R_user_dir("renv", "cache")) |
| 205 |
|
| 206 |
renv_paths_root_default_impl_v2_fallback() |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
renv_paths_root_default_impl_v2_fallback <- function() { |
| 211 |
|
| 212 |
# try using our own backfill for older versions of R |
| 213 |
envvars <- c("R_USER_CACHE_DIR", "XDG_CACHE_HOME") |
| 214 |
for (envvar in envvars) { |
| 215 |
root <- Sys.getenv(envvar, unset = NA) |
| 216 |
if (!is.na(root)) { |
| 217 |
path <- file.path(root, "R/renv") |
| 218 |
return(path) |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
# use platform-specific default fallbacks |
| 223 |
if (renv_platform_windows()) |
| 224 |
file.path(Sys.getenv("LOCALAPPDATA"), "R/cache/R/renv") |
| 225 |
else if (renv_platform_macos()) |
| 226 |
"~/Library/Caches/org.R-project.R/R/renv" |
| 227 |
else |
| 228 |
"~/.cache/R/renv" |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
renv_paths_root_default_impl_v1 <- function() { |
| 233 |
|
| 234 |
base <- switch( |
| 235 |
Sys.info()[["sysname"]], |
| 236 |
Darwin = Sys.getenv("XDG_DATA_HOME", "~/Library/Application Support"), |
| 237 |
Windows = Sys.getenv("LOCALAPPDATA", Sys.getenv("APPDATA")), |
| 238 |
Sys.getenv("XDG_DATA_HOME", "~/.local/share") |
| 239 |
) |
| 240 |
|
| 241 |
file.path(base, "renv") |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
renv_paths_root_default_tempdir <- function() { |
| 246 |
temp <- file.path(tempdir(), "renv") |
| 247 |
ensure_directory(temp) |
| 248 |
return(temp) |
| 249 |
} |
| 250 |
|
| 251 |
# nocov end |
| 252 |
|
| 253 |
#' Path for storing global state |
| 254 |
#' |
| 255 |
#' @description |
| 256 |
#' By default, renv stores global state in the following OS-specific folders: |
| 257 |
#' |
| 258 |
#' \tabular{ll}{ |
| 259 |
#' **Platform** \tab **Location** \cr |
| 260 |
#' Linux \tab `~/.cache/R/renv` \cr |
| 261 |
#' macOS \tab `~/Library/Caches/org.R-project.R/R/renv` \cr |
| 262 |
#' Windows \tab `%LOCALAPPDATA%/R/cache/R/renv` \cr |
| 263 |
#' } |
| 264 |
#' |
| 265 |
#' If desired, this path can be customized by setting the `RENV_PATHS_ROOT` |
| 266 |
#' environment variable. This can be useful if you'd like, for example, multiple |
| 267 |
#' users to be able to share a single global cache. |
| 268 |
#' |
| 269 |
#' # Customising individual paths |
| 270 |
#' |
| 271 |
#' The various state sub-directories can also be individually adjusted, if so |
| 272 |
#' desired (e.g. you'd prefer to keep the cache of package installations on a |
| 273 |
#' separate volume). The various environment variables that can be set are |
| 274 |
#' enumerated below: |
| 275 |
#' |
| 276 |
#' \tabular{ll}{ |
| 277 |
#' \strong{Environment Variable} \tab \strong{Description} \cr |
| 278 |
#' \code{RENV_PATHS_ROOT} \tab The root path used for global state storage. \cr |
| 279 |
#' \code{RENV_PATHS_LIBRARY} \tab The path to the project library. \cr |
| 280 |
#' \code{RENV_PATHS_LIBRARY_ROOT} \tab The parent path for project libraries. \cr |
| 281 |
#' \code{RENV_PATHS_LIBRARY_STAGING} \tab The parent path used for staged package installs. \cr |
| 282 |
#' \code{RENV_PATHS_SANDBOX} \tab The path to the sandboxed \R system library. \cr |
| 283 |
#' \code{RENV_PATHS_LOCKFILE} \tab The path to the [lockfile]. \cr |
| 284 |
#' \code{RENV_PATHS_CELLAR} \tab The path to the cellar, containing local package binaries and sources. \cr |
| 285 |
#' \code{RENV_PATHS_SOURCE} \tab The path containing downloaded package sources. \cr |
| 286 |
#' \code{RENV_PATHS_BINARY} \tab The path containing downloaded package binaries. \cr |
| 287 |
#' \code{RENV_PATHS_CACHE} \tab The path containing cached package installations. \cr |
| 288 |
#' \code{RENV_PATHS_PREFIX} \tab An optional prefix to prepend to the constructed library / cache paths. \cr |
| 289 |
#' \code{RENV_PATHS_RENV} \tab The path to the project's renv folder. For advanced users only. \cr |
| 290 |
#' \code{RENV_PATHS_RTOOLS} \tab (Windows only) The path to [Rtools](https://cran.r-project.org/bin/windows/Rtools/). \cr |
| 291 |
#' \code{RENV_PATHS_EXTSOFT} \tab (Windows only) The path containing external software needed for compilation of Windows source packages. \cr |
| 292 |
#' } |
| 293 |
#' |
| 294 |
#' (If you want these settings to persist in your project, it is recommended that |
| 295 |
#' you add these to an appropriate \R startup file. For example, these could be |
| 296 |
#' set in: a project-local `.Renviron`, the user-level `.Renviron`, or a |
| 297 |
#' site-wide file at `file.path(R.home("etc"), "Renviron.site")`. See |
| 298 |
#' [Startup] for more details). |
| 299 |
#' |
| 300 |
#' Note that renv will append platform-specific and version-specific entries |
| 301 |
#' to the set paths as appropriate. For example, if you have set: |
| 302 |
#' |
| 303 |
#' ``` |
| 304 |
#' Sys.setenv(RENV_PATHS_CACHE = "/mnt/shared/renv/cache") |
| 305 |
#' ``` |
| 306 |
#' |
| 307 |
#' then the directory used for the cache will still depend on the renv cache |
| 308 |
#' version (e.g. `v2`), the \R version (e.g. `3.5`) and the platform (e.g. |
| 309 |
#' `x86_64-pc-linux-gnu`). For example: |
| 310 |
#' |
| 311 |
#' ``` |
| 312 |
#' /mnt/shared/renv/cache/v2/R-3.5/x86_64-pc-linux-gnu |
| 313 |
#' ``` |
| 314 |
#' |
| 315 |
#' This ensures that you can set a single `RENV_PATHS_CACHE` environment variable |
| 316 |
#' globally without worry that it may cause collisions or errors if multiple |
| 317 |
#' versions of \R needed to interact with the same cache. |
| 318 |
#' |
| 319 |
#' If reproducibility of a project is desired on a particular machine, it is |
| 320 |
#' highly recommended that the renv cache of installed packages + binary |
| 321 |
#' packages is backed up and persisted, so that packages can be easily restored |
| 322 |
#' in the future -- installation of packages from source can often be arduous. |
| 323 |
#' |
| 324 |
#' # Sharing state across operating systems |
| 325 |
#' |
| 326 |
#' If you need to share the same cache with multiple different Linux operating |
| 327 |
#' systems, you may want to set the `RENV_PATHS_PREFIX` environment variable |
| 328 |
#' to help disambiguate the paths used on Linux. For example, setting |
| 329 |
#' `RENV_PATHS_PREFIX = "ubuntu-bionic"` would instruct renv to construct a |
| 330 |
#' cache path like: |
| 331 |
#' |
| 332 |
#' ``` |
| 333 |
#' /mnt/shared/renv/cache/v2/ubuntu-bionic/R-3.5/x86_64-pc-linux-gnu |
| 334 |
#' ``` |
| 335 |
#' |
| 336 |
#' If this is required, it's strongly recommended that this environment |
| 337 |
#' variable is set in your \R installation's `Renviron.site` file, typically |
| 338 |
#' located at `file.path(R.home("etc"), "Renviron.site")`, so that it can be |
| 339 |
#' active for any \R sessions launched on that machine. |
| 340 |
#' |
| 341 |
#' Starting from `renv 0.13.0`, you can also instruct renv to auto-generate |
| 342 |
#' an OS-specific component to include as part of library and cache paths, |
| 343 |
#' by setting the environment variable: |
| 344 |
#' |
| 345 |
#' ``` |
| 346 |
#' RENV_PATHS_PREFIX_AUTO = TRUE |
| 347 |
#' ``` |
| 348 |
#' |
| 349 |
#' The prefix will be constructed based on fields within the system's |
| 350 |
#' `/etc/os-release` file. Note that this is the default behavior with |
| 351 |
#' `renv 1.0.6` when using R 4.4.0 or later. |
| 352 |
#' |
| 353 |
#' # Package cellar |
| 354 |
#' |
| 355 |
#' If your project depends on one or more \R packages that are not available in any |
| 356 |
#' remote location, you can still provide a locally-available tarball for renv |
| 357 |
#' to use during restore. By default, these packages should be made available in |
| 358 |
#' the folder as specified by the `RENV_PATHS_CELLAR` environment variable. The |
| 359 |
#' package sources should be placed in a file at one of these locations: |
| 360 |
#' |
| 361 |
#' - `${RENV_PATHS_CELLAR}/<package>_<version>.<ext>` |
| 362 |
#' - `${RENV_PATHS_CELLAR}/<package>/<package>_<version>.<ext>` |
| 363 |
#' - `<project>/renv/cellar/<package>_<version>.<ext>` |
| 364 |
#' - `<project>/renv/cellar/<package>/<package>_<version>.<ext>` |
| 365 |
#' |
| 366 |
#' where `.<ext>` is `.tar.gz` for source packages, or `.tgz` for binaries on |
| 367 |
#' macOS and `.zip` for binaries on Windows. During `restore()`, renv will |
| 368 |
#' search the cellar for a compatible package, and prefer installation with |
| 369 |
#' that copy of the package if appropriate. |
| 370 |
#' |
| 371 |
#' # Older versions |
| 372 |
#' |
| 373 |
#' Older version of renv used a different default cache location. |
| 374 |
#' Those cache locations are: |
| 375 |
#' |
| 376 |
#' \tabular{ll}{ |
| 377 |
#' **Platform** \tab **Location** \cr |
| 378 |
#' Linux \tab `~/.local/share/renv` \cr |
| 379 |
#' macOS \tab `~/Library/Application Support/renv` \cr |
| 380 |
#' Windows \tab `%LOCALAPPDATA%/renv` \cr |
| 381 |
#' } |
| 382 |
#' |
| 383 |
#' If an renv root directory has already been created in one of the old |
| 384 |
#' locations, that will still be used. This change was made to comply with the |
| 385 |
#' CRAN policy requirements of \R packages. |
| 386 |
#' |
| 387 |
#' @rdname paths |
| 388 |
#' @name paths |
| 389 |
#' |
| 390 |
#' @format NULL |
| 391 |
#' |
| 392 |
#' @export |
| 393 |
#' |
| 394 |
#' @examples |
| 395 |
#' # get the path to the project library |
| 396 |
#' path <- renv::paths$library() |
| 397 |
paths <- list( |
| 398 |
root = renv_paths_root, |
| 399 |
library = renv_paths_library, |
| 400 |
lockfile = renv_paths_lockfile, |
| 401 |
settings = renv_paths_settings, |
| 402 |
cache = renv_paths_cache, |
| 403 |
sandbox = renv_paths_sandbox |
| 404 |
) |