| 1 |
|
| 2 |
# an explicitly-requested package type in a call to 'install()' |
| 3 |
the$install_pkg_type <- NULL |
| 4 |
|
| 5 |
# an explicitly-requested dependencies field in a call to 'install()' |
| 6 |
the$install_dependency_fields <- NULL |
| 7 |
|
| 8 |
# the formatted width of installation steps printed to the console |
| 9 |
the$install_step_width <- 40L |
| 10 |
|
| 11 |
#' Install packages |
| 12 |
#' |
| 13 |
#' @description |
| 14 |
#' Install one or more \R packages, from a variety of remote sources. |
| 15 |
#' `install()` uses the same machinery as [restore()] (i.e. it uses cached |
| 16 |
#' packages where possible) but it does not respect the lockfile, instead |
| 17 |
#' installing the latest versions available from CRAN. |
| 18 |
#' |
| 19 |
#' See `vignette("package-install")` for more details. |
| 20 |
#' |
| 21 |
#' # `Remotes` |
| 22 |
#' |
| 23 |
#' `install()` (called without arguments) will respect the `Remotes` field |
| 24 |
#' of the `DESCRIPTION` file (if present). This allows you to specify places |
| 25 |
#' to install a package other than the latest version from CRAN. |
| 26 |
#' See <https://remotes.r-lib.org/articles/dependencies.html> for details. |
| 27 |
#' |
| 28 |
#' Remotes can optionally be prefixed with a package name and `=`, to |
| 29 |
#' explicitly associate a package name with a remote specification. For |
| 30 |
#' example: |
| 31 |
#' |
| 32 |
#' ``` |
| 33 |
#' Remotes: mypkg=user/repo |
| 34 |
#' ``` |
| 35 |
#' |
| 36 |
#' This is most useful when the package name cannot be inferred from the |
| 37 |
#' remote (e.g. when the repository name differs from the package name). |
| 38 |
#' |
| 39 |
#' # Bioconductor |
| 40 |
#' |
| 41 |
#' Packages from Bioconductor can be installed by using the `bioc::` prefix. |
| 42 |
#' For example, |
| 43 |
#' |
| 44 |
#' ``` |
| 45 |
#' renv::install("bioc::Biobase") |
| 46 |
#' ``` |
| 47 |
#' |
| 48 |
#' will install the latest-available version of Biobase from Bioconductor. |
| 49 |
#' |
| 50 |
#' renv depends on BiocManager (or, for older versions of \R, BiocInstaller) |
| 51 |
#' for the installation of packages from Bioconductor. If these packages are |
| 52 |
#' not available, renv will attempt to automatically install them before |
| 53 |
#' fulfilling the installation request. |
| 54 |
#' |
| 55 |
#' @inherit renv-params |
| 56 |
#' |
| 57 |
#' @param include Packages which should be installed. `include` can |
| 58 |
#' occasionally be useful when you'd like to call `renv::install()` with |
| 59 |
#' no arguments, but restrict package installation to only some subset |
| 60 |
#' of dependencies in the project. |
| 61 |
#' |
| 62 |
#' @param exclude Packages which should not be installed. `exclude` is useful |
| 63 |
#' when using `renv::install()` to install all dependencies in a project, |
| 64 |
#' except for a specific set of packages. |
| 65 |
#' |
| 66 |
#' @param verbose Boolean; report output from `R CMD build` and `R CMD INSTALL` |
| 67 |
#' during installation? When `NULL` (the default), the value of `config$install.verbose()` |
| 68 |
#' will be used. When `FALSE`, installation output will be emitted only if |
| 69 |
#' a package fails to install. |
| 70 |
#' |
| 71 |
#' @param transactional Whether or not to use a 'transactional' package |
| 72 |
#' installation. See **Transactional Restore** in [renv::restore()] for |
| 73 |
#' more details. When `NULL` (the default), the value of the |
| 74 |
#' `install.transactional` [`config`] option will be used. |
| 75 |
#' |
| 76 |
#' @param lock Boolean; update the `renv.lock` lockfile after the successful |
| 77 |
#' installation of the requested packages? |
| 78 |
#' |
| 79 |
#' @return A named list of package records which were installed by renv. |
| 80 |
#' |
| 81 |
#' @export |
| 82 |
#' |
| 83 |
#' @examples |
| 84 |
#' \dontrun{ |
| 85 |
#' |
| 86 |
#' # install the latest version of 'digest' |
| 87 |
#' renv::install("digest") |
| 88 |
#' |
| 89 |
#' # install an old version of 'digest' (using archives) |
| 90 |
#' renv::install("digest@@0.6.18") |
| 91 |
#' |
| 92 |
#' # install 'digest' from GitHub (latest dev. version) |
| 93 |
#' renv::install("eddelbuettel/digest") |
| 94 |
#' |
| 95 |
#' # install a package from GitHub, using specific commit |
| 96 |
#' renv::install("eddelbuettel/digest@@df55b00bff33e945246eff2586717452e635032f") |
| 97 |
#' |
| 98 |
#' # install a package from Bioconductor |
| 99 |
#' # (note: requires the BiocManager package) |
| 100 |
#' renv::install("bioc::Biobase") |
| 101 |
#' |
| 102 |
#' # install a package from a subdirectory of a GitHub repo |
| 103 |
#' renv::install("username/repo:subdir") |
| 104 |
#' |
| 105 |
#' # install a package, specifying path explicitly |
| 106 |
#' renv::install("~/path/to/package") |
| 107 |
#' |
| 108 |
#' # install packages as declared in the project DESCRIPTION file |
| 109 |
#' renv::install() |
| 110 |
#' |
| 111 |
#' } |
| 112 |
install <- function(packages = NULL, |
| 113 |
..., |
| 114 |
include = NULL, |
| 115 |
exclude = NULL, |
| 116 |
library = NULL, |
| 117 |
type = NULL, |
| 118 |
rebuild = FALSE, |
| 119 |
repos = NULL, |
| 120 |
prompt = interactive(), |
| 121 |
dependencies = NULL, |
| 122 |
verbose = NULL, |
| 123 |
transactional = NULL, |
| 124 |
lock = FALSE, |
| 125 |
project = NULL) |
| 126 |
{ |
| 127 |
renv_consent_check() |
| 128 |
renv_scope_error_handler() |
| 129 |
|
| 130 |
# allow user to provide additional package names as part of '...' |
| 131 |
if (!missing(...)) { |
| 132 |
dots <- list(...) |
| 133 |
names(dots) <- names(dots) %||% rep.int("", length(dots)) |
| 134 |
packages <- c(packages, dots[!nzchar(names(dots))]) |
| 135 |
} |
| 136 |
|
| 137 |
project <- renv_project_resolve(project) |
| 138 |
renv_project_lock(project = project) |
| 139 |
renv_scope_verbose_if(prompt) |
| 140 |
|
| 141 |
# handle 'dependencies'; only override the binding when the user explicitly |
| 142 |
# passes a value, so that renv_dependencies_discover_description_fields() |
| 143 |
# can still add Suggests for the project DESCRIPTION by default |
| 144 |
if (!is.null(dependencies)) { |
| 145 |
fields <- renv_dependencies_fields(dependencies, project = project) |
| 146 |
renv_scope_binding(the, "install_dependency_fields", fields) |
| 147 |
} |
| 148 |
|
| 149 |
# handle 'verbose' |
| 150 |
verbose <- verbose %||% config$install.verbose() |
| 151 |
renv_scope_options(renv.config.install.verbose = verbose) |
| 152 |
|
| 153 |
# handle 'transactional' |
| 154 |
transactional <- transactional %||% config$install.transactional() |
| 155 |
renv_scope_options(renv.config.install.transactional = transactional) |
| 156 |
|
| 157 |
# set up library paths |
| 158 |
libpaths <- renv_libpaths_resolve(library) |
| 159 |
renv_scope_libpaths(libpaths) |
| 160 |
|
| 161 |
# check for explicitly-provided type -- we handle this specially for PPM |
| 162 |
if (!is.null(type)) { |
| 163 |
renv_scope_binding(the, "install_pkg_type", type) |
| 164 |
renv_scope_options(pkgType = type) |
| 165 |
} |
| 166 |
|
| 167 |
# override repositories if requested |
| 168 |
repos <- repos %||% config$repos.override() |
| 169 |
if (length(repos)) |
| 170 |
renv_scope_options(repos = repos) |
| 171 |
|
| 172 |
# if users have requested the use of pak, delegate there |
| 173 |
if (config$pak.enabled() && !recursing()) { |
| 174 |
renv_pak_init() |
| 175 |
return( |
| 176 |
renv_pak_install( |
| 177 |
packages = packages, |
| 178 |
include = include, |
| 179 |
exclude = exclude, |
| 180 |
library = libpaths, |
| 181 |
type = type, |
| 182 |
rebuild = rebuild, |
| 183 |
prompt = prompt, |
| 184 |
project = project |
| 185 |
) |
| 186 |
) |
| 187 |
} |
| 188 |
|
| 189 |
# resolve remotes from explicitly-requested packages |
| 190 |
remotes <- if (length(packages)) { |
| 191 |
remotes <- map(packages, renv_remotes_resolve) |
| 192 |
names(remotes) <- map_chr(remotes, `[[`, "Package") |
| 193 |
remotes |
| 194 |
} |
| 195 |
|
| 196 |
# figure out which packages we should install |
| 197 |
packages <- names(remotes) %||% renv_snapshot_dependencies(project, dev = TRUE) |
| 198 |
|
| 199 |
# apply ignored.packages setting when auto-discovering |
| 200 |
if (is.null(remotes)) { |
| 201 |
ignored <- renv_project_ignored_packages(project) |
| 202 |
packages <- setdiff(packages, ignored) |
| 203 |
} |
| 204 |
|
| 205 |
# apply exclude parameter |
| 206 |
if (length(exclude)) |
| 207 |
packages <- setdiff(packages, exclude) |
| 208 |
|
| 209 |
# apply include parameter |
| 210 |
if (length(include)) |
| 211 |
packages <- intersect(packages, include) |
| 212 |
|
| 213 |
if (empty(packages)) { |
| 214 |
writef("- There are no packages to install.") |
| 215 |
return(invisible(list())) |
| 216 |
} |
| 217 |
|
| 218 |
# add bioconductor packages if necessary; scope bioconductor repos |
| 219 |
# so they're available for transitive dependency resolution in graph_init |
| 220 |
if (renv_bioconductor_required(remotes)) { |
| 221 |
renv_scope_bioconductor(project = project) |
| 222 |
bioc <- c(renv_bioconductor_manager(), "BiocVersion") |
| 223 |
packages <- unique(c(packages, bioc)) |
| 224 |
} |
| 225 |
|
| 226 |
# don't update renv unless it was explicitly requested |
| 227 |
if (!"renv" %in% names(remotes)) |
| 228 |
packages <- setdiff(packages, "renv") |
| 229 |
|
| 230 |
# start building a list of records; they should be resolved this priority: |
| 231 |
# |
| 232 |
# 1. explicit requests from the user |
| 233 |
# 2. remotes declarations from the DESCRIPTION file |
| 234 |
# 3. existing version in library, if any |
| 235 |
# 4. fallback to package repositories |
| 236 |
# |
| 237 |
# we overlay 1 and 2 here, and then do 3 and 4 dynamically if required |
| 238 |
# during the retrieve + install stages |
| 239 |
records <- overlay(renv_project_remotes(project), remotes) |
| 240 |
|
| 241 |
# run install preflight checks |
| 242 |
if (!renv_install_preflight(project, libpaths, records)) |
| 243 |
cancel_if(prompt && !proceed()) |
| 244 |
|
| 245 |
# we're now ready to start installation |
| 246 |
renv_scope_restore( |
| 247 |
project = project, |
| 248 |
library = renv_libpaths_active(), |
| 249 |
packages = names(remotes), |
| 250 |
records = records, |
| 251 |
rebuild = rebuild |
| 252 |
) |
| 253 |
|
| 254 |
# build dependency graph; this resolves transitive dependencies |
| 255 |
# and fetches DESCRIPTION metadata without downloading tarballs |
| 256 |
descriptions <- renv_graph_init(packages, records = records, project = project) |
| 257 |
if (empty(descriptions)) { |
| 258 |
writef("- There are no packages to install.") |
| 259 |
return(invisible(list())) |
| 260 |
} |
| 261 |
|
| 262 |
# filter out packages that are already correctly installed so |
| 263 |
# the report only lists packages that will actually be installed; |
| 264 |
# renv_graph_install performs the same filtering internally |
| 265 |
library <- renv_libpaths_active() |
| 266 |
requirements <- renv_graph_requirements(descriptions) |
| 267 |
needed <- Filter(function(pkg) { |
| 268 |
renv_graph_needs_update(pkg, descriptions[[pkg]], requirements) |
| 269 |
}, names(descriptions)) |
| 270 |
|
| 271 |
if (empty(needed)) { |
| 272 |
writef("- All packages are already installed.") |
| 273 |
return(invisible(list())) |
| 274 |
} |
| 275 |
|
| 276 |
if (prompt || renv_verbose()) { |
| 277 |
renv_install_report(descriptions[needed], library = library) |
| 278 |
cancel_if(prompt && !proceed()) |
| 279 |
} |
| 280 |
|
| 281 |
# check for system requirements using pre-fetched descriptions |
| 282 |
if (config$sysreqs.check(default = renv_platform_linux())) { |
| 283 |
sysreqs <- map(descriptions, function(desc) desc[["SystemRequirements"]] %||% "") |
| 284 |
renv_sysreqs_check(sysreqs, prompt = prompt) |
| 285 |
} |
| 286 |
|
| 287 |
# download and install packages in dependency-wave order |
| 288 |
records <- renv_graph_install(descriptions) |
| 289 |
|
| 290 |
# if any explicitly-requested packages failed, signal an error |
| 291 |
# (this preserves the old behavior where install("nonexistent") errors) |
| 292 |
# but don't error for packages that are already installed — |
| 293 |
# unless resolution itself failed (e.g. incompatible R version) |
| 294 |
requested <- names(remotes) %||% packages |
| 295 |
failed <- setdiff(requested, names(records)) |
| 296 |
failed <- intersect(failed, names(descriptions)) |
| 297 |
library <- renv_libpaths_active() |
| 298 |
failed <- Filter(function(pkg) { |
| 299 |
if (isTRUE(attr(descriptions[[pkg]], "resolution_failed"))) |
| 300 |
return(TRUE) |
| 301 |
!renv_package_installed(pkg, lib.loc = library) |
| 302 |
}, failed) |
| 303 |
if (length(failed)) { |
| 304 |
reasons <- vapply(failed, function(pkg) { |
| 305 |
attr(descriptions[[pkg]], "resolution_error") %||% "" |
| 306 |
}, character(1L)) |
| 307 |
labels <- ifelse( |
| 308 |
nzchar(reasons), |
| 309 |
sprintf("%s (%s)", dQuote(failed), reasons), |
| 310 |
dQuote(failed) |
| 311 |
) |
| 312 |
stopf("failed to install %s", paste(labels, collapse = ", ")) |
| 313 |
} |
| 314 |
|
| 315 |
# check loaded packages and inform user if out-of-sync |
| 316 |
renv_install_postamble(names(records)) |
| 317 |
|
| 318 |
# update lockfile if requested |
| 319 |
if (lock && length(records)) { |
| 320 |
|
| 321 |
# avoid next automatic snapshot |
| 322 |
renv_snapshot_auto_suppress_next() |
| 323 |
|
| 324 |
# re-compute the records, to ensure they're normalized in the same |
| 325 |
# way as they might be in snapshot() |
| 326 |
# https://github.com/rstudio/renv/issues/1828 |
| 327 |
updates <- renv_lockfile_create( |
| 328 |
project = project, |
| 329 |
libpaths = libpaths, |
| 330 |
packages = names(records), |
| 331 |
exclude = exclude, |
| 332 |
prompt = FALSE, |
| 333 |
force = TRUE |
| 334 |
) |
| 335 |
|
| 336 |
# overlay these records onto the existing lockfile |
| 337 |
lockfile <- renv_lockfile_load(project = project) |
| 338 |
lockfile <- renv_lockfile_modify(lockfile, renv_lockfile_records(updates)) |
| 339 |
renv_lockfile_save(lockfile, project = project) |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
invisible(records) |
| 344 |
} |
| 345 |
|
| 346 |
renv_install_impl <- function(records) { |
| 347 |
|
| 348 |
staged <- renv_config_install_staged() |
| 349 |
|
| 350 |
writef(header("Installing packages")) |
| 351 |
|
| 352 |
if (staged) |
| 353 |
renv_install_staged(records) |
| 354 |
else |
| 355 |
renv_install_default(records) |
| 356 |
|
| 357 |
invisible(TRUE) |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
renv_install_staged <- function(records) { |
| 362 |
|
| 363 |
# get current libpaths |
| 364 |
libpaths <- renv_libpaths_all() |
| 365 |
|
| 366 |
# set up a dummy library path for installation |
| 367 |
templib <- renv_install_staged_library_path() |
| 368 |
defer(unlink(templib, recursive = TRUE)) |
| 369 |
renv_scope_libpaths(c(templib, libpaths)) |
| 370 |
|
| 371 |
# perform the install |
| 372 |
renv_install_default(records) |
| 373 |
|
| 374 |
# migrate packages into true library |
| 375 |
library <- nth(libpaths, 1L) |
| 376 |
sources <- list.files(templib, full.names = TRUE) |
| 377 |
targets <- file.path(library, basename(sources)) |
| 378 |
names(targets) <- sources |
| 379 |
enumerate(targets, renv_file_move, overwrite = TRUE) |
| 380 |
|
| 381 |
# clear filebacked cache entries |
| 382 |
descpaths <- file.path(targets, "DESCRIPTION") |
| 383 |
renv_filebacked_clear("renv_description_read", descpaths) |
| 384 |
renv_filebacked_clear("renv_hash_description", descpaths) |
| 385 |
|
| 386 |
invisible(targets) |
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
renv_install_staged_library_path_impl <- function() { |
| 391 |
|
| 392 |
# get current library path |
| 393 |
libpath <- renv_libpaths_active() |
| 394 |
|
| 395 |
# retrieve current project, library path |
| 396 |
stagedlib <- local({ |
| 397 |
|
| 398 |
# allow user configuration of staged library location |
| 399 |
override <- Sys.getenv("RENV_PATHS_LIBRARY_STAGING", unset = NA) |
| 400 |
if (!is.na(override)) |
| 401 |
return(override) |
| 402 |
|
| 403 |
# if we have an active project, use that path |
| 404 |
project <- renv_project_get(default = NULL) |
| 405 |
if (!is.null(project)) |
| 406 |
return(renv_paths_renv("staging", project = project)) |
| 407 |
|
| 408 |
# otherwise, stage within library path |
| 409 |
file.path(libpath, ".renv") |
| 410 |
|
| 411 |
}) |
| 412 |
|
| 413 |
# attempt to create it |
| 414 |
ok <- catch(ensure_directory(stagedlib)) |
| 415 |
if (inherits(ok, "error")) |
| 416 |
return(tempfile("renv-staging-")) |
| 417 |
|
| 418 |
# resolve a unique staging directory in this path |
| 419 |
# we want to keep paths short just in case; it's easy to blow up the |
| 420 |
# path length limit (hence we don't use tempfile below) |
| 421 |
for (i in 1:100) { |
| 422 |
path <- file.path(stagedlib, i) |
| 423 |
if (dir.create(path, showWarnings = FALSE)) |
| 424 |
return(path) |
| 425 |
} |
| 426 |
|
| 427 |
# all else fails, use tempfile |
| 428 |
tempfile("renv-staging-") |
| 429 |
|
| 430 |
} |
| 431 |
|
| 432 |
# NOTE: on Windows, installing packages into very long paths |
| 433 |
# can fail, as R's internal unzip utility does not handle |
| 434 |
# long Windows paths well. in addition, an renv project's |
| 435 |
# library path tends to be long, exasperating the issue. |
| 436 |
# for that reason, we try to use a shorter staging directory |
| 437 |
# |
| 438 |
# part of the challenge here is that the R temporary directory |
| 439 |
# and R library path might reside on different mounts, and so |
| 440 |
# we may want to try and avoid installing on one mount and then |
| 441 |
# copying to another mount (as that could be slow). |
| 442 |
# |
| 443 |
# note that using the renv folder might be counter-productive, |
| 444 |
# since users will want to use renv in projects sync'ed via |
| 445 |
# OneDrive and friends, and we don't want those to lock files |
| 446 |
# in the staging directory |
| 447 |
renv_install_staged_library_path <- function() { |
| 448 |
|
| 449 |
# compute path |
| 450 |
path <- renv_install_staged_library_path_impl() |
| 451 |
|
| 452 |
# create library directory |
| 453 |
ensure_directory(path) |
| 454 |
|
| 455 |
# try to make sure it has the same permissions as the library itself |
| 456 |
if (!renv_platform_windows()) { |
| 457 |
libpath <- renv_libpaths_active() |
| 458 |
umask <- Sys.umask("0") |
| 459 |
defer(Sys.umask(umask)) |
| 460 |
info <- renv_file_info(libpath) |
| 461 |
Sys.chmod(path, info$mode) |
| 462 |
} |
| 463 |
|
| 464 |
# return the created path |
| 465 |
return(path) |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
renv_install_default <- function(records) { |
| 470 |
state <- renv_restore_state() |
| 471 |
handler <- state$handler |
| 472 |
|
| 473 |
for (record in records) { |
| 474 |
package <- record$Package |
| 475 |
handler(package, renv_install_package(record)) |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
renv_install_package <- function(record) { |
| 480 |
|
| 481 |
# get active project (if any) |
| 482 |
state <- renv_restore_state() |
| 483 |
project <- state$project |
| 484 |
|
| 485 |
# figure out whether we can use the cache during install |
| 486 |
# use library path recorded in restore state as staged installs will have |
| 487 |
# mutated the library path, placing a staging library at the front |
| 488 |
library <- renv_restore_state("library") |
| 489 |
linkable <- renv_cache_linkable(project = project, library = library) |
| 490 |
linker <- if (linkable) renv_file_link else renv_file_copy |
| 491 |
|
| 492 |
cacheable <- |
| 493 |
renv_cache_config_enabled(project = project) && |
| 494 |
renv_record_cacheable(record) && |
| 495 |
!renv_restore_rebuild_required(record) |
| 496 |
|
| 497 |
if (cacheable) { |
| 498 |
|
| 499 |
# check for cache entry and install if there |
| 500 |
path <- renv_cache_find(record) |
| 501 |
if (renv_cache_package_validate(path)) |
| 502 |
return(renv_install_package_cache(record, path, linker)) |
| 503 |
|
| 504 |
} |
| 505 |
|
| 506 |
# install the package |
| 507 |
before <- Sys.time() |
| 508 |
withCallingHandlers( |
| 509 |
renv_install_package_impl(record), |
| 510 |
error = function(e) renv_install_step_error(record) |
| 511 |
) |
| 512 |
after <- Sys.time() |
| 513 |
|
| 514 |
path <- record$Path |
| 515 |
type <- renv_package_type(path, quiet = TRUE) |
| 516 |
feedback <- renv_install_package_feedback(path, type) |
| 517 |
|
| 518 |
|
| 519 |
# link into cache |
| 520 |
if (renv_cache_config_enabled(project = project)) { |
| 521 |
cached <- renv_cache_synchronize(record, linkable = linkable) |
| 522 |
if (cached) |
| 523 |
feedback <- paste(feedback, "and cached") |
| 524 |
} |
| 525 |
|
| 526 |
elapsed <- difftime(after, before, units = "auto") |
| 527 |
renv_install_step_ok(feedback, record, elapsed = elapsed) |
| 528 |
|
| 529 |
invisible() |
| 530 |
|
| 531 |
} |
| 532 |
|
| 533 |
renv_install_package_feedback <- function(path, type) { |
| 534 |
|
| 535 |
if (identical(type, "source")) |
| 536 |
return("built from source") |
| 537 |
|
| 538 |
if (renv_file_type(path, symlinks = FALSE) == "directory") |
| 539 |
return("copied local binary") |
| 540 |
|
| 541 |
"installed binary" |
| 542 |
|
| 543 |
} |
| 544 |
|
| 545 |
renv_install_package_cache <- function(record, cache, linker) { |
| 546 |
|
| 547 |
if (renv_install_package_cache_skip(record, cache)) |
| 548 |
return(TRUE) |
| 549 |
|
| 550 |
library <- renv_libpaths_active() |
| 551 |
target <- file.path(library, record[["Package"]]) |
| 552 |
|
| 553 |
# back up the previous installation if needed |
| 554 |
callback <- renv_file_backup(target) |
| 555 |
defer(callback()) |
| 556 |
|
| 557 |
before <- Sys.time() |
| 558 |
linker(cache, target) |
| 559 |
after <- Sys.time() |
| 560 |
|
| 561 |
type <- case( |
| 562 |
identical(linker, renv_file_copy) ~ "copied from cache", |
| 563 |
identical(linker, renv_file_link) ~ "linked from cache" |
| 564 |
) |
| 565 |
|
| 566 |
elapsed <- difftime(after, before, units = "auto") |
| 567 |
renv_install_step_ok(type, record, elapsed = elapsed) |
| 568 |
|
| 569 |
return(TRUE) |
| 570 |
|
| 571 |
} |
| 572 |
|
| 573 |
renv_install_package_cache_skip <- function(record, cache) { |
| 574 |
|
| 575 |
# don't skip if installation was explicitly requested |
| 576 |
if (record$Package %in% renv_restore_state("packages")) |
| 577 |
return(FALSE) |
| 578 |
|
| 579 |
# check for matching cache + target paths |
| 580 |
library <- renv_restore_state("library") %||% renv_libpaths_active() |
| 581 |
target <- file.path(library, record$Package) |
| 582 |
|
| 583 |
renv_file_same(cache, target) |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
renv_install_package_impl_prebuild <- function(record, path, quiet) { |
| 588 |
|
| 589 |
# check whether user wants us to build before install |
| 590 |
if (!identical(config$install.build(), TRUE)) |
| 591 |
return(path) |
| 592 |
|
| 593 |
# if this package already appears to be built, nothing to do |
| 594 |
if (renv_package_built(path)) |
| 595 |
return(path) |
| 596 |
|
| 597 |
# if this is an archive, we'll need to unpack it first |
| 598 |
info <- renv_file_info(path) |
| 599 |
if (identical(info$isdir, FALSE)) { |
| 600 |
|
| 601 |
# find the package directory |
| 602 |
files <- renv_archive_list(path) |
| 603 |
descpath <- grep("(?:^|/)DESCRIPTION$", files, value = TRUE) |
| 604 |
pkgpath <- dirname(descpath)[nchar(descpath) == min(nchar(descpath))] |
| 605 |
|
| 606 |
# extract to temporary directory |
| 607 |
exdir <- tempfile("renv-build-") |
| 608 |
ensure_directory(exdir) |
| 609 |
renv_archive_decompress(path, exdir = exdir) |
| 610 |
|
| 611 |
# update path to package |
| 612 |
path <- file.path(exdir, pkgpath) |
| 613 |
|
| 614 |
# and ensure we build in this directory |
| 615 |
renv_scope_wd(path) |
| 616 |
|
| 617 |
} |
| 618 |
|
| 619 |
# if this package depends on a VignetteBuilder that is not installed, |
| 620 |
# then we can't proceed |
| 621 |
descpath <- file.path(path, "DESCRIPTION") |
| 622 |
desc <- renv_description_read(descpath) |
| 623 |
builder <- desc[["VignetteBuilder"]] |
| 624 |
if (!is.null(builder) && !renv_package_installed(builder)) { |
| 625 |
fmt <- "Skipping package build: vignette builder '%s' is not installed" |
| 626 |
writef(fmt, builder) |
| 627 |
return(path) |
| 628 |
} |
| 629 |
|
| 630 |
before <- Sys.time() |
| 631 |
package <- record$Package |
| 632 |
newpath <- r_cmd_build(package, path) |
| 633 |
after <- Sys.time() |
| 634 |
|
| 635 |
newpath |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
renv_install_package_impl <- function(record, quiet = TRUE) { |
| 640 |
|
| 641 |
package <- record$Package |
| 642 |
|
| 643 |
# get path for package |
| 644 |
path <- record$Path |
| 645 |
|
| 646 |
# check if it's an archive (versus an unpacked directory) |
| 647 |
info <- renv_file_info(path) |
| 648 |
isarchive <- identical(info$isdir, FALSE) |
| 649 |
|
| 650 |
subdir <- record$RemoteSubdir %||% "" |
| 651 |
if (isarchive) { |
| 652 |
# re-pack archives if they appear to have their package |
| 653 |
# sources contained as part of a sub-directory |
| 654 |
path <- renv_package_unpack(package, path, subdir = subdir) |
| 655 |
} else if (nzchar(subdir)) { |
| 656 |
# for directories, we may need to use subdir to find the package path |
| 657 |
components <- c(path, subdir) |
| 658 |
path <- paste(components, collapse = "/") |
| 659 |
} |
| 660 |
|
| 661 |
# check whether we should build before install |
| 662 |
path <- renv_install_package_impl_prebuild(record, path, quiet) |
| 663 |
|
| 664 |
# run user-defined hooks before, after install |
| 665 |
options <- renv_install_package_options(package) |
| 666 |
before <- options$before.install %||% identity |
| 667 |
after <- options$after.install %||% identity |
| 668 |
|
| 669 |
before(package) |
| 670 |
defer(after(package)) |
| 671 |
|
| 672 |
# backup an existing installation of the package if it exists |
| 673 |
library <- renv_libpaths_active() |
| 674 |
destination <- file.path(library, package) |
| 675 |
callback <- renv_file_backup(destination) |
| 676 |
defer(callback()) |
| 677 |
|
| 678 |
# normalize paths |
| 679 |
path <- renv_path_normalize(path, mustWork = TRUE) |
| 680 |
|
| 681 |
# get library path |
| 682 |
library <- renv_libpaths_active() |
| 683 |
|
| 684 |
# if a package already exists at that path, back it up first |
| 685 |
# this avoids problems with older versions of R attempting to |
| 686 |
# overwrite a pre-existing symlink |
| 687 |
# |
| 688 |
# https://github.com/rstudio/renv/issues/611 |
| 689 |
installpath <- file.path(library, package) |
| 690 |
callback <- renv_file_backup(installpath) |
| 691 |
defer(callback()) |
| 692 |
|
| 693 |
# if this failed for some reason, just remove it |
| 694 |
if (renv_file_broken(installpath)) |
| 695 |
renv_file_remove(installpath) |
| 696 |
|
| 697 |
# if this is the path to an unpacked binary archive, |
| 698 |
# we can just copy the folder over |
| 699 |
isdir <- renv_file_type(path, symlinks = FALSE) == "directory" |
| 700 |
isbin <- renv_package_type(path, quiet = TRUE) == "binary" |
| 701 |
copyable <- isdir && isbin |
| 702 |
|
| 703 |
# shortcut via copying a binary directory if possible, |
| 704 |
# otherwise, install the package |
| 705 |
if (copyable) |
| 706 |
renv_file_copy(path, installpath, overwrite = TRUE) |
| 707 |
else |
| 708 |
r_cmd_install(package, path) |
| 709 |
|
| 710 |
# if we just installed a binary package, check that it can be loaded |
| 711 |
# (source packages are checked by default on install) |
| 712 |
withCallingHandlers( |
| 713 |
if (isbin) renv_install_test(package), |
| 714 |
error = function(err) unlink(installpath, recursive = TRUE) |
| 715 |
) |
| 716 |
|
| 717 |
# augment package metadata after install |
| 718 |
renv_package_augment(installpath, record) |
| 719 |
|
| 720 |
# return the path to the package |
| 721 |
invisible(installpath) |
| 722 |
|
| 723 |
} |
| 724 |
|
| 725 |
renv_install_test <- function(package) { |
| 726 |
|
| 727 |
# add escape hatch, just in case |
| 728 |
# (test binaries by default on Linux, but not Windows or macOS) |
| 729 |
enabled <- Sys.getenv("RENV_INSTALL_TEST_LOAD", unset = renv_platform_linux()) |
| 730 |
if (!truthy(enabled)) |
| 731 |
return(TRUE) |
| 732 |
|
| 733 |
# check whether we should skip installation testing |
| 734 |
opts <- r_cmd_install_option(package, c("install.opts", "INSTALL_opts"), FALSE) |
| 735 |
if (is.character(opts)) { |
| 736 |
flags <- unlist(strsplit(opts, "\\s+", perl = TRUE)) |
| 737 |
if ("--no-test-load" %in% flags) |
| 738 |
return(TRUE) |
| 739 |
} |
| 740 |
|
| 741 |
# make sure we use the current library paths in the launched process |
| 742 |
rlibs <- paste(renv_libpaths_all(), collapse = .Platform$path.sep) |
| 743 |
renv_scope_envvars(R_LIBS = rlibs, R_LIBS_USER = "NULL", R_LIBS_SITE = "NULL") |
| 744 |
|
| 745 |
# also hide from user .Renviron files |
| 746 |
# https://github.com/wch/r-source/blob/1c0a2dc8ce6c05f68e1959ffbe6318a309277df3/src/library/tools/R/check.R#L273-L276 |
| 747 |
renv_scope_envvars(R_ENVIRON_USER = "NULL") |
| 748 |
|
| 749 |
# make sure R_TESTS is unset here, just in case |
| 750 |
# https://github.com/wch/r-source/blob/1c0a2dc8ce6c05f68e1959ffbe6318a309277df3/src/library/tools/R/install.R#L76-L79 |
| 751 |
renv_scope_envvars(R_TESTS = NULL) |
| 752 |
|
| 753 |
# the actual code we'll run in the other process |
| 754 |
# we use 'loadNamespace()' rather than 'library()' because some packages might |
| 755 |
# intentionally throw an error in their .onAttach() hooks |
| 756 |
# https://github.com/rstudio/renv/issues/1611 |
| 757 |
code <- expr({ |
| 758 |
.libPaths(!!.libPaths()) |
| 759 |
options(warn = 1L) |
| 760 |
loadNamespace(!!package) |
| 761 |
}) |
| 762 |
|
| 763 |
# write it to a tempfile |
| 764 |
script <- renv_scope_tempfile("renv-install-", fileext = ".R") |
| 765 |
writeLines(deparse(code), con = script) |
| 766 |
|
| 767 |
# check that the package can be loaded in a separate process |
| 768 |
renv_system_exec( |
| 769 |
command = R(), |
| 770 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 771 |
action = sprintf("testing if '%s' can be loaded", package) |
| 772 |
) |
| 773 |
|
| 774 |
# return TRUE to indicate successful validation |
| 775 |
TRUE |
| 776 |
|
| 777 |
} |
| 778 |
|
| 779 |
renv_install_package_options <- function(package) { |
| 780 |
options <- getOption("renv.install.package.options") |
| 781 |
options[[package]] |
| 782 |
} |
| 783 |
|
| 784 |
renv_install_postamble <- function(packages) { |
| 785 |
|
| 786 |
# only diagnose packages currently loaded |
| 787 |
packages <- renv_vector_intersect(packages, loadedNamespaces()) |
| 788 |
|
| 789 |
installed <- map_chr(packages, renv_package_version) |
| 790 |
loaded <- map_chr(packages, renv_namespace_version) |
| 791 |
|
| 792 |
bulletin( |
| 793 |
c("", "The following loaded package(s) have been updated:"), |
| 794 |
packages[installed != loaded], |
| 795 |
"Restart your R session to use the new versions." |
| 796 |
) |
| 797 |
|
| 798 |
TRUE |
| 799 |
|
| 800 |
} |
| 801 |
|
| 802 |
renv_install_preflight_unknown_source <- function(records) { |
| 803 |
renv_check_unknown_source(records) |
| 804 |
} |
| 805 |
|
| 806 |
renv_install_preflight_permissions <- function(library) { |
| 807 |
|
| 808 |
# try creating and deleting a directory in the library folder |
| 809 |
file <- renv_scope_tempfile(".renv-write-test-", tmpdir = library) |
| 810 |
dir.create(file, recursive = TRUE, showWarnings = FALSE) |
| 811 |
|
| 812 |
# check if we created the directory successfully |
| 813 |
info <- renv_file_info(file) |
| 814 |
if (identical(info$isdir, TRUE)) |
| 815 |
return(TRUE) |
| 816 |
|
| 817 |
# nocov start |
| 818 |
if (renv_verbose()) { |
| 819 |
|
| 820 |
# construct header for message |
| 821 |
preamble <- "renv appears to be unable to access the requested library path:" |
| 822 |
|
| 823 |
# construct footer for message |
| 824 |
info <- as.list(Sys.info()) |
| 825 |
fmt <- "Check that the '%s' user has read / write access to this directory." |
| 826 |
postamble <- sprintf(fmt, info$effective_user %||% info$user) |
| 827 |
|
| 828 |
# print it |
| 829 |
bulletin( |
| 830 |
preamble = preamble, |
| 831 |
values = library, |
| 832 |
postamble = postamble |
| 833 |
) |
| 834 |
|
| 835 |
} |
| 836 |
# nocov end |
| 837 |
|
| 838 |
FALSE |
| 839 |
|
| 840 |
} |
| 841 |
|
| 842 |
renv_install_preflight <- function(project, libpaths, records) { |
| 843 |
|
| 844 |
library <- nth(libpaths, 1L) |
| 845 |
records <- filter(records, Negate(is.function)) |
| 846 |
|
| 847 |
all( |
| 848 |
renv_install_preflight_unknown_source(records), |
| 849 |
renv_install_preflight_permissions(library) |
| 850 |
) |
| 851 |
|
| 852 |
} |
| 853 |
|
| 854 |
renv_install_report <- function(records, library) { |
| 855 |
renv_pretty_print_records( |
| 856 |
"The following package(s) will be installed:", |
| 857 |
records, |
| 858 |
sprintf("These packages will be installed into %s.", renv_path_pretty(library)) |
| 859 |
) |
| 860 |
} |
| 861 |
|
| 862 |
renv_install_step_ok <- function(message, record, elapsed = NULL) { |
| 863 |
|
| 864 |
pkgver <- paste(record[["Package"]], record[["Version"]]) |
| 865 |
if (!is.null(elapsed) && !testing() && elapsed >= 0.5) |
| 866 |
message <- sprintf("%s in %s", message, renv_difftime_format_short(elapsed)) |
| 867 |
|
| 868 |
writef("%s %s [%s]", yay(), format(pkgver, width = the$install_step_width), message) |
| 869 |
|
| 870 |
} |
| 871 |
|
| 872 |
renv_install_step_error <- function(record) { |
| 873 |
pkgver <- paste(record[["Package"]], record[["Version"]]) |
| 874 |
writef("%s %s", boo(), format(pkgver, width = the$install_step_width)) |
| 875 |
} |