| 1 |
|
| 2 |
the$restore_running <- FALSE |
| 3 |
the$restore_state <- NULL |
| 4 |
|
| 5 |
#' Restore project library from a lockfile |
| 6 |
#' |
| 7 |
#' Restore a project's dependencies from a lockfile, as previously generated by |
| 8 |
#' [snapshot()]. |
| 9 |
#' |
| 10 |
#' `renv::restore()` compares packages recorded in the lockfile to |
| 11 |
#' the packages installed in the project library. Where there are differences |
| 12 |
#' it resolves them by installing the lockfile-recorded package into the |
| 13 |
#' project library. If `clean = TRUE`, `restore()` will additionally delete any |
| 14 |
#' packages in the project library that don't appear in the lockfile. |
| 15 |
#' |
| 16 |
#' @section Transactional Restore: |
| 17 |
#' |
| 18 |
#' By default, `renv::restore()` will perform a 'transactional' restore, wherein the |
| 19 |
#' project library is mutated only if all packages within the lockfile are successfully |
| 20 |
#' restored. The intention here is to prevent the private library from entering |
| 21 |
#' an inconsistent state, if some subset of packages were to install successfully |
| 22 |
#' but some other subset of packages did not. `renv::restore(transactional = FALSE)` |
| 23 |
#' can be useful if you're attempting to restore packages from a lockfile, but would |
| 24 |
#' like to update or change certain packages piece-meal if they fail to install. |
| 25 |
#' |
| 26 |
#' The term 'transactional' here borrows from the parlance of a 'database transaction', |
| 27 |
#' where the failure of any intermediate step implies that the whole transaction |
| 28 |
#' will be rolled back, so that the state of the database before the transaction |
| 29 |
#' was initiated can be preserved. See <https://en.wikipedia.org/wiki/Database_transaction> |
| 30 |
#' for more details. |
| 31 |
#' |
| 32 |
#' @inherit renv-params |
| 33 |
#' |
| 34 |
#' @param library The library paths to be used during restore. |
| 35 |
#' |
| 36 |
#' @param packages A subset of packages recorded in the lockfile to restore. |
| 37 |
#' When `NULL` (the default), all packages available in the lockfile will be |
| 38 |
#' restored. Any required recursive dependencies of the requested packages |
| 39 |
#' will be restored as well. |
| 40 |
#' |
| 41 |
#' @param transactional Whether or not to use a 'transactional' restore. |
| 42 |
#' See **Transactional Restore** for more details. When `NULL` (the default), |
| 43 |
#' the value of the `install.transactional` [`config`] option will be used. |
| 44 |
#' |
| 45 |
#' @param exclude A subset of packages to be excluded during restore. This can |
| 46 |
#' be useful for when you'd like to restore all but a subset of packages from |
| 47 |
#' a lockfile. Note that if you attempt to exclude a package which is required |
| 48 |
#' as the recursive dependency of another package, your request will be |
| 49 |
#' ignored. |
| 50 |
#' |
| 51 |
#' @param strict Boolean; when `TRUE`, packages whose lockfile records contain |
| 52 |
#' a repository URL in the `Repository` field will only be retrieved from |
| 53 |
#' that exact repository. If the recorded repository is unavailable, the |
| 54 |
#' restore will fail rather than falling back to other configured |
| 55 |
#' repositories. This can be useful when you need to ensure that packages |
| 56 |
#' are installed from a specific source (e.g. an internal package repository). |
| 57 |
#' |
| 58 |
#' @return A named list of package records which were installed by renv. |
| 59 |
#' |
| 60 |
#' @family reproducibility |
| 61 |
#' |
| 62 |
#' @export |
| 63 |
#' |
| 64 |
#' @example examples/examples-init.R |
| 65 |
restore <- function(project = NULL, |
| 66 |
..., |
| 67 |
library = NULL, |
| 68 |
lockfile = NULL, |
| 69 |
packages = NULL, |
| 70 |
exclude = NULL, |
| 71 |
rebuild = FALSE, |
| 72 |
repos = NULL, |
| 73 |
clean = FALSE, |
| 74 |
strict = FALSE, |
| 75 |
transactional = NULL, |
| 76 |
prompt = interactive()) |
| 77 |
{ |
| 78 |
renv_consent_check() |
| 79 |
renv_scope_error_handler() |
| 80 |
renv_dots_check(...) |
| 81 |
|
| 82 |
renv_scope_binding(the, "restore_running", TRUE) |
| 83 |
|
| 84 |
project <- renv_project_resolve(project) |
| 85 |
renv_project_lock(project = project) |
| 86 |
renv_scope_verbose_if(prompt) |
| 87 |
|
| 88 |
# resolve library, lockfile arguments |
| 89 |
libpaths <- renv_libpaths_resolve(library) |
| 90 |
lockfile <- lockfile %||% renv_lockfile_load(project = project, strict = TRUE) |
| 91 |
|
| 92 |
# set up .renvignore defensively |
| 93 |
renv_load_cache_renvignore(project = project) |
| 94 |
|
| 95 |
# set up transactional param |
| 96 |
transactional <- transactional %||% config$install.transactional() |
| 97 |
renv_scope_options(renv.config.install.transactional = transactional) |
| 98 |
|
| 99 |
# check and ask user if they need to activate first |
| 100 |
renv_activate_prompt("restore", library, prompt, project) |
| 101 |
|
| 102 |
# activate the requested library (place at front of library paths) |
| 103 |
library <- nth(libpaths, 1L) |
| 104 |
ensure_directory(library) |
| 105 |
renv_scope_libpaths(libpaths) |
| 106 |
|
| 107 |
# resolve the lockfile |
| 108 |
if (is.character(lockfile)) |
| 109 |
lockfile <- renv_lockfile_read(lockfile) |
| 110 |
|
| 111 |
# insert overrides (if any) |
| 112 |
lockfile <- renv_lockfile_override(lockfile) |
| 113 |
|
| 114 |
# repair potential issues in the lockfile |
| 115 |
lockfile <- renv_lockfile_repair(lockfile) |
| 116 |
|
| 117 |
# check for system requirements from these packages |
| 118 |
if (config$sysreqs.check(default = renv_platform_linux())) { |
| 119 |
records <- renv_lockfile_records(lockfile) |
| 120 |
sysreqs <- map(records, `[[`, "SystemRequirements") |
| 121 |
renv_sysreqs_check(sysreqs, prompt = prompt) |
| 122 |
} |
| 123 |
|
| 124 |
# override repositories if requested |
| 125 |
repos <- repos %||% config$repos.override() %||% lockfile$R$Repositories |
| 126 |
|
| 127 |
# transform PPM repositories if appropriate |
| 128 |
if (renv_ppm_enabled()) |
| 129 |
repos <- renv_ppm_transform(repos) |
| 130 |
|
| 131 |
if (length(repos)) |
| 132 |
renv_scope_options(repos = convert(repos, "character")) |
| 133 |
|
| 134 |
# if users have requested the use of pak, delegate there |
| 135 |
if (config$pak.enabled() && !recursing()) { |
| 136 |
|
| 137 |
renv_pak_init() |
| 138 |
|
| 139 |
# pak doesn't handle package removals, so when clean = TRUE we drop |
| 140 |
# unused packages from the project library ourselves before delegating |
| 141 |
# the install to pak |
| 142 |
if (clean) |
| 143 |
renv_pak_restore_clean(lockfile, libpaths, library, project, packages, exclude, prompt) |
| 144 |
|
| 145 |
records <- renv_pak_restore( |
| 146 |
lockfile = lockfile, |
| 147 |
packages = packages, |
| 148 |
exclude = exclude, |
| 149 |
prompt = prompt, |
| 150 |
project = project |
| 151 |
) |
| 152 |
|
| 153 |
return(renv_restore_successful(records, prompt, project)) |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
# set up Bioconductor version + repositories |
| 158 |
biocversion <- getOption("renv.bioconductor.version") %||% lockfile$Bioconductor$Version |
| 159 |
if (!is.null(biocversion)) { |
| 160 |
renv_bioconductor_init(library = library) |
| 161 |
biocversion <- package_version(biocversion) |
| 162 |
renv_scope_options(renv.bioconductor.version = biocversion) |
| 163 |
} |
| 164 |
|
| 165 |
# get records for R packages currently installed |
| 166 |
current <- snapshot(project = project, |
| 167 |
library = libpaths, |
| 168 |
lockfile = NULL, |
| 169 |
type = "all") |
| 170 |
|
| 171 |
# compare lockfile vs. currently-installed packages |
| 172 |
diff <- renv_lockfile_diff_packages(current, lockfile) |
| 173 |
|
| 174 |
# don't remove packages unless 'clean = TRUE' |
| 175 |
diff <- renv_vector_diff(diff, if (!clean) "remove") |
| 176 |
|
| 177 |
# only remove packages from the project library |
| 178 |
ispkg <- map_lgl(names(diff), function(package) { |
| 179 |
path <- find.package(package, lib.loc = libpaths, quiet = TRUE) |
| 180 |
identical(dirname(path), library) |
| 181 |
}) |
| 182 |
diff <- diff[!(diff == "remove" & !ispkg)] |
| 183 |
|
| 184 |
# don't take any actions with ignored packages |
| 185 |
ignored <- renv_project_ignored_packages(project = project) |
| 186 |
diff <- diff[renv_vector_diff(names(diff), ignored)] |
| 187 |
|
| 188 |
# only take action with requested packages; if a subset of packages was |
| 189 |
# explicitly requested, include their recursive lockfile dependencies as well |
| 190 |
requested <- packages |
| 191 |
descriptions <- NULL |
| 192 |
packages <- if (is.null(requested)) { |
| 193 |
setdiff(names(diff), exclude) |
| 194 |
} else { |
| 195 |
setdiff(requested, exclude) |
| 196 |
} |
| 197 |
if (length(packages) && !is.null(requested)) { |
| 198 |
records <- renv_lockfile_records(lockfile) |
| 199 |
descriptions <- renv_graph_init(packages, records = records, project = project) |
| 200 |
packages <- setdiff(names(descriptions), exclude) |
| 201 |
} |
| 202 |
diff <- diff[intersect(names(diff), packages)] |
| 203 |
|
| 204 |
if (!length(diff)) { |
| 205 |
name <- if (!missing(library)) "library" else "project" |
| 206 |
writef("- The %s is already synchronized with the lockfile.", name) |
| 207 |
return(renv_restore_successful(diff, prompt, project)) |
| 208 |
} |
| 209 |
|
| 210 |
# transform binary repository URLs into source repository URLs |
| 211 |
current <- renv_restore_normalize(current) |
| 212 |
lockfile <- renv_restore_normalize(lockfile) |
| 213 |
|
| 214 |
|
| 215 |
# TODO: should we avoid double-prompting here? |
| 216 |
# we prompt once here for the preflight check, and then again below based |
| 217 |
# on the actions we'll perform. |
| 218 |
if (!renv_restore_preflight(project, libpaths, diff, current, lockfile)) |
| 219 |
cancel_if(prompt && !proceed()) |
| 220 |
|
| 221 |
if (prompt || renv_verbose()) { |
| 222 |
renv_restore_report_actions(diff, current, lockfile) |
| 223 |
cancel_if(prompt && !proceed()) |
| 224 |
} |
| 225 |
|
| 226 |
# perform the restore |
| 227 |
records <- renv_restore_run_actions(project, diff, current, lockfile, rebuild, strict, descriptions) |
| 228 |
renv_restore_successful(records, prompt, project) |
| 229 |
} |
| 230 |
|
| 231 |
renv_restore_run_actions <- function(project, actions, current, lockfile, rebuild, strict = FALSE, descriptions = NULL) { |
| 232 |
|
| 233 |
packages <- names(actions) |
| 234 |
|
| 235 |
renv_scope_restore( |
| 236 |
project = project, |
| 237 |
library = renv_libpaths_active(), |
| 238 |
records = renv_lockfile_records(lockfile), |
| 239 |
packages = packages, |
| 240 |
rebuild = rebuild, |
| 241 |
strict = strict |
| 242 |
) |
| 243 |
|
| 244 |
# first, handle package removals |
| 245 |
removes <- actions[actions == "remove"] |
| 246 |
enumerate(removes, function(package, action) { |
| 247 |
renv_restore_remove(project, package, current) |
| 248 |
}) |
| 249 |
|
| 250 |
# next, handle installs |
| 251 |
installs <- actions[actions != "remove"] |
| 252 |
packages <- names(installs) |
| 253 |
|
| 254 |
# resolve dependency graph using lockfile records as lookup table |
| 255 |
lockrecords <- renv_lockfile_records(lockfile) |
| 256 |
descriptions <- descriptions %||% |
| 257 |
renv_graph_init(packages, records = lockrecords, project = project) |
| 258 |
|
| 259 |
# download + install in parallel dependency waves |
| 260 |
records <- renv_graph_install(descriptions) |
| 261 |
|
| 262 |
# check for failed packages; offer to retry with latest versions |
| 263 |
failed <- setdiff(packages, names(records)) |
| 264 |
if (length(failed)) { |
| 265 |
recovered <- renv_restore_recover(failed, project) |
| 266 |
if (length(recovered)) { |
| 267 |
records <- c(records, recovered) |
| 268 |
failed <- setdiff(packages, names(records)) |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
# error if any requested packages still failed to install |
| 273 |
if (length(failed)) { |
| 274 |
stopf( |
| 275 |
"failed to install %s", |
| 276 |
paste(dQuote(failed), collapse = ", ") |
| 277 |
) |
| 278 |
} |
| 279 |
|
| 280 |
# detect dependency tree repair |
| 281 |
diff <- renv_lockfile_diff_packages(lockrecords, records) |
| 282 |
diff <- diff[diff != "remove"] |
| 283 |
if (!empty(diff)) { |
| 284 |
renv_pretty_print_records( |
| 285 |
"The dependency tree was repaired during package installation:", |
| 286 |
records[names(diff)], |
| 287 |
"Call `renv::snapshot()` to capture these dependencies in the lockfile." |
| 288 |
) |
| 289 |
} |
| 290 |
|
| 291 |
# check installed packages and prompt for reload if needed |
| 292 |
renv_install_postamble(names(records)) |
| 293 |
|
| 294 |
# return status |
| 295 |
invisible(records) |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
renv_restore_recover <- function(failed, project) { |
| 300 |
|
| 301 |
ok <- ask( |
| 302 |
"Would you like to try installing the latest available versions of these packages?", |
| 303 |
default = FALSE |
| 304 |
) |
| 305 |
|
| 306 |
if (!ok) |
| 307 |
return(NULL) |
| 308 |
|
| 309 |
writef("") |
| 310 |
|
| 311 |
# narrow state$packages to just the failed packages so that |
| 312 |
# already-installed dependencies are not needlessly re-installed |
| 313 |
state <- renv_restore_state() |
| 314 |
old <- state$packages |
| 315 |
state$packages <- failed |
| 316 |
defer(state$packages <- old) |
| 317 |
|
| 318 |
# resolve latest versions (no lockfile records = latest from repos) |
| 319 |
descriptions <- catch(renv_graph_init(failed, project = project)) |
| 320 |
if (inherits(descriptions, "error")) |
| 321 |
return(NULL) |
| 322 |
|
| 323 |
renv_graph_install(descriptions) |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
renv_restore_state <- function(key = NULL) { |
| 328 |
state <- the$restore_state |
| 329 |
if (is.null(key)) state else state[[key]] |
| 330 |
} |
| 331 |
|
| 332 |
renv_restore_begin <- function(project = NULL, |
| 333 |
library = NULL, |
| 334 |
records = NULL, |
| 335 |
packages = NULL, |
| 336 |
handler = NULL, |
| 337 |
rebuild = NULL, |
| 338 |
strict = FALSE, |
| 339 |
recursive = TRUE) |
| 340 |
{ |
| 341 |
# resolve rebuild request |
| 342 |
rebuild <- case( |
| 343 |
identical(rebuild, TRUE) ~ packages, |
| 344 |
identical(rebuild, FALSE) ~ character(), |
| 345 |
identical(rebuild, "*") ~ NA_character_, |
| 346 |
as.character(rebuild) |
| 347 |
) |
| 348 |
|
| 349 |
# get previous restore state (so we can restore it after if needed) |
| 350 |
oldstate <- the$restore_state |
| 351 |
|
| 352 |
# set new restore state |
| 353 |
the$restore_state <- env( |
| 354 |
|
| 355 |
# the active project (if any) used for restore |
| 356 |
project = project, |
| 357 |
|
| 358 |
# the library path into which packages will be installed. |
| 359 |
# this is set because some behaviors depend on whether the target |
| 360 |
# library is the project library, but during staged installs the |
| 361 |
# library paths might be mutated during restore |
| 362 |
library = library, |
| 363 |
|
| 364 |
# the package records used for restore, providing information |
| 365 |
# on the packages to be installed (their version, source, etc) |
| 366 |
records = records, |
| 367 |
|
| 368 |
# the set of packages to be installed in this restore session; |
| 369 |
# as explicitly requested by the user / front-end API call. |
| 370 |
# packages in this list should be re-installed even if a compatible |
| 371 |
# version appears to be already installed |
| 372 |
packages = packages, |
| 373 |
|
| 374 |
# an optional handler, to be used during retrieve / restore |
| 375 |
# TODO: should we split this into separate handlers? |
| 376 |
handler = handler %||% function(package, action) action, |
| 377 |
|
| 378 |
# packages which should be rebuilt (skipping the cache) |
| 379 |
rebuild = rebuild, |
| 380 |
|
| 381 |
# when TRUE, packages with a URL-valued Repository field must be |
| 382 |
# retrieved from that exact repository; no fallback to other repos |
| 383 |
strict = strict, |
| 384 |
|
| 385 |
# should package dependencies be crawled recursively? this is useful if |
| 386 |
# the records list is incomplete and needs to be built as packages are |
| 387 |
# downloaded |
| 388 |
recursive = recursive, |
| 389 |
|
| 390 |
# packages which we have attempted to retrieve |
| 391 |
retrieved = new.env(parent = emptyenv()), |
| 392 |
|
| 393 |
# packages which need to be installed |
| 394 |
install = mapping(), |
| 395 |
|
| 396 |
# a collection of the requirements imposed on dependent packages |
| 397 |
# as they are discovered |
| 398 |
requirements = new.env(parent = emptyenv()), |
| 399 |
|
| 400 |
# the number of packages that were downloaded |
| 401 |
downloaded = 0L |
| 402 |
|
| 403 |
) |
| 404 |
|
| 405 |
# return prior state |
| 406 |
oldstate |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
renv_restore_end <- function(state) { |
| 411 |
the$restore_state <- state |
| 412 |
} |
| 413 |
|
| 414 |
# nocov start |
| 415 |
|
| 416 |
renv_restore_report_actions <- function(actions, current, lockfile) { |
| 417 |
|
| 418 |
if (!renv_verbose() || empty(actions)) |
| 419 |
return(invisible(NULL)) |
| 420 |
|
| 421 |
lhs <- renv_lockfile_records(current) |
| 422 |
rhs <- renv_lockfile_records(lockfile) |
| 423 |
renv_pretty_print_records_pair( |
| 424 |
"The following package(s) will be updated:", |
| 425 |
lhs[names(lhs) %in% names(actions)], |
| 426 |
rhs[names(rhs) %in% names(actions)] |
| 427 |
) |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
# nocov end |
| 432 |
|
| 433 |
renv_restore_remove <- function(project, package, lockfile) { |
| 434 |
records <- renv_lockfile_records(lockfile) |
| 435 |
record <- records[[package]] |
| 436 |
printf("- Removing %s [%s] ... ", package, record$Version) |
| 437 |
paths <- renv_paths_library(project = project, package) |
| 438 |
recursive <- renv_file_type(paths) == "directory" |
| 439 |
unlink(paths, recursive = recursive) |
| 440 |
writef("OK [removed from library]") |
| 441 |
TRUE |
| 442 |
} |
| 443 |
|
| 444 |
renv_restore_preflight <- function(project, libpaths, actions, current, lockfile) { |
| 445 |
records <- renv_lockfile_records(lockfile) |
| 446 |
matching <- keep(records, names(actions)) |
| 447 |
renv_install_preflight(project, libpaths, matching) |
| 448 |
} |
| 449 |
|
| 450 |
renv_restore_find <- function(package, record) { |
| 451 |
|
| 452 |
# skip packages whose installation was explicitly requested |
| 453 |
state <- renv_restore_state() |
| 454 |
record <- renv_record_validate(package, record) |
| 455 |
if (package %in% state$packages) |
| 456 |
return("") |
| 457 |
|
| 458 |
# check the active library paths to see if this package is already installed |
| 459 |
for (library in renv_libpaths_all()) { |
| 460 |
path <- renv_restore_find_impl(package, record, library) |
| 461 |
if (nzchar(path)) |
| 462 |
return(path) |
| 463 |
} |
| 464 |
|
| 465 |
"" |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
renv_restore_find_impl <- function(package, record, library) { |
| 470 |
|
| 471 |
path <- file.path(library, package) |
| 472 |
if (!file.exists(path)) |
| 473 |
return("") |
| 474 |
|
| 475 |
# attempt to read DESCRIPTION |
| 476 |
current <- catch(as.list(renv_description_read(path))) |
| 477 |
if (inherits(current, "error")) |
| 478 |
return("") |
| 479 |
|
| 480 |
# check for an up-to-date version from R package repository |
| 481 |
if (renv_record_source(record) %in% c("cran", "repository")) { |
| 482 |
fields <- c("Package", "Version") |
| 483 |
if (identical(record[fields], current[fields])) |
| 484 |
return(path) |
| 485 |
} |
| 486 |
|
| 487 |
# otherwise, match on remote fields |
| 488 |
fields <- renv_record_names(record, c("Package", "Version")) |
| 489 |
if (identical(record[fields], current[fields])) |
| 490 |
return(path) |
| 491 |
|
| 492 |
# failed to match; return empty path |
| 493 |
"" |
| 494 |
|
| 495 |
} |
| 496 |
|
| 497 |
renv_restore_rebuild_required <- function(record) { |
| 498 |
state <- renv_restore_state() |
| 499 |
any(c(NA_character_, record$Package) %in% state$rebuild) |
| 500 |
} |
| 501 |
|
| 502 |
renv_restore_successful <- function(records, prompt, project) { |
| 503 |
|
| 504 |
# ensure the activate script is up-to-date |
| 505 |
renv_infrastructure_write_activate(project, create = FALSE) |
| 506 |
|
| 507 |
# perform python-related restore steps |
| 508 |
renv_python_restore(project, prompt) |
| 509 |
|
| 510 |
# return restored records |
| 511 |
invisible(records) |
| 512 |
|
| 513 |
} |
| 514 |
|
| 515 |
renv_restore_normalize <- function(lockfile) { |
| 516 |
records <- renv_lockfile_records(lockfile) |
| 517 |
renv_lockfile_records(lockfile) <- map(records, renv_restore_normalize_impl) |
| 518 |
lockfile |
| 519 |
} |
| 520 |
|
| 521 |
renv_restore_normalize_impl <- function(record) { |
| 522 |
|
| 523 |
# transform binary repository URLs into source URLs |
| 524 |
if (config$ppm.enabled()) { |
| 525 |
repository <- record[["Repository"]] %||% "" |
| 526 |
if (nzchar(repository)) { |
| 527 |
srcurl <- renv_ppm_normalize(repository) |
| 528 |
binurl <- renv_ppm_transform(srcurl) |
| 529 |
record[["Repository"]] <- binurl |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
# apply override if set |
| 534 |
override <- config$repos.override() |
| 535 |
if (length(override)) { |
| 536 |
record[["Repository"]] <- NULL |
| 537 |
} |
| 538 |
|
| 539 |
# return potentially mutated record |
| 540 |
record |
| 541 |
|
| 542 |
} |