| 1 |
|
| 2 |
#' Record current state of the project library in the lockfile |
| 3 |
#' |
| 4 |
#' @description |
| 5 |
#' Call `renv::snapshot()` to update a [lockfile] with the current state of |
| 6 |
#' dependencies in the project library. The lockfile can be used to later |
| 7 |
#' [restore] these dependencies as required. |
| 8 |
#' |
| 9 |
#' It's also possible to call `renv::snapshot()` with a non-renv project, |
| 10 |
#' in which case it will record the current state of dependencies in the |
| 11 |
#' current library paths. This makes it possible to [restore] the current packages, |
| 12 |
#' providing lightweight portability and reproducibility without isolation. |
| 13 |
#' |
| 14 |
#' If you want to automatically snapshot after each change, you can |
| 15 |
#' set `config$config$auto.snapshot(TRUE)` -- see `?config` for more details. |
| 16 |
#' |
| 17 |
#' # Snapshot types |
| 18 |
#' |
| 19 |
#' Depending on how you prefer to manage your \R package dependencies, you may |
| 20 |
#' want to enable an alternate snapshot type.. The types available are as follows: |
| 21 |
#' |
| 22 |
#' \describe{ |
| 23 |
#' |
| 24 |
#' \item{`"implicit"`}{ |
| 25 |
#' (The default) Capture only packages which appear to be used in your project, |
| 26 |
#' as determined by `renv::dependencies()`. This ensures that only the packages |
| 27 |
#' actually required by your project will enter the lockfile; the downside |
| 28 |
#' if it might be slow if your project contains a large number of files. |
| 29 |
#' If speed becomes an issue, you might consider using `.renvignore` files to |
| 30 |
#' limit which files renv uses for dependency discovery, or switching to |
| 31 |
#' explicit mode, as described next. |
| 32 |
#' } |
| 33 |
#' |
| 34 |
#' \item{`"explicit"`}{ |
| 35 |
#' Only capture packages which are explicitly listed in the project |
| 36 |
#' `DESCRIPTION` file. This workflow is recommended for users who wish to |
| 37 |
#' manage their project's \R package dependencies directly, and can be used |
| 38 |
#' for both package and non-package \R projects. Packages used in this manner |
| 39 |
#' should be recorded in either the `Depends` or `Imports` field of the |
| 40 |
#' `DESCRIPTION` file. |
| 41 |
#' } |
| 42 |
#' |
| 43 |
#' \item{`"all"`}{ |
| 44 |
#' Capture all packages within the active \R libraries in the lockfile. |
| 45 |
#' This is the quickest and simplest method, but may lead to undesired |
| 46 |
#' packages (e.g. development dependencies) entering the lockfile. |
| 47 |
#' } |
| 48 |
#' |
| 49 |
#' \item{`"custom"`}{ |
| 50 |
#' Like `"implicit"`, but use a custom user-defined filter instead. The filter |
| 51 |
#' should be specified by the \R option `renv.snapshot.filter`, and should |
| 52 |
#' either be a character vector naming a function (e.g. `"package::method"`), |
| 53 |
#' or be a function itself. The function should only accept one argument (the |
| 54 |
#' project directory), and should return a vector of package names to include |
| 55 |
#' in the lockfile. |
| 56 |
#' } |
| 57 |
#' |
| 58 |
#' } |
| 59 |
#' |
| 60 |
#' You can change the snapshot type for the current project with [settings()]. |
| 61 |
#' For example, the following code will switch to using `"explicit"` snapshots: |
| 62 |
#' |
| 63 |
#' ``` |
| 64 |
#' renv::settings$snapshot.type("explicit") |
| 65 |
#' ``` |
| 66 |
#' |
| 67 |
#' When the `packages` argument is set, `type` is ignored, and instead only the |
| 68 |
#' requested set of packages, and their recursive dependencies, will be written |
| 69 |
#' to the lockfile. |
| 70 |
#' |
| 71 |
#' @inherit renv-params |
| 72 |
#' |
| 73 |
#' @param library The \R libraries to snapshot. When `NULL`, the active \R |
| 74 |
#' libraries (as reported by `.libPaths()`) are used. |
| 75 |
#' |
| 76 |
#' @param lockfile The location where the generated lockfile should be written. |
| 77 |
#' By default, the lockfile is written to a file called `renv.lock` in the |
| 78 |
#' project directory. When `NULL`, the lockfile (as an \R object) is returned |
| 79 |
#' directly instead. |
| 80 |
#' |
| 81 |
#' @param type The type of snapshot to perform: |
| 82 |
#' * `"implicit"`, (the default), uses all packages captured by [dependencies()]. |
| 83 |
#' * `"explicit"` uses packages recorded in `DESCRIPTION`. |
| 84 |
#' * `"all"` uses all packages in the project library. |
| 85 |
#' * `"custom"` uses a custom filter. |
| 86 |
#' |
| 87 |
#' See **Snapshot types** below for more details. |
| 88 |
#' |
| 89 |
#' @inheritParams dependencies |
| 90 |
#' |
| 91 |
#' @param repos The \R repositories to be recorded in the lockfile. Defaults |
| 92 |
#' to the currently active package repositories, as retrieved by |
| 93 |
#' `getOption("repos")`. |
| 94 |
#' |
| 95 |
#' |
| 96 |
#' @param packages A vector of packages to be included in the lockfile. When |
| 97 |
#' `NULL` (the default), all packages relevant for the type of snapshot being |
| 98 |
#' performed will be included. When set, the `type` argument is ignored. |
| 99 |
#' Recursive dependencies of the specified packages will be added to the |
| 100 |
#' lockfile as well. |
| 101 |
#' |
| 102 |
#' @param exclude A vector of packages to be explicitly excluded from the lockfile. |
| 103 |
#' Note that transitive package dependencies will always be included, to avoid |
| 104 |
#' potentially creating an incomplete / non-functional lockfile. |
| 105 |
#' |
| 106 |
#' @param update Boolean; if the lockfile already exists, then attempt to update |
| 107 |
#' that lockfile without removing any prior package records. |
| 108 |
#' |
| 109 |
#' @param force Boolean; force generation of a lockfile even when pre-flight |
| 110 |
#' validation checks have failed? |
| 111 |
#' |
| 112 |
#' @param reprex Boolean; generate output appropriate for embedding the lockfile |
| 113 |
#' as part of a [reprex](https://tidyverse.org/help/#reprex)? |
| 114 |
#' |
| 115 |
#' @return The generated lockfile, as an \R object (invisibly). Note that |
| 116 |
#' this function is normally called for its side effects. |
| 117 |
#' |
| 118 |
#' |
| 119 |
#' @seealso More on handling package [dependencies()] |
| 120 |
#' @family reproducibility |
| 121 |
#' |
| 122 |
#' @export |
| 123 |
#' |
| 124 |
#' @example examples/examples-init.R |
| 125 |
snapshot <- function(project = NULL, |
| 126 |
..., |
| 127 |
library = NULL, |
| 128 |
lockfile = paths$lockfile(project = project), |
| 129 |
type = settings$snapshot.type(project = project), |
| 130 |
dev = NULL, |
| 131 |
repos = getOption("repos"), |
| 132 |
packages = NULL, |
| 133 |
exclude = NULL, |
| 134 |
prompt = interactive(), |
| 135 |
update = FALSE, |
| 136 |
force = FALSE, |
| 137 |
reprex = FALSE) |
| 138 |
{ |
| 139 |
renv_consent_check() |
| 140 |
renv_scope_error_handler() |
| 141 |
renv_dots_check(...) |
| 142 |
|
| 143 |
renv_snapshot_auto_suppress_next() |
| 144 |
|
| 145 |
project <- renv_project_resolve(project) |
| 146 |
renv_project_lock(project = project) |
| 147 |
renv_scope_verbose_if(prompt) |
| 148 |
|
| 149 |
# use setting as default if dev not explicitly provided |
| 150 |
if (is.null(dev)) |
| 151 |
dev <- settings$snapshot.dev(project = project) |
| 152 |
|
| 153 |
repos <- renv_repos_validate(repos) |
| 154 |
renv_scope_options(repos = repos) |
| 155 |
|
| 156 |
# set up .renvignore defensively |
| 157 |
renv_load_cache_renvignore(project = project) |
| 158 |
|
| 159 |
if (!is.null(lockfile)) |
| 160 |
renv_activate_prompt("snapshot", library, prompt, project) |
| 161 |
|
| 162 |
libpaths <- renv_path_normalize(library %||% renv_libpaths_all()) |
| 163 |
if (config$snapshot.validate()) |
| 164 |
renv_snapshot_preflight(project, libpaths) |
| 165 |
|
| 166 |
# when packages is set, we treat this as an 'all' type snapshot, but |
| 167 |
# with explicit package filters turned on |
| 168 |
if (!is.null(packages)) { |
| 169 |
|
| 170 |
if (!missing(type)) { |
| 171 |
fmt <- "packages argument is set; type argument %s will be ignored" |
| 172 |
warningf(fmt, stringify(type)) |
| 173 |
} |
| 174 |
|
| 175 |
type <- "packages" |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
alt <- new <- renv_lockfile_create( |
| 180 |
project = project, |
| 181 |
type = type, |
| 182 |
libpaths = libpaths, |
| 183 |
packages = packages, |
| 184 |
exclude = exclude, |
| 185 |
prompt = prompt, |
| 186 |
force = force, |
| 187 |
dev = dev |
| 188 |
) |
| 189 |
|
| 190 |
if (is.null(lockfile)) |
| 191 |
return(new) |
| 192 |
|
| 193 |
# if running as part of 'reprex', then render output inline |
| 194 |
if (reprex) |
| 195 |
return(renv_snapshot_reprex(new)) |
| 196 |
|
| 197 |
# check for missing dependencies and warn if any are discovered |
| 198 |
# (note: use 'new' rather than 'alt' here as we don't want to attempt |
| 199 |
# validation on uninstalled packages) |
| 200 |
valid <- renv_snapshot_validate(project, new, libpaths) |
| 201 |
renv_snapshot_validate_report(valid, prompt, force) |
| 202 |
|
| 203 |
# get prior lockfile state; be robust against invalid lockfiles |
| 204 |
old <- tryCatch( |
| 205 |
if (file.exists(lockfile)) renv_lockfile_read(lockfile), |
| 206 |
error = function(cnd) { |
| 207 |
extra <- "The report below will omit lockfile package versions." |
| 208 |
message <- paste(conditionMessage(cnd), extra, sep = "\n") |
| 209 |
warning(message, call. = FALSE) |
| 210 |
list() |
| 211 |
} |
| 212 |
) |
| 213 |
|
| 214 |
if (length(old)) { |
| 215 |
|
| 216 |
# preserve records from alternate OSes in lockfile |
| 217 |
alt <- renv_snapshot_preserve(old, new) |
| 218 |
|
| 219 |
# check if there are any changes in the lockfile |
| 220 |
diff <- renv_lockfile_diff(old, alt) |
| 221 |
if (empty(diff)) { |
| 222 |
writef("- The lockfile is already up to date.") |
| 223 |
return(renv_snapshot_successful(alt, prompt, project)) |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
# update new reference |
| 229 |
new <- alt |
| 230 |
|
| 231 |
# if we're only updating the lockfile, then merge any missing records |
| 232 |
# from 'old' back into 'new' |
| 233 |
if (update) |
| 234 |
for (package in names(old$Packages)) |
| 235 |
new$Packages[[package]] <- new$Packages[[package]] %||% old$Packages[[package]] |
| 236 |
|
| 237 |
# report actions to the user |
| 238 |
actions <- renv_lockfile_diff_packages(old, new) |
| 239 |
if (prompt || renv_verbose()) |
| 240 |
renv_snapshot_report_actions(actions, old, new) |
| 241 |
|
| 242 |
# request user confirmation |
| 243 |
cancel_if(length(actions) && file.exists(lockfile) && prompt && !proceed()) |
| 244 |
|
| 245 |
# write it out |
| 246 |
ensure_parent_directory(lockfile) |
| 247 |
renv_lockfile_write(new, file = lockfile) |
| 248 |
|
| 249 |
# ensure the lockfile is .Rbuildignore-d |
| 250 |
renv_infrastructure_write_rbuildignore(project) |
| 251 |
|
| 252 |
# ensure the activate script is up-to-date |
| 253 |
renv_infrastructure_write_activate(project, create = FALSE) |
| 254 |
|
| 255 |
# return new records |
| 256 |
renv_snapshot_successful(new, prompt, project) |
| 257 |
} |
| 258 |
|
| 259 |
renv_snapshot_preserve <- function(old, new) { |
| 260 |
records <- filter(old$Packages, renv_snapshot_preserve_impl) |
| 261 |
if (length(records)) |
| 262 |
new$Packages[names(records)] <- records |
| 263 |
new |
| 264 |
} |
| 265 |
|
| 266 |
renv_snapshot_preserve_impl <- function(record) { |
| 267 |
|
| 268 |
ostype <- tolower(record[["OS_type"]] %||% "") |
| 269 |
if (!nzchar(ostype)) |
| 270 |
return(FALSE) |
| 271 |
|
| 272 |
altos <- if (renv_platform_unix()) "windows" else "unix" |
| 273 |
identical(ostype, altos) |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
renv_snapshot_preflight <- function(project, libpaths) { |
| 278 |
lapply(libpaths, renv_snapshot_preflight_impl, project = project) |
| 279 |
} |
| 280 |
|
| 281 |
renv_snapshot_preflight_impl <- function(project, library) { |
| 282 |
renv_snapshot_preflight_library_exists(project, library) |
| 283 |
} |
| 284 |
|
| 285 |
renv_snapshot_preflight_library_exists <- function(project, library) { |
| 286 |
|
| 287 |
# check that we have a directory |
| 288 |
type <- renv_file_type(library, symlinks = FALSE) |
| 289 |
if (type == "directory") |
| 290 |
return(TRUE) |
| 291 |
|
| 292 |
# if the file exists but isn't a directory, fail |
| 293 |
if (nzchar(type)) { |
| 294 |
fmt <- "library '%s' exists but is not a directory" |
| 295 |
stopf(fmt, renv_path_aliased(library)) |
| 296 |
} |
| 297 |
|
| 298 |
# the directory doesn't exist; perhaps the user hasn't called init |
| 299 |
if (identical(library, renv_paths_library(project = project))) { |
| 300 |
fmt <- "project '%s' has no private library -- have you called `renv::init()`?" |
| 301 |
stopf(fmt, renv_path_aliased(project)) |
| 302 |
} |
| 303 |
|
| 304 |
# user tried to snapshot arbitrary but missing path |
| 305 |
fmt <- "library '%s' does not exist; cannot proceed" |
| 306 |
stopf(fmt, renv_path_aliased(library)) |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
renv_snapshot_validate <- function(project, lockfile, libpaths) { |
| 311 |
|
| 312 |
# allow user to disable snapshot validation, just in case |
| 313 |
enabled <- config$snapshot.validate() |
| 314 |
if (!enabled) |
| 315 |
return(TRUE) |
| 316 |
|
| 317 |
methods <- list( |
| 318 |
renv_snapshot_validate_bioconductor, |
| 319 |
renv_snapshot_validate_dependencies_available, |
| 320 |
renv_snapshot_validate_dependencies_compatible, |
| 321 |
renv_snapshot_validate_sources |
| 322 |
) |
| 323 |
|
| 324 |
ok <- map_lgl(methods, function(method) { |
| 325 |
tryCatch( |
| 326 |
method(project, lockfile, libpaths), |
| 327 |
error = function(e) { warning(e); FALSE } |
| 328 |
) |
| 329 |
}) |
| 330 |
|
| 331 |
all(ok) |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
renv_snapshot_validate_report <- function(valid, prompt, force) { |
| 336 |
|
| 337 |
# nothing to do if everything is valid |
| 338 |
if (valid) { |
| 339 |
dlog("snapshot", "passed pre-flight validation checks") |
| 340 |
return(TRUE) |
| 341 |
} |
| 342 |
|
| 343 |
# if we're forcing snapshot, ignore the failures |
| 344 |
if (force) { |
| 345 |
dlog("snapshot", "ignoring error in pre-flight validation checks as 'force = TRUE'") |
| 346 |
return(TRUE) |
| 347 |
} |
| 348 |
|
| 349 |
# if we were called during init, ignore failures |
| 350 |
if (the$init_running) { |
| 351 |
dlog("snapshot", "called during init; ignoring error in pre-flight validation checks") |
| 352 |
return(TRUE) |
| 353 |
} |
| 354 |
|
| 355 |
# in interactive sessions, if 'prompt' is set, then ask the user |
| 356 |
# if they would like to proceed |
| 357 |
if (interactive() && !testing() && prompt) { |
| 358 |
cancel_if(!proceed()) |
| 359 |
return(TRUE) |
| 360 |
} |
| 361 |
|
| 362 |
# otherwise, bail on error (need to use 'force = TRUE') |
| 363 |
stop("aborting snapshot due to pre-flight validation failure") |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
# nocov start |
| 368 |
renv_snapshot_validate_bioconductor <- function(project, lockfile, libpaths) { |
| 369 |
|
| 370 |
ok <- TRUE |
| 371 |
|
| 372 |
# check whether any packages are installed from Bioconductor |
| 373 |
records <- renv_lockfile_records(lockfile) |
| 374 |
sources <- extract_chr(records, "Source") |
| 375 |
if (!"Bioconductor" %in% sources) |
| 376 |
return(ok) |
| 377 |
|
| 378 |
# check for BiocManager or BiocInstaller |
| 379 |
package <- renv_bioconductor_manager() |
| 380 |
if (!package %in% names(records)) { |
| 381 |
|
| 382 |
text <- c( |
| 383 |
"One or more Bioconductor packages are used in your project,", |
| 384 |
"but the %s package is not available.", |
| 385 |
"", |
| 386 |
"Consider installing %s before snapshot.", |
| 387 |
"" |
| 388 |
) |
| 389 |
caution(text, package) |
| 390 |
|
| 391 |
ok <- FALSE |
| 392 |
} |
| 393 |
|
| 394 |
# check that Bioconductor packages are from correct release |
| 395 |
version <- |
| 396 |
lockfile$Bioconductor$Version %||% |
| 397 |
renv_bioconductor_version(project = project) |
| 398 |
|
| 399 |
biocrepos <- renv_bioconductor_repos(version = version) |
| 400 |
renv_scope_options(repos = biocrepos) |
| 401 |
|
| 402 |
# collect Bioconductor records |
| 403 |
bioc <- records %>% |
| 404 |
filter(function(record) renv_record_source(record) == "bioconductor") %>% |
| 405 |
map(function(record) record[c("Package", "Version")]) %>% |
| 406 |
bind() |
| 407 |
|
| 408 |
# collect latest versions of these packages |
| 409 |
bioc$Latest <- vapply(bioc$Package, function(package) { |
| 410 |
entry <- catch(renv_available_packages_latest(package)) |
| 411 |
if (inherits(entry, "error")) |
| 412 |
return("<NA>") |
| 413 |
entry$Version |
| 414 |
}, FUN.VALUE = character(1)) |
| 415 |
|
| 416 |
# check for version mismatches (allow mismatch in minor version) |
| 417 |
bioc$Mismatch <- mapply(function(current, latest) { |
| 418 |
|
| 419 |
if (identical(latest, "<NA>")) |
| 420 |
return(TRUE) |
| 421 |
|
| 422 |
current <- renv_version_maj_min(current) |
| 423 |
latest <- renv_version_maj_min(latest) |
| 424 |
current != latest |
| 425 |
|
| 426 |
}, bioc$Version, bioc$Latest) |
| 427 |
|
| 428 |
bad <- bioc[bioc$Mismatch, ] |
| 429 |
if (nrow(bad)) { |
| 430 |
|
| 431 |
fmt <- "%s [installed %s != latest %s]" |
| 432 |
msg <- sprintf(fmt, format(bad$Package), format(bad$Version), bad$Latest) |
| 433 |
bulletin( |
| 434 |
"The following Bioconductor packages appear to be from a separate Bioconductor release:", |
| 435 |
msg, |
| 436 |
c( |
| 437 |
"renv may be unable to restore these packages.", |
| 438 |
paste("Bioconductor version:", version) |
| 439 |
) |
| 440 |
) |
| 441 |
|
| 442 |
ok <- FALSE |
| 443 |
} |
| 444 |
|
| 445 |
ok |
| 446 |
|
| 447 |
} |
| 448 |
# nocov end |
| 449 |
|
| 450 |
renv_snapshot_validate_dependencies_available <- function(project, lockfile, libpaths) { |
| 451 |
|
| 452 |
# use library to collect package dependency versions |
| 453 |
records <- renv_lockfile_records(lockfile) |
| 454 |
packages <- extract_chr(records, "Package") |
| 455 |
locs <- find.package(packages, lib.loc = libpaths, quiet = TRUE) |
| 456 |
deps <- bapply(locs, renv_dependencies_discover_description) |
| 457 |
if (empty(deps)) |
| 458 |
return(TRUE) |
| 459 |
|
| 460 |
splat <- split(deps, deps$Package) |
| 461 |
|
| 462 |
# exclude base R packages |
| 463 |
splat <- splat[renv_vector_diff(names(splat), renv_packages_base())] |
| 464 |
|
| 465 |
# check for required packages not currently installed |
| 466 |
requested <- names(splat) |
| 467 |
missing <- renv_vector_diff(requested, packages) |
| 468 |
if (empty(missing)) |
| 469 |
return(TRUE) |
| 470 |
|
| 471 |
# exclude ignored packages |
| 472 |
missing <- renv_vector_diff(missing, settings$ignored.packages(project = project)) |
| 473 |
if (empty(missing)) |
| 474 |
return(TRUE) |
| 475 |
|
| 476 |
usedby <- map_chr(missing, function(package) { |
| 477 |
|
| 478 |
revdeps <- sort(unique(basename(deps$Source)[deps$Package == package])) |
| 479 |
|
| 480 |
items <- revdeps; limit <- 3L |
| 481 |
if (length(revdeps) > limit) { |
| 482 |
rest <- length(revdeps) - limit |
| 483 |
suffix <- paste("and", length(revdeps) - 3L, plural("other", rest)) |
| 484 |
items <- c(revdeps[seq_len(limit)], suffix) |
| 485 |
} |
| 486 |
|
| 487 |
paste(items, collapse = ", ") |
| 488 |
|
| 489 |
}) |
| 490 |
|
| 491 |
bulletin( |
| 492 |
"The following required packages are not installed:", |
| 493 |
sprintf("%s [required by %s]", format(missing), usedby), |
| 494 |
"Consider reinstalling these packages before snapshotting the lockfile." |
| 495 |
) |
| 496 |
|
| 497 |
FALSE |
| 498 |
|
| 499 |
} |
| 500 |
|
| 501 |
renv_snapshot_validate_dependencies_compatible <- function(project, lockfile, libpaths) { |
| 502 |
|
| 503 |
# use library to collect package dependency versions |
| 504 |
records <- renv_lockfile_records(lockfile) |
| 505 |
packages <- extract_chr(records, "Package") |
| 506 |
locs <- find.package(packages, lib.loc = libpaths, quiet = TRUE) |
| 507 |
deps <- bapply(locs, renv_dependencies_discover_description) |
| 508 |
if (empty(deps)) |
| 509 |
return(TRUE) |
| 510 |
|
| 511 |
splat <- split(deps, deps$Package) |
| 512 |
|
| 513 |
# exclude base R packages |
| 514 |
splat <- splat[renv_vector_diff(names(splat), renv_packages_base())] |
| 515 |
|
| 516 |
# collapse requirements for each package |
| 517 |
bad <- enumerate(splat, function(package, requirements) { |
| 518 |
|
| 519 |
# skip NULL records (should be handled above) |
| 520 |
record <- records[[package]] |
| 521 |
if (is.null(record)) |
| 522 |
return(NULL) |
| 523 |
|
| 524 |
version <- record$Version |
| 525 |
|
| 526 |
# drop packages without explicit version requirement |
| 527 |
requirements <- requirements[nzchar(requirements$Require), ] |
| 528 |
if (nrow(requirements) == 0) |
| 529 |
return(NULL) |
| 530 |
|
| 531 |
# add in requested version |
| 532 |
requirements$Requested <- version |
| 533 |
|
| 534 |
# generate expressions to evaluate |
| 535 |
fmt <- "package_version('%s') %s package_version('%s')" |
| 536 |
code <- with(requirements, sprintf(fmt, Requested, Require, Version)) |
| 537 |
parsed <- parse(text = code) |
| 538 |
ok <- map_lgl(parsed, eval, envir = baseenv()) |
| 539 |
|
| 540 |
# return requirements that weren't satisfied |
| 541 |
requirements[!ok, ] |
| 542 |
|
| 543 |
}) |
| 544 |
|
| 545 |
bad <- bind(bad) |
| 546 |
if (empty(bad)) |
| 547 |
return(TRUE) |
| 548 |
|
| 549 |
package <- basename(bad$Source) |
| 550 |
requires <- sprintf("%s (%s %s)", bad$Package, bad$Require, bad$Version) |
| 551 |
request <- bad$Requested |
| 552 |
|
| 553 |
fmt <- "%s requires %s, but version %s is installed" |
| 554 |
txt <- sprintf(fmt, format(package), format(requires), format(request)) |
| 555 |
bulletin( |
| 556 |
"The following package(s) have unsatisfied dependencies:", |
| 557 |
txt, |
| 558 |
"Consider updating the required dependencies as appropriate." |
| 559 |
) |
| 560 |
|
| 561 |
FALSE |
| 562 |
|
| 563 |
} |
| 564 |
|
| 565 |
renv_snapshot_validate_sources <- function(project, lockfile, libpaths) { |
| 566 |
records <- renv_lockfile_records(lockfile) |
| 567 |
renv_check_unknown_source(records, project) |
| 568 |
} |
| 569 |
|
| 570 |
# NOTE: if packages are found in multiple libraries, |
| 571 |
# then the first package found in the library paths is |
| 572 |
# kept and others are discarded |
| 573 |
renv_snapshot_libpaths <- function(libpaths = NULL, |
| 574 |
project = NULL) |
| 575 |
{ |
| 576 |
dynamic( |
| 577 |
key = list(libpaths = libpaths, project = project), |
| 578 |
value = renv_snapshot_libpaths_impl(libpaths, project) |
| 579 |
) |
| 580 |
} |
| 581 |
|
| 582 |
renv_snapshot_libpaths_impl <- function(libpaths = NULL, |
| 583 |
project = NULL) |
| 584 |
{ |
| 585 |
records <- uapply( |
| 586 |
libpaths, |
| 587 |
renv_snapshot_library, |
| 588 |
project = project |
| 589 |
) |
| 590 |
|
| 591 |
dupes <- duplicated(names(records)) |
| 592 |
records[!dupes] |
| 593 |
} |
| 594 |
|
| 595 |
renv_snapshot_library <- function(library = NULL, |
| 596 |
records = TRUE, |
| 597 |
project = NULL) |
| 598 |
{ |
| 599 |
# list packages in the library |
| 600 |
library <- renv_path_normalize(library %||% renv_libpaths_active()) |
| 601 |
paths <- list.files(library, full.names = TRUE) |
| 602 |
|
| 603 |
# remove 'base' packages |
| 604 |
paths <- paths[!basename(paths) %in% renv_packages_base()] |
| 605 |
|
| 606 |
# remove ignored packages |
| 607 |
ignored <- renv_project_ignored_packages(project = project) |
| 608 |
paths <- paths[!basename(paths) %in% ignored] |
| 609 |
|
| 610 |
# remove paths that are not valid package names |
| 611 |
pattern <- sprintf("^%s$", .standard_regexps()$valid_package_name) |
| 612 |
paths <- paths[grep(pattern, basename(paths))] |
| 613 |
|
| 614 |
# validate the remaining set of packages |
| 615 |
valid <- renv_snapshot_check(paths) |
| 616 |
|
| 617 |
# remove duplicates (so only first package entry discovered in library wins) |
| 618 |
duplicated <- duplicated(basename(valid)) |
| 619 |
packages <- valid[!duplicated] |
| 620 |
|
| 621 |
# early exit if we're just collecting the list of packages |
| 622 |
if (!records) |
| 623 |
return(basename(packages)) |
| 624 |
|
| 625 |
# snapshot description files |
| 626 |
descriptions <- file.path(packages, "DESCRIPTION") |
| 627 |
records <- lapply(descriptions, compose(catch, renv_snapshot_description)) |
| 628 |
names(records) <- basename(packages) |
| 629 |
|
| 630 |
# report any snapshot failures |
| 631 |
broken <- filter(records, inherits, what = "error") |
| 632 |
if (length(broken)) { |
| 633 |
|
| 634 |
messages <- map_chr(broken, conditionMessage) |
| 635 |
text <- sprintf("'%s': %s", names(broken), messages) |
| 636 |
bulletin( |
| 637 |
"renv was unable to snapshot the following packages:", |
| 638 |
text, |
| 639 |
"These packages will likely need to be repaired and / or reinstalled." |
| 640 |
) |
| 641 |
|
| 642 |
stopf("snapshot of library %s failed", renv_path_pretty(library)) |
| 643 |
|
| 644 |
} |
| 645 |
|
| 646 |
# name results and return |
| 647 |
names(records) <- map_chr(records, `[[`, "Package") |
| 648 |
records |
| 649 |
|
| 650 |
} |
| 651 |
|
| 652 |
renv_snapshot_check <- function(paths) { |
| 653 |
|
| 654 |
paths <- grep("00LOCK", paths, invert = TRUE, value = TRUE) |
| 655 |
paths <- renv_snapshot_check_broken_link(paths) |
| 656 |
paths <- renv_snapshot_check_tempfile(paths) |
| 657 |
paths <- renv_snapshot_check_missing_description(paths) |
| 658 |
paths |
| 659 |
|
| 660 |
} |
| 661 |
|
| 662 |
renv_snapshot_check_broken_link <- function(paths) { |
| 663 |
|
| 664 |
broken <- !file.exists(paths) |
| 665 |
if (!any(broken)) |
| 666 |
return(paths) |
| 667 |
|
| 668 |
bulletin( |
| 669 |
"The following package(s) have broken symlinks into the cache:", |
| 670 |
basename(paths)[broken], |
| 671 |
"Use `renv::repair()` to try and reinstall these packages." |
| 672 |
) |
| 673 |
|
| 674 |
paths[!broken] |
| 675 |
|
| 676 |
} |
| 677 |
|
| 678 |
renv_snapshot_check_tempfile <- function(paths) { |
| 679 |
|
| 680 |
names <- basename(paths) |
| 681 |
missing <- grepl("^file(?:\\w){12}", names) |
| 682 |
if (!any(missing)) |
| 683 |
return(paths) |
| 684 |
|
| 685 |
bulletin( |
| 686 |
"The following folder(s) appear to be left-over temporary directories:", |
| 687 |
map_chr(paths[missing], renv_path_pretty), |
| 688 |
"Consider removing these folders from your R library." |
| 689 |
) |
| 690 |
|
| 691 |
paths[!missing] |
| 692 |
|
| 693 |
} |
| 694 |
|
| 695 |
renv_snapshot_check_missing_description <- function(paths) { |
| 696 |
|
| 697 |
desc <- file.path(paths, "DESCRIPTION") |
| 698 |
missing <- !file.exists(desc) |
| 699 |
if (!any(missing)) |
| 700 |
return(paths) |
| 701 |
|
| 702 |
bulletin( |
| 703 |
"The following package(s) are missing their DESCRIPTION files:", |
| 704 |
sprintf("%s [%s]", format(basename(paths[missing])), paths[missing]), |
| 705 |
c( |
| 706 |
"These may be left over from a prior, failed installation attempt.", |
| 707 |
"Consider removing or reinstalling these packages." |
| 708 |
) |
| 709 |
) |
| 710 |
|
| 711 |
paths[!missing] |
| 712 |
|
| 713 |
} |
| 714 |
|
| 715 |
renv_snapshot_description <- function(path = NULL, package = NULL) { |
| 716 |
|
| 717 |
# resolve path |
| 718 |
path <- path %||% renv_package_find(package, lib.loc = renv_libpaths_all()) |
| 719 |
if (!nzchar(path)) |
| 720 |
stopf("package '%s' is not installed", package) |
| 721 |
|
| 722 |
# read and snapshot DESCRIPTION file |
| 723 |
dcf <- renv_description_read(path, package) |
| 724 |
renv_snapshot_description_impl(dcf, path) |
| 725 |
|
| 726 |
} |
| 727 |
|
| 728 |
renv_snapshot_description_impl <- function(dcf, path = NULL) { |
| 729 |
|
| 730 |
version <- |
| 731 |
getOption("renv.lockfile.version") %||% |
| 732 |
Sys.getenv("RENV_LOCKFILE_VERSION", unset = 2L) |
| 733 |
|
| 734 |
if (version == 1L) |
| 735 |
renv_snapshot_description_impl_v1(dcf, path) |
| 736 |
else if (version == 2L) |
| 737 |
renv_snapshot_description_impl_v2(dcf, path) |
| 738 |
else |
| 739 |
stopf("unsupported lockfile version '%s'", format(version)) |
| 740 |
|
| 741 |
} |
| 742 |
|
| 743 |
renv_snapshot_description_impl_v1 <- function(dcf, path = NULL) { |
| 744 |
|
| 745 |
# figure out the package source |
| 746 |
source <- renv_snapshot_description_source(dcf) |
| 747 |
dcf[names(source)] <- source |
| 748 |
|
| 749 |
# check for required fields |
| 750 |
required <- c("Package", "Version", "Source") |
| 751 |
missing <- renv_vector_diff(required, names(dcf)) |
| 752 |
if (length(missing)) { |
| 753 |
fmt <- "required fields %s missing from DESCRIPTION at path '%s'" |
| 754 |
stopf(fmt, paste(dQuote(missing), collapse = ", "), path %||% "<unknown>") |
| 755 |
} |
| 756 |
|
| 757 |
# if this is a standard remote for a bioconductor package, |
| 758 |
# remove the other remote fields |
| 759 |
bioc <- |
| 760 |
nzchar(dcf[["biocViews"]] %||% "") && |
| 761 |
identical(dcf[["RemoteType"]], "standard") |
| 762 |
|
| 763 |
if (bioc) { |
| 764 |
fields <- grep("^Remote(?!s)", names(dcf), perl = TRUE, invert = TRUE) |
| 765 |
dcf <- dcf[fields] |
| 766 |
} |
| 767 |
|
| 768 |
# generate a hash if we can |
| 769 |
dcf[["Hash"]] <- if (is.null(path)) |
| 770 |
renv_hash_record(dcf) |
| 771 |
else |
| 772 |
renv_hash_description(path) |
| 773 |
|
| 774 |
# generate a Requirements field -- primarily for use by 'pak' |
| 775 |
fields <- c("Depends", "Imports", "LinkingTo") |
| 776 |
deps <- bind(map(dcf[fields], renv_description_parse_field)) |
| 777 |
all <- unique(csort(unlist(deps$Package))) |
| 778 |
dcf[["Requirements"]] <- all |
| 779 |
|
| 780 |
# get remotes fields |
| 781 |
remotes <- local({ |
| 782 |
|
| 783 |
# if this seems to be a cran-like record, only keep remotes |
| 784 |
# when RemoteSha appears to be a hash (e.g. for r-universe) |
| 785 |
# note that RemoteSha may be a package version when installed |
| 786 |
# by e.g. pak |
| 787 |
if (renv_record_cranlike(dcf)) { |
| 788 |
sha <- dcf[["RemoteSha"]] |
| 789 |
if (is.null(sha) || nchar(sha) < 40L) |
| 790 |
return(character()) |
| 791 |
} |
| 792 |
|
| 793 |
# grab the relevant remotes |
| 794 |
git <- grep("^git", names(dcf), value = TRUE) |
| 795 |
remotes <- grep("^Remote(?!s)", names(dcf), perl = TRUE, value = TRUE) |
| 796 |
|
| 797 |
# don't include 'RemoteRef' if it's a non-informative remote |
| 798 |
if (identical(dcf[["RemoteRef"]], "HEAD")) |
| 799 |
remotes <- setdiff(remotes, "RemoteRef") |
| 800 |
|
| 801 |
c(git, remotes) |
| 802 |
|
| 803 |
}) |
| 804 |
|
| 805 |
# only keep relevant fields |
| 806 |
extra <- c("Repository", "OS_type") |
| 807 |
all <- c(required, extra, remotes, "Requirements", "Hash") |
| 808 |
keep <- renv_vector_intersect(all, names(dcf)) |
| 809 |
|
| 810 |
# return as list |
| 811 |
as.list(dcf[keep]) |
| 812 |
|
| 813 |
} |
| 814 |
|
| 815 |
renv_snapshot_description_impl_v2 <- function(dcf, path) { |
| 816 |
|
| 817 |
# figure out the package source |
| 818 |
source <- renv_snapshot_description_source(dcf) |
| 819 |
dcf[names(source)] <- source |
| 820 |
|
| 821 |
# check for required fields |
| 822 |
required <- c("Package", "Version", "Source") |
| 823 |
missing <- renv_vector_diff(required, names(dcf)) |
| 824 |
if (length(missing)) { |
| 825 |
fmt <- "required fields %s missing from DESCRIPTION at path '%s'" |
| 826 |
stopf(fmt, paste(dQuote(missing), collapse = ", "), path %||% "<unknown>") |
| 827 |
} |
| 828 |
|
| 829 |
# if this is a standard remote for a bioconductor package, |
| 830 |
# remove the other remote fields |
| 831 |
bioc <- |
| 832 |
nzchar(dcf[["biocViews"]] %||% "") && |
| 833 |
identical(dcf[["RemoteType"]], "standard") |
| 834 |
|
| 835 |
if (bioc) { |
| 836 |
fields <- grep("^Remote(?!s)", names(dcf), perl = TRUE, invert = TRUE) |
| 837 |
dcf <- dcf[fields] |
| 838 |
} |
| 839 |
|
| 840 |
# drop fields that normally only appear in binary packages, |
| 841 |
# or fields which might differ from user to user, or might |
| 842 |
# differ depending on the mirror used for publication |
| 843 |
ignore <- c("Archs", "Built", "Date/Publication", "File", "MD5sum", "Packaged") |
| 844 |
dcf[ignore] <- NULL |
| 845 |
|
| 846 |
# drop remote fields for cranlike remotes |
| 847 |
if (renv_record_cranlike(dcf)) { |
| 848 |
sha <- dcf[["RemoteSha"]] |
| 849 |
if (is.null(sha) || nchar(sha) < 40L) { |
| 850 |
remotes <- grep("^Remote", names(dcf), perl = TRUE, value = TRUE) |
| 851 |
dcf[remotes] <- NULL |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
# drop the old Github remote fields |
| 856 |
github <- grepl("^Github", names(dcf), perl = TRUE) |
| 857 |
dcf <- dcf[!github] |
| 858 |
|
| 859 |
# split fields which are normally declared as lists of packages |
| 860 |
depfields <- c("Depends", "Imports", "Suggests", "LinkingTo", "Enhances") |
| 861 |
for (depfield in depfields) { |
| 862 |
if (!is.null(dcf[[depfield]])) { |
| 863 |
fields <- strsplit(dcf[[depfield]], ",", fixed = TRUE) |
| 864 |
dcf[[depfield]] <- as.list(trimws(fields[[1L]])) |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
# reorganize fields a bit |
| 869 |
dcf <- dcf[c(required, setdiff(names(dcf), required))] |
| 870 |
|
| 871 |
# return as list |
| 872 |
as.list(dcf) |
| 873 |
|
| 874 |
} |
| 875 |
|
| 876 |
renv_snapshot_description_source_custom <- function(dcf) { |
| 877 |
|
| 878 |
# only proceed for cranlike remotes |
| 879 |
if (!renv_record_cranlike(dcf)) |
| 880 |
return(NULL) |
| 881 |
|
| 882 |
# check for a declared repository URL |
| 883 |
remoterepos <- dcf[["RemoteRepos"]] |
| 884 |
if (is.null(remoterepos)) |
| 885 |
return(NULL) |
| 886 |
|
| 887 |
# if this package appears to be installed from Bioconductor, skip |
| 888 |
if (nzchar(dcf[["biocViews"]] %||% "")) |
| 889 |
return(NULL) |
| 890 |
|
| 891 |
# if the declared repository appears to be a CRAN mirror, skip it |
| 892 |
mirrors <- renv_cran_mirrors() |
| 893 |
if (any(renv_repos_matches(remoterepos, mirrors))) |
| 894 |
return(NULL) |
| 895 |
|
| 896 |
# if this package appears to have been installed from a |
| 897 |
# repository which we have knowledge of, skip |
| 898 |
repos <- as.list(getOption("repos")) |
| 899 |
repository <- dcf[["Repository"]] |
| 900 |
if (!is.null(repository) && repository %in% names(repos)) |
| 901 |
return(NULL) |
| 902 |
|
| 903 |
# check whether the declared repository matches one of the |
| 904 |
# repositories that are currently in use; if so, skip it |
| 905 |
# |
| 906 |
# we explicitly ignore 'CRAN' as a repository name here, since older |
| 907 |
# versions of renv may have erroneously marked packages installed from |
| 908 |
# other package repositories as 'CRAN' |
| 909 |
# |
| 910 |
# https://github.com/rstudio/renv/issues/2104 |
| 911 |
name <- dcf[["RemoteReposName"]] |
| 912 |
declared <- if (is.null(name) || identical(name, "CRAN")) |
| 913 |
renv_repos_matches(remoterepos, repos) |
| 914 |
else |
| 915 |
name %in% names(repos) |
| 916 |
|
| 917 |
if (declared) |
| 918 |
return(NULL) |
| 919 |
|
| 920 |
list(Source = "Repository", Repository = remoterepos) |
| 921 |
|
| 922 |
} |
| 923 |
|
| 924 |
renv_snapshot_description_source <- function(dcf) { |
| 925 |
|
| 926 |
# check for packages installed from a repository not currently |
| 927 |
# encoded as part of the user's repository option, and include if required |
| 928 |
source <- renv_snapshot_description_source_custom(dcf) |
| 929 |
if (!is.null(source)) |
| 930 |
return(source) |
| 931 |
|
| 932 |
# check for a custom declared remote type |
| 933 |
if (!renv_record_cranlike(dcf)) { |
| 934 |
type <- dcf[["RemoteType"]] %||% "standard" |
| 935 |
return(list(Source = alias(type))) |
| 936 |
} |
| 937 |
|
| 938 |
# packages from Bioconductor are normally tagged with a 'biocViews' entry; |
| 939 |
# use that to infer a Bioconductor source |
| 940 |
if (nzchar(dcf[["biocViews"]] %||% "")) |
| 941 |
return(list(Source = "Bioconductor")) |
| 942 |
|
| 943 |
# check for a declared repository |
| 944 |
repository <- dcf[["RemoteReposName"]] %||% dcf[["Repository"]] |
| 945 |
if (!is.null(repository)) |
| 946 |
return(list(Source = "Repository", Repository = repository)) |
| 947 |
|
| 948 |
# check for a valid package name |
| 949 |
package <- dcf[["Package"]] |
| 950 |
if (is.null(package)) |
| 951 |
return(list(Source = "unknown")) |
| 952 |
|
| 953 |
# if this is running as part of the synchronization check, skip CRAN queries |
| 954 |
# https://github.com/rstudio/renv/issues/812 |
| 955 |
if (the$project_synchronized_check_running) |
| 956 |
return(list(Source = "unknown")) |
| 957 |
|
| 958 |
# check to see if this is a base / recommended package; if so, assume that |
| 959 |
# the package was installed from CRAN at this point |
| 960 |
# |
| 961 |
# normally these would be caught by the 'Repository' check above, but it |
| 962 |
# seems like, in some cases, base / recommended packages might be installed |
| 963 |
# without those available |
| 964 |
# |
| 965 |
# https://github.com/rstudio/renv/issues/1948#issuecomment-2245134768 |
| 966 |
pkgs <- installed_packages( |
| 967 |
lib.loc = c(.Library, .Library.site), |
| 968 |
priority = c("base", "recommended"), |
| 969 |
field = "Package" |
| 970 |
) |
| 971 |
|
| 972 |
if (package %in% pkgs) |
| 973 |
return(list(Source = "Repository", Repository = "CRAN")) |
| 974 |
|
| 975 |
# NOTE: this is sort of a hack that allows renv to declare packages which |
| 976 |
# appear to be installed from sources, but are actually available on the |
| 977 |
# active R package repositories, as though they were retrieved from that |
| 978 |
# repository. however, this is often what users intend, especially if |
| 979 |
# they haven't configured their repository to tag the packages it makes |
| 980 |
# available with the 'Repository:' field in the DESCRIPTION file. |
| 981 |
# |
| 982 |
# still, this has the awkward side-effect of a package's source potentially |
| 983 |
# depending on what repositories happen to be active at the time of snapshot, |
| 984 |
# so it'd be nice to tighten up the logic here if possible |
| 985 |
# |
| 986 |
# NOTE: local sources are also searched here as part of finding the 'latest' |
| 987 |
# available package, so we need to handle local packages discovered here |
| 988 |
tryCatch( |
| 989 |
renv_snapshot_description_source_hack(package, dcf), |
| 990 |
error = function(e) list(Source = "unknown") |
| 991 |
) |
| 992 |
|
| 993 |
} |
| 994 |
|
| 995 |
renv_snapshot_description_source_hack <- function(package, dcf) { |
| 996 |
|
| 997 |
# check cellar |
| 998 |
for (type in renv_package_pkgtypes()) { |
| 999 |
cellar <- renv_available_packages_cellar(type) |
| 1000 |
if (package %in% cellar$Package) |
| 1001 |
return(list(Source = "Cellar")) |
| 1002 |
} |
| 1003 |
|
| 1004 |
# check available packages |
| 1005 |
latest <- catch(renv_available_packages_latest(package)) |
| 1006 |
if (is.null(latest) || inherits(latest, "error")) |
| 1007 |
return(list(Source = "unknown")) |
| 1008 |
|
| 1009 |
# check version; use unknown if it's too new |
| 1010 |
if (renv_version_gt(dcf[["Version"]], latest[["Version"]])) |
| 1011 |
return(list(Source = "unknown")) |
| 1012 |
|
| 1013 |
# ok, this package appears to be from a package repository |
| 1014 |
list(Source = "Repository", Repository = latest[["Repository"]]) |
| 1015 |
|
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
# nocov start |
| 1020 |
renv_snapshot_report_actions <- function(actions, old, new) { |
| 1021 |
|
| 1022 |
if (!renv_verbose()) |
| 1023 |
return(invisible()) |
| 1024 |
|
| 1025 |
if (length(actions)) { |
| 1026 |
lhs <- renv_lockfile_records(old) |
| 1027 |
rhs <- renv_lockfile_records(new) |
| 1028 |
renv_pretty_print_records_pair( |
| 1029 |
"The following package(s) will be updated in the lockfile:", |
| 1030 |
lhs[names(lhs) %in% names(actions)], |
| 1031 |
rhs[names(rhs) %in% names(actions)] |
| 1032 |
) |
| 1033 |
} |
| 1034 |
|
| 1035 |
oldr <- old$R$Version |
| 1036 |
newr <- new$R$Version |
| 1037 |
rdiff <- renv_version_compare(oldr %||% "0", newr %||% "0") |
| 1038 |
|
| 1039 |
if (rdiff != 0L) { |
| 1040 |
n <- max(nchar(names(actions)), 0) |
| 1041 |
fmt <- paste("-", format("R", width = n), " ", "[%s -> %s]") |
| 1042 |
placeholder <- renv_record_placeholder() |
| 1043 |
msg <- sprintf(fmt, oldr %||% placeholder, newr %||% placeholder) |
| 1044 |
writef( |
| 1045 |
c("The version of R recorded in the lockfile will be updated:", msg, "") |
| 1046 |
) |
| 1047 |
} |
| 1048 |
|
| 1049 |
} |
| 1050 |
# nocov end |
| 1051 |
|
| 1052 |
# compute the package dependencies inferred for a project, |
| 1053 |
# respecting the snapshot type selected (or currently configured) |
| 1054 |
# for the associated project |
| 1055 |
renv_snapshot_dependencies <- function(project, type = NULL, dev = FALSE) { |
| 1056 |
|
| 1057 |
type <- type %||% settings$snapshot.type(project = project) |
| 1058 |
|
| 1059 |
packages <- dynamic( |
| 1060 |
list(project = project, type = type, dev = dev), |
| 1061 |
renv_snapshot_dependencies_impl(project, type, dev) |
| 1062 |
) |
| 1063 |
|
| 1064 |
if (!renv_tests_running()) |
| 1065 |
packages <- unique(c(packages, "renv")) |
| 1066 |
|
| 1067 |
packages |
| 1068 |
|
| 1069 |
} |
| 1070 |
|
| 1071 |
renv_snapshot_dependencies_impl <- function(project, type = NULL, dev = FALSE) { |
| 1072 |
|
| 1073 |
if (type %in% "all") { |
| 1074 |
packages <- installed_packages(field = "Package") |
| 1075 |
return(setdiff(packages, renv_packages_base())) |
| 1076 |
} |
| 1077 |
|
| 1078 |
if (type %in% "custom") { |
| 1079 |
filter <- renv_snapshot_filter_custom_resolve() |
| 1080 |
return(filter(project)) |
| 1081 |
} |
| 1082 |
|
| 1083 |
path <- case( |
| 1084 |
type %in% c("packrat", "implicit") ~ project, |
| 1085 |
type %in% "explicit" ~ file.path(project, "DESCRIPTION"), |
| 1086 |
~ { |
| 1087 |
fmt <- "internal error: unhandled snapshot type '%s' in %s" |
| 1088 |
stopf(fmt, type, stringify(sys.call())) |
| 1089 |
} |
| 1090 |
) |
| 1091 |
|
| 1092 |
# avoid errors when the project directory, or DESCRIPTION file, |
| 1093 |
# does not exist (imply no dependencies) |
| 1094 |
# |
| 1095 |
# https://github.com/rstudio/renv/issues/1949 |
| 1096 |
if (!file.exists(path)) |
| 1097 |
return(character()) |
| 1098 |
|
| 1099 |
# count the number of files in each directory, so we can report |
| 1100 |
# to the user if we scanned a folder containing many files |
| 1101 |
count <- integer() |
| 1102 |
|
| 1103 |
packages <- withCallingHandlers( |
| 1104 |
|
| 1105 |
renv_dependencies_impl( |
| 1106 |
path = path, |
| 1107 |
root = project, |
| 1108 |
field = "Package", |
| 1109 |
errors = config$dependency.errors(), |
| 1110 |
dev = dev |
| 1111 |
), |
| 1112 |
|
| 1113 |
# require user confirmation to proceed if there's a reported error |
| 1114 |
renv.dependencies.problems = function(cnd) { |
| 1115 |
|
| 1116 |
if (identical(config$dependency.errors(), "ignored")) |
| 1117 |
return() |
| 1118 |
|
| 1119 |
if (interactive() && !proceed()) |
| 1120 |
cancel() |
| 1121 |
|
| 1122 |
}, |
| 1123 |
|
| 1124 |
# collect information about folders containing lots of files |
| 1125 |
renv.dependencies.count = function(cnd) { |
| 1126 |
count[[cnd$data$path]] <<- cnd$data$count |
| 1127 |
}, |
| 1128 |
|
| 1129 |
# notify the user if we took a long time to discover dependencies |
| 1130 |
renv.dependencies.elapsed_time = function(cnd) { |
| 1131 |
|
| 1132 |
# only relevant for implicit-type snapshots |
| 1133 |
if (!type %in% c("packrat", "implicit")) |
| 1134 |
return() |
| 1135 |
|
| 1136 |
# check for timeout |
| 1137 |
elapsed <- cnd$data |
| 1138 |
limit <- getOption("renv.dependencies.elapsed_time_threshold", default = 10L) |
| 1139 |
if (elapsed < limit) |
| 1140 |
return() |
| 1141 |
|
| 1142 |
# tally up directories with lots of files |
| 1143 |
count <- count[order(count)] |
| 1144 |
count <- count[count >= 200] |
| 1145 |
|
| 1146 |
# report to user |
| 1147 |
ignore <- file.exists(file.path(project, ".renvignore")) |
| 1148 |
|
| 1149 |
lines <- c( |
| 1150 |
"", |
| 1151 |
if (ignore) { |
| 1152 |
"NOTE: Dependency discovery took %s during snapshot using current .renvignore specifications." |
| 1153 |
} else { |
| 1154 |
"NOTE: Dependency discovery took %s during snapshot." |
| 1155 |
}, |
| 1156 |
if (ignore) { |
| 1157 |
"Consider modifying .renvignore or switching to explicit snapshots." |
| 1158 |
} else { |
| 1159 |
"Consider using .renvignore to ignore files, or switching to explicit snapshots." |
| 1160 |
}, |
| 1161 |
"See `?renv::dependencies` for more information.", |
| 1162 |
if (length(count)) c( |
| 1163 |
"", |
| 1164 |
sprintf("- %s: %s", format(names(count)), nplural("file", count)) |
| 1165 |
), |
| 1166 |
"" |
| 1167 |
) |
| 1168 |
|
| 1169 |
# force output in this scope |
| 1170 |
renv_scope_caution(TRUE) |
| 1171 |
caution(lines, renv_difftime_format(elapsed)) |
| 1172 |
|
| 1173 |
} |
| 1174 |
|
| 1175 |
) |
| 1176 |
|
| 1177 |
unique(packages) |
| 1178 |
|
| 1179 |
} |
| 1180 |
|
| 1181 |
# compute package records from the provided library paths, |
| 1182 |
# normally to be included as part of an renv lockfile |
| 1183 |
renv_snapshot_packages <- function(packages, libpaths, project) { |
| 1184 |
|
| 1185 |
ignored <- c( |
| 1186 |
renv_packages_base(), |
| 1187 |
renv_project_ignored_packages(project = project), |
| 1188 |
if (renv_tests_running()) "renv" |
| 1189 |
) |
| 1190 |
|
| 1191 |
callback <- function(package, location, project) { |
| 1192 |
if (nzchar(location) && !package %in% ignored) |
| 1193 |
return(location) |
| 1194 |
} |
| 1195 |
|
| 1196 |
# expand package dependency tree |
| 1197 |
paths <- renv_package_dependencies( |
| 1198 |
packages = packages, |
| 1199 |
libpaths = libpaths, |
| 1200 |
callback = callback, |
| 1201 |
project = project |
| 1202 |
) |
| 1203 |
|
| 1204 |
# keep only packages with known locations |
| 1205 |
paths <- paths %>% filter(is.character) %>% filter(nzchar) |
| 1206 |
|
| 1207 |
# diagnose issues with the scanned packages |
| 1208 |
paths <- renv_snapshot_check(paths) |
| 1209 |
|
| 1210 |
# now, snapshot the remaining packages |
| 1211 |
map(paths, renv_snapshot_description) |
| 1212 |
|
| 1213 |
} |
| 1214 |
|
| 1215 |
renv_snapshot_report_missing <- function(missing, type) { |
| 1216 |
|
| 1217 |
missing <- setdiff(missing, "renv") |
| 1218 |
if (empty(missing)) |
| 1219 |
return(invisible()) |
| 1220 |
|
| 1221 |
preamble <- "The following required packages are not installed:" |
| 1222 |
|
| 1223 |
postamble <- c( |
| 1224 |
"Packages must first be installed before renv can snapshot them.", |
| 1225 |
if (type %in% "explicit") |
| 1226 |
"If these packages are no longer required, consider removing them from your DESCRIPTION file." |
| 1227 |
else |
| 1228 |
"Use `renv::dependencies()` to see where this package is used in your project." |
| 1229 |
) |
| 1230 |
|
| 1231 |
bulletin( |
| 1232 |
preamble = preamble, |
| 1233 |
values = sort(unique(missing)), |
| 1234 |
postamble = postamble |
| 1235 |
) |
| 1236 |
|
| 1237 |
# only prompt the user to install if a restart is available |
| 1238 |
restart <- findRestart("renv_recompute_records") |
| 1239 |
if (is.null(restart)) |
| 1240 |
return(invisible()) |
| 1241 |
|
| 1242 |
choices <- c( |
| 1243 |
snapshot = "Snapshot, just using the currently installed packages.", |
| 1244 |
install = "Install the packages, then snapshot.", |
| 1245 |
cancel = "Cancel, and resolve the situation on your own." |
| 1246 |
) |
| 1247 |
|
| 1248 |
choice <- menu(choices, title = "What do you want to do?") |
| 1249 |
|
| 1250 |
if (choice == "snapshot") { |
| 1251 |
# do nothing |
| 1252 |
} else if (choice == "install") { |
| 1253 |
install(include = missing, prompt = FALSE) |
| 1254 |
invokeRestart(restart) |
| 1255 |
} else { |
| 1256 |
cancel() |
| 1257 |
} |
| 1258 |
|
| 1259 |
invisible() |
| 1260 |
|
| 1261 |
} |
| 1262 |
|
| 1263 |
renv_snapshot_filter_custom_resolve <- function() { |
| 1264 |
|
| 1265 |
# check for custom filter |
| 1266 |
filter <- getOption("renv.snapshot.filter", default = NULL) |
| 1267 |
if (is.null(filter)) { |
| 1268 |
fmt <- "snapshot of type '%s' requested, but '%s' is not registered" |
| 1269 |
stopf(fmt, "custom", "renv.snapshot.filter") |
| 1270 |
} |
| 1271 |
|
| 1272 |
# allow for filter naming a function to use |
| 1273 |
if (is.character(filter)) |
| 1274 |
filter <- eval(parse(text = filter), envir = baseenv()) |
| 1275 |
|
| 1276 |
# check we got a function |
| 1277 |
if (!is.function(filter)) { |
| 1278 |
fmt <- "snapshot of type '%s' requested, but '%s' is not a function" |
| 1279 |
stopf(fmt, "custom", "renv.snapshot.filter") |
| 1280 |
} |
| 1281 |
|
| 1282 |
# return resolved function |
| 1283 |
filter |
| 1284 |
|
| 1285 |
} |
| 1286 |
|
| 1287 |
renv_snapshot_fixup <- function(records) { |
| 1288 |
|
| 1289 |
records <- renv_snapshot_fixup_renv(records) |
| 1290 |
records |
| 1291 |
|
| 1292 |
} |
| 1293 |
|
| 1294 |
renv_snapshot_fixup_renv <- function(records) { |
| 1295 |
|
| 1296 |
# don't run when testing renv |
| 1297 |
if (renv_tests_running()) |
| 1298 |
return(records) |
| 1299 |
|
| 1300 |
# check for an existing valid record |
| 1301 |
record <- records$renv |
| 1302 |
if (is.null(record)) |
| 1303 |
return(records) |
| 1304 |
|
| 1305 |
source <- renv_record_source(record) |
| 1306 |
if (source != "unknown") |
| 1307 |
return(records) |
| 1308 |
|
| 1309 |
# no valid record available; construct a synthetic one |
| 1310 |
remote <- renv_metadata_remote() |
| 1311 |
|
| 1312 |
# add it to the set of records |
| 1313 |
records$renv <- renv_remotes_resolve(remote) |
| 1314 |
|
| 1315 |
# return it |
| 1316 |
records |
| 1317 |
|
| 1318 |
} |
| 1319 |
|
| 1320 |
renv_snapshot_reprex <- function(lockfile) { |
| 1321 |
|
| 1322 |
fmt <- "<sup>Lockfile generated by renv %s.</sup>" |
| 1323 |
version <- sprintf(fmt, renv_metadata_version_friendly()) |
| 1324 |
|
| 1325 |
text <- c( |
| 1326 |
"<details style=\"margin-bottom: 10px;\">", |
| 1327 |
"<summary>Lockfile</summary>", |
| 1328 |
"```", |
| 1329 |
renv_lockfile_write(lockfile, file = NULL), |
| 1330 |
"```", |
| 1331 |
version, |
| 1332 |
"</details>" |
| 1333 |
) |
| 1334 |
|
| 1335 |
output <- paste(text, collapse = "\n") |
| 1336 |
class(output) <- "knit_asis" |
| 1337 |
attr(output, "knit_cacheable") <- NA |
| 1338 |
|
| 1339 |
output |
| 1340 |
|
| 1341 |
} |
| 1342 |
|
| 1343 |
renv_snapshot_successful <- function(records, prompt, project) { |
| 1344 |
|
| 1345 |
# update snapshot flag |
| 1346 |
the$auto_snapshot_failed <- FALSE |
| 1347 |
|
| 1348 |
# perform python snapshot on success |
| 1349 |
renv_python_snapshot(project, prompt) |
| 1350 |
|
| 1351 |
# return generated records |
| 1352 |
invisible(records) |
| 1353 |
|
| 1354 |
} |