| 1 |
|
| 2 |
renv_graph_init <- function(remotes, records = list(), project = NULL, scope = parent.frame()) { |
| 3 |
|
| 4 |
# create an environment to track resolved descriptions (avoids cycles/dupes) |
| 5 |
project <- project %||% renv_project_resolve() |
| 6 |
envir <- new.env(parent = emptyenv()) |
| 7 |
|
| 8 |
# when the user passes dependencies = TRUE/FALSE, install() sets |
| 9 |
# the$install_dependency_fields; apply extended fields (e.g. Suggests) |
| 10 |
# only to the top-level remotes, not to transitive dependencies. |
| 11 |
# when NULL, read from project settings so that e.g. Suggests is |
| 12 |
# respected when configured in settings$package.dependency.fields |
| 13 |
fields <- the$install_dependency_fields %||% { |
| 14 |
renv_dependencies_fields(project = project) |
| 15 |
} |
| 16 |
|
| 17 |
# pre-seed with project-level Remotes; these act as fallback records |
| 18 |
# so that packages specified via the project DESCRIPTION Remotes field |
| 19 |
# are resolved from the correct source (e.g. GitHub) even when the |
| 20 |
# caller doesn't explicitly include them in 'records' |
| 21 |
if (!is.null(project) && config$install.remotes()) { |
| 22 |
projrecords <- renv_project_remotes(project) |
| 23 |
for (name in names(projrecords)) |
| 24 |
if (is.null(records[[name]])) |
| 25 |
records[[name]] <- projrecords[[name]] |
| 26 |
} |
| 27 |
|
| 28 |
# phase 1: resolve top-level remotes with extended dependency fields; |
| 29 |
# BFS ensures explicit user requests take priority over implicit lookups |
| 30 |
queue <- list() |
| 31 |
for (remote in remotes) { |
| 32 |
deps <- renv_graph_resolve(remote, envir, records = records, fields = fields) |
| 33 |
queue <- c(queue, as.list(deps)) |
| 34 |
} |
| 35 |
|
| 36 |
# if any resolved description indicates Bioconductor is needed (via Source |
| 37 |
# or biocViews), activate Bioconductor repos for the rest of resolution |
| 38 |
resolved <- as.list(envir) |
| 39 |
bioc <- map_lgl(resolved, function(desc) { |
| 40 |
source <- renv_record_source(desc, normalize = TRUE) |
| 41 |
biocviews <- desc[["biocViews"]] %||% "" |
| 42 |
identical(source, "bioconductor") || nzchar(biocviews) |
| 43 |
}) |
| 44 |
|
| 45 |
if (any(bioc)) { |
| 46 |
project <- renv_restore_state(key = "project") %||% renv_project_resolve() |
| 47 |
renv_scope_bioconductor(project = project, scope = scope) |
| 48 |
} |
| 49 |
|
| 50 |
# phase 2: resolve transitive dependencies with default fields |
| 51 |
idx <- 1L |
| 52 |
while (idx <= length(queue)) { |
| 53 |
remote <- queue[[idx]] |
| 54 |
idx <- idx + 1L |
| 55 |
deps <- renv_graph_resolve(remote, envir, records = records) |
| 56 |
queue <- c(queue, as.list(deps)) |
| 57 |
} |
| 58 |
|
| 59 |
# phase 3: check version requirements; if a resolved package |
| 60 |
# doesn't satisfy constraints from other packages in the graph, |
| 61 |
# try to upgrade it to the latest available version |
| 62 |
descriptions <- as.list(envir) |
| 63 |
requirements <- renv_graph_requirements(descriptions) |
| 64 |
|
| 65 |
for (package in ls(envir = requirements)) { |
| 66 |
|
| 67 |
reqs <- requirements[[package]] |
| 68 |
|
| 69 |
version <- descriptions[[package]]$Version |
| 70 |
if (is.null(version)) |
| 71 |
next |
| 72 |
|
| 73 |
if (renv_graph_compatible(version, reqs)) |
| 74 |
next |
| 75 |
|
| 76 |
latest <- catch(renv_available_packages_latest(package)) |
| 77 |
if (inherits(latest, "error")) |
| 78 |
next |
| 79 |
|
| 80 |
desc <- descriptions[[package]] |
| 81 |
desc$Version <- latest$Version |
| 82 |
descriptions[[package]] <- desc |
| 83 |
assign(package, desc, envir = envir) |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
# warn about any remaining unsatisfied version requirements |
| 88 |
renv_graph_requirements_check(descriptions, requirements) |
| 89 |
|
| 90 |
descriptions |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
renv_graph_resolve <- function(remote, envir, records = NULL, fields = NULL, override = FALSE) { |
| 95 |
|
| 96 |
# resolve the record; use pre-resolved record if available |
| 97 |
record <- if (is.character(remote) && !is.null(records[[remote]])) |
| 98 |
records[[remote]] |
| 99 |
else |
| 100 |
renv_remotes_resolve(remote) |
| 101 |
|
| 102 |
# resolve lazy records (functions that produce the actual record) |
| 103 |
if (is.function(record)) |
| 104 |
record <- record() |
| 105 |
|
| 106 |
# skip base packages (utils, methods, etc.) -- they can't be installed |
| 107 |
package <- record$Package |
| 108 |
if (package %in% renv_packages_base()) |
| 109 |
return(character()) |
| 110 |
|
| 111 |
# skip if already resolved; because we use BFS, top-level remotes |
| 112 |
# are always resolved before transitive dependencies, so the first |
| 113 |
# resolution for a given package name wins. |
| 114 |
# |
| 115 |
# when override is TRUE (called from Remotes processing), allow |
| 116 |
# replacing default repository records with the Remotes-specified |
| 117 |
# source -- e.g. a GitHub remote should win over a CRAN record |
| 118 |
if (exists(package, envir = envir, inherits = FALSE)) { |
| 119 |
|
| 120 |
if (!override) |
| 121 |
return(character()) |
| 122 |
|
| 123 |
existing <- get(package, envir = envir, inherits = FALSE) |
| 124 |
if (is.null(existing)) |
| 125 |
return(character()) |
| 126 |
|
| 127 |
source <- renv_record_source(existing, normalize = TRUE) |
| 128 |
if (!identical(source, "repository")) |
| 129 |
return(character()) |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
# reserve the slot to prevent re-processing via dependency cycles |
| 134 |
assign(package, NULL, envir = envir) |
| 135 |
|
| 136 |
# fetch DESCRIPTION-level metadata for this record; |
| 137 |
# renv_graph_description_repository includes cellar via |
| 138 |
# renv_available_packages_latest(cellar = TRUE) |
| 139 |
desc <- catch(renv_graph_description(record)) |
| 140 |
if (inherits(desc, "error")) { |
| 141 |
desc <- structure(list(), |
| 142 |
resolution_failed = TRUE, |
| 143 |
resolution_error = conditionMessage(desc) |
| 144 |
) |
| 145 |
} |
| 146 |
|
| 147 |
# merge record fields (Source, Remote* fields) into description |
| 148 |
for (field in names(record)) |
| 149 |
if (is.null(desc[[field]])) |
| 150 |
desc[[field]] <- record[[field]] |
| 151 |
|
| 152 |
# supplement with installed DESCRIPTION for non-standard dependency fields |
| 153 |
# (e.g. Config/Needs/protein); PACKAGES metadata only has standard fields |
| 154 |
if (!is.null(fields)) { |
| 155 |
installed <- catch(renv_description_read(package = package)) |
| 156 |
if (!inherits(installed, "error")) { |
| 157 |
for (field in fields) |
| 158 |
if (is.null(desc[[field]]) && !is.null(installed[[field]])) |
| 159 |
desc[[field]] <- installed[[field]] |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
# store the resolved description |
| 164 |
assign(package, desc, envir = envir) |
| 165 |
|
| 166 |
# return dependencies for the caller to enqueue |
| 167 |
deps <- renv_graph_deps(desc, fields = fields) |
| 168 |
|
| 169 |
# resolve any Remotes declared by this package; this pre-populates envir |
| 170 |
# with the correct (e.g. GitHub) resolution so that when the BFS later |
| 171 |
# encounters the bare package name, it finds it already resolved. |
| 172 |
# override = TRUE allows these to replace default repository records |
| 173 |
# that may have already been resolved into envir |
| 174 |
if (config$install.remotes()) { |
| 175 |
remotes <- paste(exclude(desc[["Remotes"]], NA), collapse = ", ") |
| 176 |
specs <- strsplit(remotes, "\\s*,\\s*")[[1L]] |
| 177 |
for (spec in specs) { |
| 178 |
rdeps <- catch(renv_graph_resolve(spec, envir, records = records, override = TRUE)) |
| 179 |
if (!inherits(rdeps, "error")) |
| 180 |
deps <- c(deps, rdeps) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
deps |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
renv_graph_description <- function(record) { |
| 189 |
|
| 190 |
source <- renv_record_source(record, normalize = TRUE) |
| 191 |
|
| 192 |
case( |
| 193 |
source == "repository" ~ renv_graph_description_repository(record), |
| 194 |
source == "bioconductor" ~ renv_graph_description_bioconductor(record), |
| 195 |
source == "github" ~ renv_graph_description_github(record), |
| 196 |
source == "gitlab" ~ renv_graph_description_gitlab(record), |
| 197 |
source == "bitbucket" ~ renv_graph_description_bitbucket(record), |
| 198 |
source == "git" ~ renv_graph_description_git(record), |
| 199 |
source == "local" ~ renv_graph_description_local(record), |
| 200 |
source == "url" ~ as.list(record), |
| 201 |
TRUE ~ renv_graph_description_repository(record) |
| 202 |
) |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
renv_graph_description_repository <- function(record) { |
| 207 |
|
| 208 |
package <- record$Package |
| 209 |
version <- record$Version |
| 210 |
|
| 211 |
# respect explicitly-requested package type (e.g. install(..., type = "binary")) |
| 212 |
# so that graph resolution finds versions available for the requested type; |
| 213 |
# without this, we always search source packages and may resolve a version |
| 214 |
# that only exists as source when the user asked for binaries |
| 215 |
type <- the$install_pkg_type %||% "source" |
| 216 |
|
| 217 |
# try available packages entry (returns full fields including Imports, Depends); |
| 218 |
# we need these fields for dependency graph resolution; |
| 219 |
# in strict mode with a URL-valued Repository, search only that repository |
| 220 |
repository <- record[["Repository"]] |
| 221 |
strict <- renv_restore_state(key = "strict") %||% FALSE |
| 222 |
repos <- if (strict && is.character(repository) && grepl("://", repository, fixed = TRUE)) |
| 223 |
renv_repos_baseurl(repository) |
| 224 |
|
| 225 |
entry <- catch( |
| 226 |
renv_available_packages_entry( |
| 227 |
package = package, |
| 228 |
type = type, |
| 229 |
filter = version, |
| 230 |
repos = repos, |
| 231 |
prefer = record[["Repository"]] |
| 232 |
) |
| 233 |
) |
| 234 |
|
| 235 |
if (!inherits(entry, "error")) |
| 236 |
return(as.list(entry)) |
| 237 |
|
| 238 |
# try cellar via renv_available_packages_latest (includes cellar = TRUE); |
| 239 |
# cellar packages won't be found by renv_available_packages_entry. |
| 240 |
# NOTE: renv_available_packages_latest only returns limited fields |
| 241 |
# (Package, Version, etc.) — no Depends/Imports/LinkingTo |
| 242 |
latest <- catch(renv_available_packages_latest(package, type = type)) |
| 243 |
if (!inherits(latest, "error")) { |
| 244 |
if (is.null(version) || identical(latest$Version, version)) |
| 245 |
return(as.list(latest)) |
| 246 |
} |
| 247 |
|
| 248 |
# the requested version wasn't found in configured repos; try crandb for the |
| 249 |
# specific version's dependency fields. this must come before any fallback |
| 250 |
# that reuses the latest entry's fields with an overridden version, since |
| 251 |
# dependency constraints can change between versions (e.g. #2278) |
| 252 |
if (!is.null(version)) { |
| 253 |
crandb <- catch(renv_graph_description_crandb(package, version)) |
| 254 |
if (!inherits(crandb, "error")) |
| 255 |
return(as.list(crandb)) |
| 256 |
} |
| 257 |
|
| 258 |
# last-resort fallback when crandb is unreachable: use the latest entry's |
| 259 |
# full fields with the requested version substituted. this may return stale |
| 260 |
# dependency constraints if they changed between versions, but is better |
| 261 |
# than failing outright. |
| 262 |
if (!is.null(version) && !inherits(latest, "error")) { |
| 263 |
full <- catch(renv_available_packages_entry(package, type = type)) |
| 264 |
if (!inherits(full, "error")) { |
| 265 |
desc <- as.list(full) |
| 266 |
desc$Version <- version |
| 267 |
return(desc) |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
# if we found any entry at all (e.g. version filter excluded it), |
| 272 |
# use the latest available description |
| 273 |
if (!inherits(latest, "error")) |
| 274 |
return(as.list(latest)) |
| 275 |
|
| 276 |
# all lookups failed; signal an error so the caller can warn |
| 277 |
stopf("package '%s' is not available", package) |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
renv_graph_description_bioconductor <- function(record) { |
| 282 |
|
| 283 |
# activate Bioconductor repositories in this context |
| 284 |
version <- record[["git_branch"]] |
| 285 |
if (!is.null(version)) { |
| 286 |
parts <- strsplit(version, "_", fixed = TRUE)[[1L]] |
| 287 |
ok <- length(parts) == 3L && tolower(parts[[1L]]) == "release" |
| 288 |
if (!ok) version <- NULL else version <- paste(parts[2:3], collapse = ".") |
| 289 |
} |
| 290 |
|
| 291 |
project <- renv_restore_state(key = "project") %||% renv_project_resolve() |
| 292 |
renv_scope_bioconductor(project = project, version = version) |
| 293 |
|
| 294 |
renv_graph_description_repository(record) |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
renv_graph_description_crandb <- function(package, version) { |
| 299 |
|
| 300 |
url <- sprintf("https://crandb.r-pkg.org/%s/%s", package, version) |
| 301 |
destfile <- renv_scope_tempfile("renv-crandb-") |
| 302 |
download(url, destfile = destfile, quiet = TRUE) |
| 303 |
json <- renv_json_read(file = destfile) |
| 304 |
|
| 305 |
# convert to DESCRIPTION-like list |
| 306 |
desc <- list( |
| 307 |
Package = json[["Package"]] %||% package, |
| 308 |
Version = json[["Version"]] %||% version |
| 309 |
) |
| 310 |
|
| 311 |
# convert structured dependency fields to DESCRIPTION-format strings |
| 312 |
fields <- c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances") |
| 313 |
for (field in fields) { |
| 314 |
value <- json[[field]] |
| 315 |
if (!is.null(value)) |
| 316 |
desc[[field]] <- renv_graph_description_crandb_convert(value) |
| 317 |
} |
| 318 |
|
| 319 |
desc |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
renv_graph_description_crandb_convert <- function(value) { |
| 324 |
|
| 325 |
parts <- enum_chr(value, function(name, version) { |
| 326 |
sprintf("%s (%s)", name, version) |
| 327 |
}) |
| 328 |
|
| 329 |
fixed <- gsub(" (*)", "", parts, fixed = TRUE) |
| 330 |
paste(fixed, collapse = ", ") |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
renv_graph_description_git <- function(record) { |
| 335 |
desc <- renv_remotes_resolve_git_description(record) |
| 336 |
as.list(desc) |
| 337 |
} |
| 338 |
|
| 339 |
renv_graph_description_github <- function(record) { |
| 340 |
|
| 341 |
host <- record$RemoteHost %||% config$github.host() |
| 342 |
user <- record$RemoteUsername |
| 343 |
repo <- record$RemoteRepo |
| 344 |
subdir <- record$RemoteSubdir |
| 345 |
sha <- record$RemoteSha |
| 346 |
|
| 347 |
origin <- fsub("api.github.com", "github.com", renv_retrieve_origin(host)) |
| 348 |
url <- paste(c(origin, user, repo), collapse = "/") |
| 349 |
|
| 350 |
desc <- renv_remotes_resolve_github_description(url, host, user, repo, subdir, sha) |
| 351 |
as.list(desc) |
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
renv_graph_description_gitlab <- function(record) { |
| 356 |
|
| 357 |
host <- record$RemoteHost %||% config$gitlab.host() |
| 358 |
user <- record$RemoteUsername |
| 359 |
repo <- record$RemoteRepo |
| 360 |
subdir <- record$RemoteSubdir |
| 361 |
ref <- record$RemoteRef |
| 362 |
|
| 363 |
parts <- c(if (nzchar(subdir %||% "")) subdir, "DESCRIPTION") |
| 364 |
descpath <- URLencode(paste(parts, collapse = "/"), reserved = TRUE) |
| 365 |
|
| 366 |
# scope authentication |
| 367 |
renv_scope_auth(repo) |
| 368 |
|
| 369 |
# retrieve DESCRIPTION file |
| 370 |
fmt <- "%s/api/v4/projects/%s/repository/files/%s/raw?ref=%s" |
| 371 |
origin <- renv_retrieve_origin(host) |
| 372 |
id <- URLencode(paste(user, repo, sep = "/"), reserved = TRUE) |
| 373 |
url <- sprintf(fmt, origin, id, descpath, ref) |
| 374 |
|
| 375 |
destfile <- renv_scope_tempfile("renv-description-") |
| 376 |
download(url, destfile = destfile, type = "gitlab", quiet = TRUE) |
| 377 |
desc <- renv_dcf_read(destfile) |
| 378 |
as.list(desc) |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
renv_graph_description_bitbucket <- function(record) { |
| 383 |
|
| 384 |
host <- record$RemoteHost %||% config$bitbucket.host() |
| 385 |
user <- record$RemoteUsername |
| 386 |
repo <- record$RemoteRepo |
| 387 |
subdir <- record$RemoteSubdir |
| 388 |
ref <- record$RemoteRef |
| 389 |
|
| 390 |
# scope authentication |
| 391 |
renv_scope_auth(repo) |
| 392 |
|
| 393 |
# get DESCRIPTION file |
| 394 |
parts <- c(if (nzchar(subdir %||% "")) subdir, "DESCRIPTION") |
| 395 |
descpath <- paste(parts, collapse = "/") |
| 396 |
fmt <- "%s/repositories/%s/%s/src/%s/%s" |
| 397 |
origin <- renv_retrieve_origin(host) |
| 398 |
url <- sprintf(fmt, origin, user, repo, ref, descpath) |
| 399 |
|
| 400 |
destfile <- renv_scope_tempfile("renv-description-") |
| 401 |
download(url, destfile = destfile, type = "bitbucket", quiet = TRUE) |
| 402 |
desc <- renv_dcf_read(destfile) |
| 403 |
as.list(desc) |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
renv_graph_description_local <- function(record) { |
| 408 |
|
| 409 |
# try reading DESCRIPTION from the local path; |
| 410 |
# renv_description_read handles directories, archives, and files |
| 411 |
path <- record$Path %||% record$RemoteUrl |
| 412 |
if (!is.null(path) && file.exists(path)) { |
| 413 |
desc <- catch(renv_description_read(path)) |
| 414 |
if (!inherits(desc, "error")) |
| 415 |
return(as.list(desc)) |
| 416 |
} |
| 417 |
|
| 418 |
as.list(record) |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
renv_graph_deps <- function(desc, fields = NULL) { |
| 423 |
|
| 424 |
fields <- fields %||% c("Depends", "Imports", "LinkingTo") |
| 425 |
|
| 426 |
deps <- character() |
| 427 |
for (field in fields) { |
| 428 |
value <- renv_description_parse_field(desc[[field]]) |
| 429 |
deps <- c(deps, value$Package) |
| 430 |
} |
| 431 |
|
| 432 |
# exclude base packages (R, utils, methods, etc.) |
| 433 |
deps <- setdiff(unique(deps), renv_packages_base()) |
| 434 |
deps |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
renv_graph_requirements <- function(descriptions) { |
| 439 |
|
| 440 |
requirements <- new.env(parent = emptyenv()) |
| 441 |
|
| 442 |
fields <- c("Depends", "Imports", "LinkingTo") |
| 443 |
for (desc in descriptions) { |
| 444 |
for (field in fields) { |
| 445 |
parsed <- renv_description_parse_field(desc[[field]]) |
| 446 |
if (is.null(parsed)) |
| 447 |
next |
| 448 |
explicit <- parsed[nzchar(parsed$Require) & nzchar(parsed$Version), ] |
| 449 |
if (nrow(explicit) == 0L) |
| 450 |
next |
| 451 |
explicit$RequiredBy <- desc$Package |
| 452 |
for (i in seq_len(nrow(explicit))) { |
| 453 |
pkg <- explicit$Package[[i]] |
| 454 |
requirements[[pkg]] <- c(requirements[[pkg]], list(explicit[i, ])) |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
# bind each package's requirement rows into a single data frame |
| 460 |
enumerate(as.list(requirements), function(pkg, rows) { |
| 461 |
requirements[[pkg]] <- bind(rows) |
| 462 |
}) |
| 463 |
|
| 464 |
requirements |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
renv_graph_compatible <- function(version, requirements) { |
| 469 |
|
| 470 |
if (is.null(requirements) || nrow(requirements) == 0L) |
| 471 |
return(TRUE) |
| 472 |
|
| 473 |
rversion <- numeric_version(version) |
| 474 |
all(map_lgl(seq_len(nrow(requirements)), function(i) { |
| 475 |
expr <- call(requirements$Require[[i]], rversion, requirements$Version[[i]]) |
| 476 |
eval(expr, envir = baseenv()) |
| 477 |
})) |
| 478 |
|
| 479 |
} |
| 480 |
|
| 481 |
renv_graph_requirements_check <- function(descriptions, requirements) { |
| 482 |
|
| 483 |
if (!renv_verbose()) |
| 484 |
return(invisible()) |
| 485 |
|
| 486 |
messages <- character() |
| 487 |
for (package in ls(envir = requirements)) { |
| 488 |
|
| 489 |
reqs <- requirements[[package]] |
| 490 |
|
| 491 |
version <- descriptions[[package]]$Version |
| 492 |
if (is.null(version)) |
| 493 |
next |
| 494 |
|
| 495 |
if (renv_graph_compatible(version, reqs)) |
| 496 |
next |
| 497 |
|
| 498 |
# find which requirements are unsatisfied |
| 499 |
rversion <- numeric_version(version) |
| 500 |
for (i in seq_len(nrow(reqs))) { |
| 501 |
expr <- call(reqs$Require[[i]], rversion, reqs$Version[[i]]) |
| 502 |
if (!eval(expr, envir = baseenv())) { |
| 503 |
fmt <- "'%s' requires '%s %s %s', but '%s %s' will be installed" |
| 504 |
msg <- sprintf(fmt, |
| 505 |
reqs$RequiredBy[[i]], |
| 506 |
package, reqs$Require[[i]], reqs$Version[[i]], |
| 507 |
package, version |
| 508 |
) |
| 509 |
messages <- c(messages, msg) |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
} |
| 514 |
|
| 515 |
if (empty(messages)) |
| 516 |
return() |
| 517 |
|
| 518 |
bulletin( |
| 519 |
"The following issues were discovered while preparing for installation:", |
| 520 |
messages, |
| 521 |
"Installation of these packages may not succeed." |
| 522 |
) |
| 523 |
|
| 524 |
} |
| 525 |
|
| 526 |
# Tree printing ---- |
| 527 |
|
| 528 |
renv_graph_print <- function(records, roots, fields) { |
| 529 |
|
| 530 |
if (!renv_verbose()) |
| 531 |
return(invisible()) |
| 532 |
|
| 533 |
# keep only roots that exist in the records |
| 534 |
roots <- csort(intersect(roots, names(records))) |
| 535 |
if (empty(roots)) |
| 536 |
return(invisible()) |
| 537 |
|
| 538 |
# print a virtual root with top-level packages as children |
| 539 |
writef(".") |
| 540 |
renv_graph_print_impl(roots, records, fields, prefix = "") |
| 541 |
|
| 542 |
invisible() |
| 543 |
|
| 544 |
} |
| 545 |
|
| 546 |
renv_graph_print_impl <- function(children, records, fields, prefix) { |
| 547 |
|
| 548 |
n <- length(children) |
| 549 |
for (i in seq_len(n)) { |
| 550 |
|
| 551 |
child <- children[[i]] |
| 552 |
record <- records[[child]] |
| 553 |
label <- renv_record_format_short(record) |
| 554 |
last <- i == n |
| 555 |
|
| 556 |
connector <- if (last) "\u2514\u2500 " else "\u251c\u2500 " |
| 557 |
writef("%s%s%s %s", prefix, connector, child, label) |
| 558 |
|
| 559 |
extension <- if (last) " " else "\u2502 " |
| 560 |
subdeps <- renv_graph_print_children(record, records, fields) |
| 561 |
renv_graph_print_impl(subdeps, records, fields, paste0(prefix, extension)) |
| 562 |
|
| 563 |
} |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
renv_graph_print_children <- function(record, records, fields) { |
| 568 |
|
| 569 |
deps <- character() |
| 570 |
for (field in fields) { |
| 571 |
value <- record[[field]] |
| 572 |
if (is.null(value)) |
| 573 |
next |
| 574 |
# extract package names, stripping version constraints |
| 575 |
pkgs <- sub("\\s*\\(.*", "", unlist(value)) |
| 576 |
deps <- c(deps, pkgs) |
| 577 |
} |
| 578 |
|
| 579 |
# keep only packages present in the lockfile, sorted |
| 580 |
csort(intersect(unique(deps), names(records))) |
| 581 |
|
| 582 |
} |
| 583 |
|
| 584 |
# compute the root packages in a set of records: packages that are |
| 585 |
# not dependencies of any other package in the set |
| 586 |
renv_graph_roots <- function(records, fields) { |
| 587 |
|
| 588 |
deps <- character() |
| 589 |
for (record in records) |
| 590 |
deps <- c(deps, renv_graph_print_children(record, records, fields)) |
| 591 |
|
| 592 |
roots <- setdiff(names(records), unique(deps)) |
| 593 |
|
| 594 |
# if every package is a dep of something else (unlikely), fall back to all |
| 595 |
if (empty(roots)) |
| 596 |
roots <- names(records) |
| 597 |
|
| 598 |
csort(roots) |
| 599 |
|
| 600 |
} |
| 601 |
|
| 602 |
renv_graph_needs_update <- function(pkg, record, requirements) { |
| 603 |
|
| 604 |
# check whether the resolved version is already installed |
| 605 |
path <- renv_restore_find(pkg, record) |
| 606 |
if (nzchar(path)) |
| 607 |
return(FALSE) |
| 608 |
|
| 609 |
# for transitive dependencies that are already installed, check |
| 610 |
# whether the installed version satisfies dependency requirements; |
| 611 |
# this avoids upgrading dependencies during install() when the |
| 612 |
# existing library version is already compatible. |
| 613 |
# explicitly-requested packages (in state$packages) always get |
| 614 |
# installed, so skip this check for those. |
| 615 |
state <- renv_restore_state() |
| 616 |
if (!(pkg %in% state$packages)) { |
| 617 |
installed <- renv_package_version(pkg) |
| 618 |
if (!is.null(installed)) { |
| 619 |
reqs <- requirements[[pkg]] |
| 620 |
if (renv_graph_compatible(installed, reqs)) |
| 621 |
return(FALSE) |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
TRUE |
| 626 |
|
| 627 |
} |
| 628 |
|
| 629 |
renv_graph_adjacency <- function(descriptions) { |
| 630 |
|
| 631 |
packages <- names(descriptions) |
| 632 |
n <- length(packages) |
| 633 |
|
| 634 |
# build adjacency: for each package, its deps within the set |
| 635 |
adj <- lapply(descriptions, function(desc) { |
| 636 |
deps <- renv_graph_deps(desc) |
| 637 |
intersect(deps, packages) |
| 638 |
}) |
| 639 |
names(adj) <- packages |
| 640 |
|
| 641 |
# compute in-degree (number of deps within the set) |
| 642 |
indegree <- integer(n) |
| 643 |
names(indegree) <- packages |
| 644 |
for (pkg in packages) |
| 645 |
indegree[pkg] <- length(adj[[pkg]]) |
| 646 |
|
| 647 |
# build reverse adjacency: for each package, who depends on it |
| 648 |
revadj <- vector("list", n) |
| 649 |
names(revadj) <- packages |
| 650 |
for (pkg in packages) |
| 651 |
revadj[pkg] <- list(character()) |
| 652 |
|
| 653 |
for (pkg in packages) |
| 654 |
for (dep in adj[[pkg]]) |
| 655 |
revadj[[dep]] <- c(revadj[[dep]], pkg) |
| 656 |
|
| 657 |
list( |
| 658 |
packages = packages, |
| 659 |
adj = adj, |
| 660 |
indegree = indegree, |
| 661 |
revadj = revadj |
| 662 |
) |
| 663 |
|
| 664 |
} |
| 665 |
|
| 666 |
renv_graph_sort <- function(descriptions) { |
| 667 |
|
| 668 |
packages <- names(descriptions) |
| 669 |
if (length(packages) == 0L) |
| 670 |
return(descriptions) |
| 671 |
|
| 672 |
g <- renv_graph_adjacency(descriptions) |
| 673 |
indegree <- g$indegree |
| 674 |
|
| 675 |
# Kahn's algorithm |
| 676 |
queue <- g$packages[indegree == 0L] |
| 677 |
result <- character() |
| 678 |
|
| 679 |
while (length(queue) > 0L) { |
| 680 |
current <- queue[[1L]] |
| 681 |
queue <- queue[-1L] |
| 682 |
result <- c(result, current) |
| 683 |
|
| 684 |
for (dependent in g$revadj[[current]]) { |
| 685 |
indegree[[dependent]] <- indegree[[dependent]] - 1L |
| 686 |
if (indegree[[dependent]] == 0L) |
| 687 |
queue <- c(queue, dependent) |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
# handle cycles: append any remaining packages |
| 692 |
remaining <- setdiff(g$packages, result) |
| 693 |
if (length(remaining) > 0L) { |
| 694 |
warningf("dependency cycle detected among: %s", paste(remaining, collapse = ", ")) |
| 695 |
result <- c(result, remaining) |
| 696 |
} |
| 697 |
|
| 698 |
descriptions[result] |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
renv_graph_waves <- function(descriptions) { |
| 703 |
|
| 704 |
packages <- names(descriptions) |
| 705 |
if (length(packages) == 0L) |
| 706 |
return(list()) |
| 707 |
|
| 708 |
g <- renv_graph_adjacency(descriptions) |
| 709 |
indegree <- g$indegree |
| 710 |
|
| 711 |
# iterative peeling: each iteration collects all packages with in-degree 0 |
| 712 |
waves <- list() |
| 713 |
remaining <- g$packages |
| 714 |
|
| 715 |
while (length(remaining) > 0L) { |
| 716 |
|
| 717 |
# find all packages with no unresolved dependencies |
| 718 |
ready <- remaining[indegree[remaining] == 0L] |
| 719 |
|
| 720 |
# handle cycles: if nothing is ready, dump remaining into final wave |
| 721 |
if (length(ready) == 0L) { |
| 722 |
warningf("dependency cycle detected among: %s", paste(remaining, collapse = ", ")) |
| 723 |
waves <- c(waves, list(remaining)) |
| 724 |
break |
| 725 |
} |
| 726 |
|
| 727 |
waves <- c(waves, list(ready)) |
| 728 |
remaining <- setdiff(remaining, ready) |
| 729 |
|
| 730 |
# decrement in-degree for dependents |
| 731 |
for (pkg in ready) |
| 732 |
for (dependent in g$revadj[[pkg]]) |
| 733 |
indegree[[dependent]] <- indegree[[dependent]] - 1L |
| 734 |
|
| 735 |
} |
| 736 |
|
| 737 |
waves |
| 738 |
|
| 739 |
} |
| 740 |
|
| 741 |
renv_graph_urls <- function(descriptions) { |
| 742 |
|
| 743 |
result <- vector("list", length(descriptions)) |
| 744 |
names(result) <- names(descriptions) |
| 745 |
|
| 746 |
for (i in seq_along(descriptions)) { |
| 747 |
|
| 748 |
desc <- descriptions[[i]] |
| 749 |
package <- desc$Package |
| 750 |
source <- renv_record_source(desc, normalize = TRUE) |
| 751 |
|
| 752 |
# resolve download URL; on error the package falls through |
| 753 |
# to sequential retrieval which handles its own error reporting |
| 754 |
result[[package]] <- tryCatch( |
| 755 |
switch(source, |
| 756 |
repository = renv_graph_url_repository(desc), |
| 757 |
bioconductor = renv_graph_url_bioconductor(desc), |
| 758 |
github = renv_graph_url_github(desc), |
| 759 |
gitlab = renv_graph_url_gitlab(desc), |
| 760 |
url = renv_graph_url_url(desc), |
| 761 |
local = renv_graph_url_local(desc), |
| 762 |
cellar = renv_graph_url_cellar(desc), |
| 763 |
NULL |
| 764 |
), |
| 765 |
error = function(cnd) NULL |
| 766 |
) |
| 767 |
|
| 768 |
} |
| 769 |
|
| 770 |
result |
| 771 |
|
| 772 |
} |
| 773 |
|
| 774 |
renv_graph_url_bioconductor <- function(desc) { |
| 775 |
|
| 776 |
project <- renv_restore_state(key = "project") %||% renv_project_resolve() |
| 777 |
renv_scope_bioconductor(project = project) |
| 778 |
renv_graph_url_repository(desc) |
| 779 |
|
| 780 |
} |
| 781 |
|
| 782 |
renv_graph_url_repository <- function(desc) { |
| 783 |
|
| 784 |
package <- desc$Package |
| 785 |
version <- desc$Version |
| 786 |
|
| 787 |
# use renv_available_packages_latest to select the best record; |
| 788 |
# this handles binary vs source selection (including P3M), |
| 789 |
# NeedsCompilation checks, and toolchain availability |
| 790 |
# |
| 791 |
# include custom repository URL (if any) so packages only |
| 792 |
# available on non-standard repositories can be found; |
| 793 |
# in strict mode, use only the recorded URL |
| 794 |
repository <- desc[["Repository"]] |
| 795 |
strict <- renv_restore_state(key = "strict") %||% FALSE |
| 796 |
repos <- if (is.character(repository) && grepl("://", repository, fixed = TRUE)) { |
| 797 |
baseurl <- renv_repos_baseurl(repository) |
| 798 |
current <- getOption("repos") |
| 799 |
matches <- any(renv_repos_matches(baseurl, current)) |
| 800 |
augmented <- when(matches, current, c(baseurl, current)) |
| 801 |
when(strict, baseurl, augmented) |
| 802 |
} |
| 803 |
|
| 804 |
record <- catch(renv_available_packages_latest(package, repos = repos)) |
| 805 |
if (inherits(record, "error")) |
| 806 |
return(NULL) |
| 807 |
|
| 808 |
# if the latest version matches, use it directly |
| 809 |
if (is.null(version) || identical(record$Version, version)) |
| 810 |
return(renv_graph_url_repository_record(desc, record)) |
| 811 |
|
| 812 |
# the latest version doesn't match; try a version-filtered lookup |
| 813 |
# using the same type that was selected for the latest |
| 814 |
type <- attr(record, "type", exact = TRUE) %||% "source" |
| 815 |
entry <- catch(renv_available_packages_entry( |
| 816 |
package = package, |
| 817 |
type = type, |
| 818 |
filter = version, |
| 819 |
prefer = desc[["Repository"]] |
| 820 |
)) |
| 821 |
|
| 822 |
if (!inherits(entry, "error")) { |
| 823 |
tagged <- renv_available_packages_record(entry, type) |
| 824 |
return(renv_graph_url_repository_record(desc, tagged)) |
| 825 |
} |
| 826 |
|
| 827 |
# the requested version is not in current available packages; |
| 828 |
# try the CRAN-style archive (source only) |
| 829 |
baseurl <- renv_repos_baseurl(attr(record, "url", exact = TRUE)) |
| 830 |
root <- renv_retrieve_repos_archive_root(baseurl, as.list(desc)) |
| 831 |
|
| 832 |
if (!is.null(root)) { |
| 833 |
repourl <- attr(record, "url", exact = TRUE) |
| 834 |
reponame <- attr(record, "name", exact = TRUE) |
| 835 |
name <- renv_retrieve_repos_archive_name(as.list(desc), "source") |
| 836 |
url <- file.path(root, name) |
| 837 |
destfile <- renv_retrieve_path(as.list(desc), type = "source") |
| 838 |
return(list( |
| 839 |
url = url, |
| 840 |
destfile = destfile, |
| 841 |
type = "repository", |
| 842 |
pkgtype = "source", |
| 843 |
repourl = repourl, |
| 844 |
reponame = reponame |
| 845 |
)) |
| 846 |
} |
| 847 |
|
| 848 |
# couldn't resolve; fall back to sequential retrieval |
| 849 |
NULL |
| 850 |
|
| 851 |
} |
| 852 |
|
| 853 |
renv_graph_url_repository_record <- function(desc, record) { |
| 854 |
|
| 855 |
type <- attr(record, "type", exact = TRUE) %||% "source" |
| 856 |
repo <- attr(record, "url", exact = TRUE) |
| 857 |
reponame <- attr(record, "name", exact = TRUE) |
| 858 |
name <- renv_retrieve_repos_archive_name(record, type) |
| 859 |
|
| 860 |
url <- file.path(repo, name) |
| 861 |
destfile <- renv_retrieve_path(as.list(desc), type = type) |
| 862 |
|
| 863 |
# carry repository metadata so install can tag the record |
| 864 |
# for renv_package_augment (RemoteRepos, RemoteReposName) |
| 865 |
list( |
| 866 |
url = url, |
| 867 |
destfile = destfile, |
| 868 |
type = "repository", |
| 869 |
pkgtype = type, |
| 870 |
repourl = repo, |
| 871 |
reponame = reponame |
| 872 |
) |
| 873 |
|
| 874 |
} |
| 875 |
|
| 876 |
renv_graph_url_github <- function(desc) { |
| 877 |
|
| 878 |
host <- desc$RemoteHost %||% config$github.host() |
| 879 |
origin <- renv_retrieve_origin(host) |
| 880 |
user <- desc$RemoteUsername |
| 881 |
repo <- desc$RemoteRepo |
| 882 |
ref <- desc$RemoteSha %||% desc$RemoteRef |
| 883 |
|
| 884 |
if (is.null(ref) || is.null(user) || is.null(repo)) |
| 885 |
return(NULL) |
| 886 |
|
| 887 |
url <- sprintf("%s/repos/%s/%s/tarball/%s", origin, user, repo, ref) |
| 888 |
destfile <- renv_retrieve_path(as.list(desc)) |
| 889 |
|
| 890 |
list(url = url, destfile = destfile, type = "github") |
| 891 |
|
| 892 |
} |
| 893 |
|
| 894 |
renv_graph_url_gitlab <- function(desc) { |
| 895 |
|
| 896 |
host <- desc$RemoteHost %||% config$gitlab.host() |
| 897 |
origin <- renv_retrieve_origin(host) |
| 898 |
user <- desc$RemoteUsername |
| 899 |
repo <- desc$RemoteRepo |
| 900 |
sha <- desc$RemoteSha %||% desc$RemoteRef |
| 901 |
|
| 902 |
if (is.null(user) || is.null(repo)) |
| 903 |
return(NULL) |
| 904 |
|
| 905 |
id <- URLencode(paste(user, repo, sep = "/"), reserved = TRUE) |
| 906 |
url <- sprintf("%s/api/v4/projects/%s/repository/archive.tar.gz", origin, id) |
| 907 |
if (!is.null(sha)) |
| 908 |
url <- paste(url, paste("sha", sha, sep = "="), sep = "?") |
| 909 |
|
| 910 |
destfile <- renv_retrieve_path(as.list(desc)) |
| 911 |
|
| 912 |
list(url = url, destfile = destfile, type = "gitlab") |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
renv_graph_url_url <- function(desc) { |
| 917 |
|
| 918 |
url <- desc$RemoteUrl |
| 919 |
if (is.null(url)) { |
| 920 |
# fall back to RemotePkgRef parsing (see renv_retrieve_url_resolve) |
| 921 |
pkgref <- desc$RemotePkgRef |
| 922 |
if (!is.null(pkgref)) { |
| 923 |
remote <- renv_remotes_parse(pkgref) |
| 924 |
if (identical(remote$type, "url")) |
| 925 |
url <- remote$url |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
if (is.null(url)) |
| 930 |
return(NULL) |
| 931 |
|
| 932 |
hash <- md5(url) |
| 933 |
ext <- fileext(url, default = ".tar.gz") |
| 934 |
destfile <- renv_paths_source("url", paste0(hash, ext)) |
| 935 |
|
| 936 |
list(url = url, destfile = destfile, type = "url") |
| 937 |
|
| 938 |
} |
| 939 |
|
| 940 |
renv_graph_url_local <- function(desc) { |
| 941 |
|
| 942 |
# check Path and Source fields for a local file path |
| 943 |
for (field in c("Path", "Source")) { |
| 944 |
path <- desc[[field]] |
| 945 |
if (is.null(path) || !nzchar(path)) |
| 946 |
next |
| 947 |
if (!grepl("[/\\\\]|[.](?:zip|tgz|gz)$", path)) |
| 948 |
next |
| 949 |
if (!file.exists(path)) |
| 950 |
next |
| 951 |
|
| 952 |
path <- renv_path_normalize(path, mustWork = TRUE) |
| 953 |
destfile <- renv_retrieve_path(as.list(desc)) |
| 954 |
|
| 955 |
# use a file URI so the parallel downloaders can handle it uniformly |
| 956 |
url <- paste0("file://", path) |
| 957 |
return(list(url = url, destfile = destfile, type = "local")) |
| 958 |
} |
| 959 |
|
| 960 |
NULL |
| 961 |
|
| 962 |
} |
| 963 |
|
| 964 |
renv_graph_url_cellar <- function(desc) { |
| 965 |
|
| 966 |
# cellar records from renv_available_packages_latest carry the |
| 967 |
# file URI in their "url" attribute (set by renv_record_tag) |
| 968 |
url <- attr(desc, "url", exact = TRUE) |
| 969 |
type <- attr(desc, "type", exact = TRUE) %||% "source" |
| 970 |
|
| 971 |
if (is.null(url)) |
| 972 |
return(NULL) |
| 973 |
|
| 974 |
# the url points to the cellar directory; construct the tarball name |
| 975 |
name <- renv_retrieve_repos_archive_name(as.list(desc), type) |
| 976 |
href <- file.path(url, name) |
| 977 |
|
| 978 |
destfile <- renv_retrieve_path(as.list(desc), type = type) |
| 979 |
|
| 980 |
list( |
| 981 |
url = href, |
| 982 |
destfile = destfile, |
| 983 |
type = "cellar", |
| 984 |
cellarurl = url |
| 985 |
) |
| 986 |
|
| 987 |
} |
| 988 |
|
| 989 |
renv_graph_scope_retrieve <- function(scope = parent.frame()) { |
| 990 |
|
| 991 |
# prepare retrieve environment (repos, PPM, user agent); |
| 992 |
# this setup is normally done inside renv_retrieve_impl — |
| 993 |
# we replicate it here so parallel / forked processes inherit these options |
| 994 |
repos <- renv_repos_normalize() |
| 995 |
if (renv_ppm_enabled()) |
| 996 |
repos <- renv_ppm_transform(repos) |
| 997 |
|
| 998 |
agent <- renv_http_useragent() |
| 999 |
if (!grepl("renv", agent)) |
| 1000 |
agent <- paste(sprintf("renv (%s)", renv_metadata_version()), agent, sep = "; ") |
| 1001 |
|
| 1002 |
renv_scope_options(repos = repos, HTTPUserAgent = agent, scope = scope) |
| 1003 |
|
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
renv_graph_install <- function(descriptions) { |
| 1008 |
|
| 1009 |
packages <- names(descriptions) |
| 1010 |
if (length(packages) == 0L) |
| 1011 |
return(invisible(list())) |
| 1012 |
|
| 1013 |
project <- renv_project_resolve() |
| 1014 |
library <- renv_libpaths_active() |
| 1015 |
|
| 1016 |
# set up restore state if not already provided by the caller |
| 1017 |
state <- the$restore_state %||% renv_scope_restore( |
| 1018 |
project = project, |
| 1019 |
library = library, |
| 1020 |
records = descriptions, |
| 1021 |
packages = packages, |
| 1022 |
recursive = FALSE |
| 1023 |
) |
| 1024 |
|
| 1025 |
renv_graph_scope_retrieve() |
| 1026 |
|
| 1027 |
# prepare install environment (inherited by subprocesses) |
| 1028 |
rlibs <- paste(renv_libpaths_all(), collapse = .Platform$path.sep) |
| 1029 |
renv_scope_envvars(R_LIBS = rlibs, R_LIBS_USER = "NULL", R_LIBS_SITE = "NULL") |
| 1030 |
renv_scope_envvars(RENV_WATCHDOG_ENABLED = "FALSE") |
| 1031 |
renv_scope_rtools() |
| 1032 |
|
| 1033 |
tar <- Sys.getenv("R_INSTALL_TAR", unset = renv_tar_exe(default = "internal")) |
| 1034 |
renv_scope_envvars(R_INSTALL_TAR = tar) |
| 1035 |
|
| 1036 |
if (renv_platform_macos()) |
| 1037 |
renv_xcode_check() |
| 1038 |
|
| 1039 |
renv_scope_install() |
| 1040 |
|
| 1041 |
# set up staged install if configured |
| 1042 |
staged <- renv_config_install_staged() |
| 1043 |
installdir <- library |
| 1044 |
|
| 1045 |
if (staged) { |
| 1046 |
templib <- renv_install_staged_library_path() |
| 1047 |
defer(unlink(templib, recursive = TRUE)) |
| 1048 |
renv_scope_libpaths(c(templib, renv_libpaths_all())) |
| 1049 |
installdir <- templib |
| 1050 |
} |
| 1051 |
|
| 1052 |
# determine cache settings |
| 1053 |
linkable <- renv_cache_linkable(project = project, library = library) |
| 1054 |
linker <- if (linkable) renv_file_link else renv_file_copy |
| 1055 |
|
| 1056 |
# staging directory for unpacked packages; scoped to this function |
| 1057 |
# so temp files from renv_package_unpack survive until install completes |
| 1058 |
staging <- renv_scope_tempfile("renv-graph-staging-") |
| 1059 |
ensure_directory(staging) |
| 1060 |
|
| 1061 |
# track results, failures, and error messages |
| 1062 |
all <- list() |
| 1063 |
failed <- stack("character") |
| 1064 |
errors <- stack() |
| 1065 |
verbose <- config$install.verbose() |
| 1066 |
jobs <- config$install.jobs() |
| 1067 |
timer <- timer() |
| 1068 |
|
| 1069 |
progress <- spinner("", 0L) |
| 1070 |
defer(progress$restore()) |
| 1071 |
|
| 1072 |
# ── Phase 1: Download all packages up front ────────────────────── |
| 1073 |
|
| 1074 |
# filter out packages that are already correctly installed; |
| 1075 |
# safe to do up front since nothing has been installed yet |
| 1076 |
requirements <- renv_graph_requirements(descriptions) |
| 1077 |
remaining <- Filter(function(pkg) { |
| 1078 |
renv_graph_needs_update(pkg, descriptions[[pkg]], requirements) |
| 1079 |
}, packages) |
| 1080 |
|
| 1081 |
# check cache: packages with a valid cache entry can be installed |
| 1082 |
# directly (just a link/copy), skipping download entirely; |
| 1083 |
# defer the actual install until the "Installing packages" phase |
| 1084 |
# so the output appears under that header |
| 1085 |
cachehits <- list() |
| 1086 |
for (pkg in remaining) { |
| 1087 |
desc <- descriptions[[pkg]] |
| 1088 |
cacheable <- |
| 1089 |
renv_cache_config_enabled(project = project) && |
| 1090 |
renv_record_cacheable(desc) && |
| 1091 |
!renv_restore_rebuild_required(desc) |
| 1092 |
|
| 1093 |
if (cacheable) { |
| 1094 |
path <- renv_cache_find(desc) |
| 1095 |
if (renv_cache_package_validate(path)) { |
| 1096 |
cachehits[[pkg]] <- list(record = desc, cache = path) |
| 1097 |
} |
| 1098 |
} |
| 1099 |
} |
| 1100 |
remaining <- setdiff(remaining, names(cachehits)) |
| 1101 |
|
| 1102 |
# download all remaining packages in one parallel batch |
| 1103 |
count <- 0L |
| 1104 |
downloaded <- list() |
| 1105 |
|
| 1106 |
if (length(remaining) > 0L) { |
| 1107 |
|
| 1108 |
writef(header("Downloading packages")) |
| 1109 |
|
| 1110 |
urlinfo <- renv_graph_urls(descriptions[remaining]) |
| 1111 |
|
| 1112 |
downloadable <- Filter(Negate(is.null), urlinfo) |
| 1113 |
fallbacks <- setdiff(remaining, names(downloadable)) |
| 1114 |
|
| 1115 |
timer$tick() |
| 1116 |
streamed <- character() |
| 1117 |
|
| 1118 |
if (length(downloadable)) { |
| 1119 |
|
| 1120 |
urls <- vapply(downloadable, `[[`, character(1L), "url") |
| 1121 |
destfiles <- vapply(downloadable, `[[`, character(1L), "destfile") |
| 1122 |
types <- vapply(downloadable, `[[`, character(1L), "type") |
| 1123 |
|
| 1124 |
# normalize to forward slashes so lookup keys match curl's %{filename_effective} |
| 1125 |
# output on all platforms (renv_download_parallel does the same normalization) |
| 1126 |
destfiles <- chartr("\\", "/", destfiles) |
| 1127 |
|
| 1128 |
ensure_parent_directory(destfiles) |
| 1129 |
|
| 1130 |
# build destfile -> package lookup for the callback |
| 1131 |
lookup <- names(destfiles) |
| 1132 |
names(lookup) <- destfiles |
| 1133 |
|
| 1134 |
# stream per-package progress as each download completes; |
| 1135 |
# disabled in testing mode to keep output order deterministic |
| 1136 |
showprogress <- !testing() && !ci() |
| 1137 |
awaiting <- names(downloadable) |
| 1138 |
|
| 1139 |
# define a callback to be invoked upon completion of each download |
| 1140 |
callback <- function(destfile, code, size, time, exitcode, error) { |
| 1141 |
|
| 1142 |
pkg <- lookup[[destfile]] |
| 1143 |
if (is.null(pkg)) |
| 1144 |
return() |
| 1145 |
|
| 1146 |
if (showprogress) progress$clear() |
| 1147 |
if (showprogress) progress$tick() |
| 1148 |
|
| 1149 |
desc <- descriptions[[pkg]] |
| 1150 |
pkgver <- paste(desc$Package, desc$Version) |
| 1151 |
|
| 1152 |
if (exitcode == "0") { |
| 1153 |
elapsed <- as.difftime(time, units = "secs") |
| 1154 |
status <- sprintf("%s in %s", renv_pretty_bytes(size), renv_difftime_format_short(elapsed)) |
| 1155 |
writef("%s %s [%s]", yay(), format(pkgver, width = the$install_step_width), status) |
| 1156 |
} else { |
| 1157 |
writef("%s %s", boo(), format(pkgver, width = the$install_step_width)) |
| 1158 |
} |
| 1159 |
|
| 1160 |
streamed <<- c(streamed, pkg) |
| 1161 |
awaiting <<- setdiff(awaiting, pkg) |
| 1162 |
|
| 1163 |
if (showprogress && length(awaiting) > 0L) |
| 1164 |
progress$update(awaiting) |
| 1165 |
|
| 1166 |
flush(stdout()) |
| 1167 |
|
| 1168 |
} |
| 1169 |
|
| 1170 |
# notify that we're about to download some packages |
| 1171 |
if (showprogress) { |
| 1172 |
progress$reset("Downloading", length(awaiting)) |
| 1173 |
if (length(awaiting)) |
| 1174 |
progress$update(awaiting) |
| 1175 |
} |
| 1176 |
|
| 1177 |
# perform the actual download |
| 1178 |
ok <- renv_download_parallel( |
| 1179 |
urls = urls, |
| 1180 |
destfiles = destfiles, |
| 1181 |
types = types, |
| 1182 |
callback = if (!testing()) callback |
| 1183 |
) |
| 1184 |
|
| 1185 |
# clear up and determine what packages need 'fallback' downloads |
| 1186 |
if (showprogress) |
| 1187 |
progress$clear() |
| 1188 |
for (pkg in names(downloadable)) { |
| 1189 |
if (!isTRUE(ok[[pkg]])) |
| 1190 |
fallbacks <- c(fallbacks, pkg) |
| 1191 |
} |
| 1192 |
|
| 1193 |
} |
| 1194 |
|
| 1195 |
# sequential fallback for unsupported sources or failed parallel downloads |
| 1196 |
for (pkg in fallbacks) { |
| 1197 |
|
| 1198 |
status <- catch({ |
| 1199 |
renv_scope_options(renv.download.headers = NULL) |
| 1200 |
renv_scope_options(renv.verbose = FALSE) |
| 1201 |
renv_retrieve_impl_one(pkg) |
| 1202 |
}) |
| 1203 |
|
| 1204 |
if (inherits(status, "error")) { |
| 1205 |
downloadable[[pkg]] <- NULL |
| 1206 |
next |
| 1207 |
} |
| 1208 |
|
| 1209 |
rec <- renv_restore_state()$install$data()[[pkg]] |
| 1210 |
if (is.null(rec)) |
| 1211 |
next |
| 1212 |
|
| 1213 |
downloadable[[pkg]] <- list( |
| 1214 |
url = "", |
| 1215 |
destfile = rec$Path, |
| 1216 |
type = renv_record_source(rec, normalize = TRUE) |
| 1217 |
) |
| 1218 |
|
| 1219 |
} |
| 1220 |
|
| 1221 |
# build downloaded records and report any packages not already streamed |
| 1222 |
for (package in sort(remaining)) { |
| 1223 |
|
| 1224 |
info <- downloadable[[package]] |
| 1225 |
hasfile <- !is.null(info) && file.exists(info$destfile) |
| 1226 |
|
| 1227 |
if (!hasfile) { |
| 1228 |
if (!(package %in% streamed)) { |
| 1229 |
desc <- descriptions[[package]] |
| 1230 |
pkgver <- paste(desc$Package, desc$Version) |
| 1231 |
writef("%s %s", boo(), format(pkgver, width = the$install_step_width)) |
| 1232 |
} |
| 1233 |
errors$push(list(package = package, message = "failed to download")) |
| 1234 |
failed$push(package) |
| 1235 |
next |
| 1236 |
} |
| 1237 |
|
| 1238 |
desc <- descriptions[[package]] |
| 1239 |
record <- as.list(desc) |
| 1240 |
record$Path <- info$destfile |
| 1241 |
|
| 1242 |
# tag repository records so renv_package_augment can write |
| 1243 |
# RemoteRepos / RemoteReposName into the installed DESCRIPTION |
| 1244 |
if (!is.null(info$repourl)) |
| 1245 |
record <- renv_record_tag(record, info$pkgtype, info$repourl, info$reponame) |
| 1246 |
|
| 1247 |
# tag cellar records with their file URI |
| 1248 |
if (!is.null(info$cellarurl)) |
| 1249 |
record <- renv_record_tag(record, info$type, info$cellarurl, "__renv_cellar__") |
| 1250 |
|
| 1251 |
# print progress for packages not already reported by the callback |
| 1252 |
if (!(package %in% streamed)) { |
| 1253 |
pkgver <- paste(record$Package, record$Version) |
| 1254 |
writef("%s %s", yay(), format(pkgver, width = the$install_step_width)) |
| 1255 |
} |
| 1256 |
|
| 1257 |
downloaded[[package]] <- record |
| 1258 |
|
| 1259 |
} |
| 1260 |
|
| 1261 |
count <- length(downloaded) |
| 1262 |
|
| 1263 |
} |
| 1264 |
|
| 1265 |
if (count > 0L) { |
| 1266 |
fmt <- "Successfully downloaded %s in %s.\n" |
| 1267 |
elapsed <- timer$tick() |
| 1268 |
writef(fmt, nplural("package", count), renv_difftime_format(elapsed)) |
| 1269 |
} |
| 1270 |
|
| 1271 |
writef(header("Installing packages")) |
| 1272 |
timer$tick() |
| 1273 |
|
| 1274 |
# install packages from the cache |
| 1275 |
for (pkg in names(cachehits)) { |
| 1276 |
entry <- cachehits[[pkg]] |
| 1277 |
renv_install_package_cache(entry$record, entry$cache, linker) |
| 1278 |
all[[pkg]] <- entry$record |
| 1279 |
} |
| 1280 |
|
| 1281 |
# ── Phase 2: Classify and install all packages ─────────────── |
| 1282 |
# Both binary and source packages participate in the same |
| 1283 |
# dependency-ordered installation. The only difference is the |
| 1284 |
# install method: binaries are file copies / extractions done |
| 1285 |
# synchronously; source packages are built via R CMD INSTALL |
| 1286 |
# in worker subprocesses. |
| 1287 |
|
| 1288 |
entries <- list() |
| 1289 |
|
| 1290 |
for (package in sort(setdiff(remaining, failed$data()))) { |
| 1291 |
|
| 1292 |
record <- downloaded[[package]] |
| 1293 |
if (is.null(record)) { |
| 1294 |
failed$push(package) |
| 1295 |
next |
| 1296 |
} |
| 1297 |
|
| 1298 |
type <- renv_graph_install_classify(record) |
| 1299 |
entries[[package]] <- list(record = record, type = type) |
| 1300 |
|
| 1301 |
} |
| 1302 |
|
| 1303 |
showstatus <- !testing() && !verbose && !ci() |
| 1304 |
|
| 1305 |
installnames <- setdiff(names(entries), failed$data()) |
| 1306 |
installdescs <- descriptions[intersect(installnames, names(descriptions))] |
| 1307 |
|
| 1308 |
# shared closure for processing one completed package; |
| 1309 |
# callbacks accumulates per-package backup-restore functions |
| 1310 |
callbacks <- list() |
| 1311 |
|
| 1312 |
handle <- function(pkg, result) { |
| 1313 |
entry <- entries[[pkg]] |
| 1314 |
installpath <- file.path(installdir, pkg) |
| 1315 |
isbinary <- identical(entry$type, "binary") |
| 1316 |
message <- if (isbinary) "installed binary" else "built from source" |
| 1317 |
|
| 1318 |
if (result$success && file.exists(installpath)) { |
| 1319 |
renv_install_step_ok(message, entry$record, elapsed = result$elapsed) |
| 1320 |
renv_graph_install_finalize(entry$record, entry$prepared, installdir, project, linkable) |
| 1321 |
all[[pkg]] <<- entry$record |
| 1322 |
} else { |
| 1323 |
unlink(installpath, recursive = TRUE) |
| 1324 |
renv_install_step_error(entry$record) |
| 1325 |
if (verbose) writeLines(result$output) |
| 1326 |
errors$push(list(package = pkg, message = "install failed", output = result$output)) |
| 1327 |
failed$push(pkg) |
| 1328 |
} |
| 1329 |
|
| 1330 |
callbacks[[pkg]]() |
| 1331 |
} |
| 1332 |
|
| 1333 |
if (getRversion() >= "4.0") { |
| 1334 |
|
| 1335 |
# ready-queue event loop (live Kahn's algorithm) |
| 1336 |
graph <- renv_graph_adjacency(installdescs) |
| 1337 |
indegree <- graph$indegree |
| 1338 |
revadj <- graph$revadj |
| 1339 |
|
| 1340 |
ready <- sort(names(indegree)[indegree == 0L]) |
| 1341 |
|
| 1342 |
server <- renv_socket_server() |
| 1343 |
defer(close(server$socket)) |
| 1344 |
active <- list() |
| 1345 |
|
| 1346 |
if (showstatus) |
| 1347 |
progress$reset("Installing", length(installnames)) |
| 1348 |
|
| 1349 |
timeout <- getOption("renv.install.timeout", default = 3600L) |
| 1350 |
deadline <- Sys.time() + timeout |
| 1351 |
|
| 1352 |
repeat { |
| 1353 |
|
| 1354 |
# fill worker slots from ready queue |
| 1355 |
while (length(ready) > 0L) { |
| 1356 |
pkg <- ready[1L] |
| 1357 |
|
| 1358 |
entry <- entries[[pkg]] |
| 1359 |
|
| 1360 |
# unpack (if needed) and prepare |
| 1361 |
prepared <- catch(renv_graph_install_unpack_and_prepare( |
| 1362 |
entry$record, entry$type, staging, installdir |
| 1363 |
)) |
| 1364 |
if (inherits(prepared, "error")) { |
| 1365 |
ready <- ready[-1L] |
| 1366 |
msg <- conditionMessage(prepared) |
| 1367 |
if (showstatus) progress$clear() |
| 1368 |
writef("- Failed to prepare '%s': %s", pkg, msg) |
| 1369 |
errors$push(list(package = pkg, message = msg)) |
| 1370 |
failed$push(pkg) |
| 1371 |
if (showstatus) progress$tick() |
| 1372 |
next |
| 1373 |
} |
| 1374 |
|
| 1375 |
entry$prepared <- prepared |
| 1376 |
entries[[pkg]] <- entry |
| 1377 |
|
| 1378 |
callbacks[[pkg]] <- renv_graph_install_backup(installdir, pkg) |
| 1379 |
|
| 1380 |
if (identical(entry$type, "binary")) { |
| 1381 |
|
| 1382 |
# binary packages are just file copies -- install synchronously |
| 1383 |
ready <- ready[-1L] |
| 1384 |
|
| 1385 |
t0 <- Sys.time() |
| 1386 |
result <- catch(renv_graph_install_binary(prepared)) |
| 1387 |
t1 <- Sys.time() |
| 1388 |
elapsed <- difftime(t1, t0, units = "auto") |
| 1389 |
|
| 1390 |
if (showstatus) { |
| 1391 |
progress$clear() |
| 1392 |
progress$tick() |
| 1393 |
} |
| 1394 |
|
| 1395 |
success <- !inherits(result, "error") |
| 1396 |
output <- if (success) character() else conditionMessage(result) |
| 1397 |
handle(pkg, list(success = success, output = output, elapsed = elapsed)) |
| 1398 |
|
| 1399 |
# update indegrees for dependents |
| 1400 |
if (success) { |
| 1401 |
for (dependent in revadj[[pkg]]) { |
| 1402 |
indegree[[dependent]] <- indegree[[dependent]] - 1L |
| 1403 |
if (indegree[[dependent]] == 0L) |
| 1404 |
ready <- c(ready, dependent) |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
} else { |
| 1409 |
|
| 1410 |
# source packages need a worker slot |
| 1411 |
if (length(active) >= jobs) |
| 1412 |
break |
| 1413 |
|
| 1414 |
ready <- ready[-1L] |
| 1415 |
active[[pkg]] <- renv_graph_install_launch_socket( |
| 1416 |
prepared, server$port |
| 1417 |
) |
| 1418 |
|
| 1419 |
} |
| 1420 |
|
| 1421 |
} |
| 1422 |
|
| 1423 |
if (length(active) == 0L && length(ready) == 0L) |
| 1424 |
break |
| 1425 |
|
| 1426 |
# if only ready (binary) packages remain, skip socketSelect |
| 1427 |
if (length(active) == 0L) |
| 1428 |
next |
| 1429 |
|
| 1430 |
if (showstatus) |
| 1431 |
progress$update(names(active)) |
| 1432 |
|
| 1433 |
# collect connected worker sockets into a named list |
| 1434 |
conns <- list() |
| 1435 |
for (pkg in names(active)) |
| 1436 |
if (!is.null(active[[pkg]]$conn)) |
| 1437 |
conns[[pkg]] <- active[[pkg]]$conn |
| 1438 |
|
| 1439 |
remaining <- as.double(deadline - Sys.time(), units = "secs") |
| 1440 |
if (remaining <= 0) { |
| 1441 |
for (pkg in names(active)) { |
| 1442 |
elapsed <- difftime( |
| 1443 |
Sys.time(), active[[pkg]]$start, units = "auto" |
| 1444 |
) |
| 1445 |
handle(pkg, list( |
| 1446 |
success = FALSE, |
| 1447 |
output = "worker process timed out", |
| 1448 |
elapsed = elapsed |
| 1449 |
)) |
| 1450 |
renv_graph_install_worker_cleanup(active[[pkg]]) |
| 1451 |
} |
| 1452 |
active <- list() |
| 1453 |
break |
| 1454 |
} |
| 1455 |
|
| 1456 |
# wait for activity on server socket (new connections) or |
| 1457 |
# worker sockets (results / EOF); server is always first |
| 1458 |
wait <- min(5, remaining) |
| 1459 |
sockets <- c(list(server$socket), conns) |
| 1460 |
flags <- socketSelect(sockets, write = FALSE, timeout = wait) |
| 1461 |
|
| 1462 |
if (!any(flags)) { |
| 1463 |
# check for workers that never connected (startup timeout) |
| 1464 |
for (pkg in names(active)) { |
| 1465 |
if (!is.null(active[[pkg]]$conn)) |
| 1466 |
next |
| 1467 |
elapsed <- as.double( |
| 1468 |
difftime(Sys.time(), active[[pkg]]$start, units = "secs") |
| 1469 |
) |
| 1470 |
if (elapsed > 60) { |
| 1471 |
if (showstatus) { |
| 1472 |
progress$clear() |
| 1473 |
progress$tick() |
| 1474 |
} |
| 1475 |
handle(pkg, list( |
| 1476 |
success = FALSE, |
| 1477 |
output = "worker failed to start", |
| 1478 |
elapsed = as.difftime(elapsed, units = "secs") |
| 1479 |
)) |
| 1480 |
renv_graph_install_worker_cleanup(active[[pkg]]) |
| 1481 |
active[[pkg]] <- NULL |
| 1482 |
if (showstatus && length(active) > 0L) |
| 1483 |
progress$update(names(active)) |
| 1484 |
} |
| 1485 |
} |
| 1486 |
next |
| 1487 |
} |
| 1488 |
|
| 1489 |
# handle new worker connections (server socket is first); |
| 1490 |
# errors here are transient -- the startup timeout (60s) |
| 1491 |
# handles workers that genuinely fail to connect |
| 1492 |
if (flags[[1L]]) { |
| 1493 |
conn <- tryCatch( |
| 1494 |
socketAccept( |
| 1495 |
server$socket, |
| 1496 |
open = "rb", |
| 1497 |
blocking = TRUE, |
| 1498 |
timeout = 30 |
| 1499 |
), |
| 1500 |
error = identity |
| 1501 |
) |
| 1502 |
if (!inherits(conn, "error")) { |
| 1503 |
pkg <- tryCatch(unserialize(conn), error = identity) |
| 1504 |
valid <- is.character(pkg) && (pkg %in% names(active)) |
| 1505 |
if (valid) { |
| 1506 |
active[[pkg]]$conn <- conn |
| 1507 |
} else { |
| 1508 |
close(conn) |
| 1509 |
} |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
# handle worker results (flags[-1L] corresponds to conns) |
| 1514 |
wflags <- flags[-1L] |
| 1515 |
for (pkg in names(conns)[wflags]) { |
| 1516 |
|
| 1517 |
if (is.null(active[[pkg]])) |
| 1518 |
next |
| 1519 |
|
| 1520 |
data <- tryCatch( |
| 1521 |
unserialize(active[[pkg]]$conn), |
| 1522 |
error = identity |
| 1523 |
) |
| 1524 |
|
| 1525 |
elapsed <- difftime( |
| 1526 |
time1 = Sys.time(), |
| 1527 |
time2 = active[[pkg]]$start, |
| 1528 |
units = "auto" |
| 1529 |
) |
| 1530 |
|
| 1531 |
if (showstatus) { |
| 1532 |
progress$clear() |
| 1533 |
progress$tick() |
| 1534 |
} |
| 1535 |
|
| 1536 |
result <- renv_graph_install_parse_result(data, elapsed) |
| 1537 |
|
| 1538 |
handle(pkg, result) |
| 1539 |
renv_graph_install_worker_cleanup(active[[pkg]]) |
| 1540 |
active[[pkg]] <- NULL |
| 1541 |
|
| 1542 |
# decrement dependents' indegree and enqueue newly ready |
| 1543 |
if (result$success) { |
| 1544 |
for (dependent in revadj[[pkg]]) { |
| 1545 |
indegree[[dependent]] <- indegree[[dependent]] - 1L |
| 1546 |
if (indegree[[dependent]] == 0L) |
| 1547 |
ready <- c(ready, dependent) |
| 1548 |
} |
| 1549 |
} |
| 1550 |
# on failure: dependents keep indegree > 0, never enqueued |
| 1551 |
|
| 1552 |
if (showstatus && length(active) > 0L) |
| 1553 |
progress$update(names(active)) |
| 1554 |
|
| 1555 |
} |
| 1556 |
|
| 1557 |
} |
| 1558 |
|
| 1559 |
# mark packages stuck due to failed dependencies |
| 1560 |
stuck <- names(indegree)[indegree > 0L] |
| 1561 |
stuck <- setdiff(stuck, failed$data()) |
| 1562 |
for (pkg in stuck) failed$push(pkg) |
| 1563 |
|
| 1564 |
} else { |
| 1565 |
|
| 1566 |
# R < 4.0 fallback: wave-based, pipe-based collection |
| 1567 |
waves <- renv_graph_waves(installdescs) |
| 1568 |
|
| 1569 |
remaining <- installnames |
| 1570 |
|
| 1571 |
if (showstatus && length(remaining) > 0L) { |
| 1572 |
progress$reset("Installing", length(remaining)) |
| 1573 |
progress$update(remaining) |
| 1574 |
} |
| 1575 |
|
| 1576 |
for (wave in waves) { |
| 1577 |
|
| 1578 |
wave <- setdiff(wave, failed$data()) |
| 1579 |
if (length(wave) == 0L) |
| 1580 |
next |
| 1581 |
|
| 1582 |
wave <- sort(wave) |
| 1583 |
|
| 1584 |
# install binary packages in this wave synchronously first |
| 1585 |
for (pkg in wave) { |
| 1586 |
entry <- entries[[pkg]] |
| 1587 |
if (!identical(entry$type, "binary")) |
| 1588 |
next |
| 1589 |
|
| 1590 |
prepared <- catch(renv_graph_install_unpack_and_prepare( |
| 1591 |
entry$record, "binary", staging, installdir |
| 1592 |
)) |
| 1593 |
if (inherits(prepared, "error")) { |
| 1594 |
msg <- conditionMessage(prepared) |
| 1595 |
if (showstatus) progress$clear() |
| 1596 |
writef("- Failed to prepare '%s': %s", pkg, msg) |
| 1597 |
errors$push(list(package = pkg, message = msg)) |
| 1598 |
failed$push(pkg) |
| 1599 |
if (showstatus) progress$tick() |
| 1600 |
remaining <- setdiff(remaining, pkg) |
| 1601 |
if (showstatus && length(remaining) > 0L) |
| 1602 |
progress$update(remaining) |
| 1603 |
next |
| 1604 |
} |
| 1605 |
|
| 1606 |
entry$prepared <- prepared |
| 1607 |
entries[[pkg]] <- entry |
| 1608 |
|
| 1609 |
callbacks[[pkg]] <- renv_graph_install_backup(installdir, pkg) |
| 1610 |
|
| 1611 |
t0 <- Sys.time() |
| 1612 |
result <- catch(renv_graph_install_binary(prepared)) |
| 1613 |
t1 <- Sys.time() |
| 1614 |
elapsed <- difftime(t1, t0, units = "auto") |
| 1615 |
|
| 1616 |
if (showstatus) { |
| 1617 |
progress$clear() |
| 1618 |
progress$tick() |
| 1619 |
} |
| 1620 |
|
| 1621 |
success <- !inherits(result, "error") |
| 1622 |
output <- if (success) character() else conditionMessage(result) |
| 1623 |
handle(pkg, list(success = success, output = output, elapsed = elapsed)) |
| 1624 |
remaining <- setdiff(remaining, pkg) |
| 1625 |
if (showstatus && length(remaining) > 0L) |
| 1626 |
progress$update(remaining) |
| 1627 |
} |
| 1628 |
|
| 1629 |
# install source packages in this wave via worker processes |
| 1630 |
sourcepkgs <- Filter(function(pkg) { |
| 1631 |
entry <- entries[[pkg]] |
| 1632 |
identical(entry$type, "source") && !(pkg %in% failed$data()) |
| 1633 |
}, wave) |
| 1634 |
|
| 1635 |
while (length(sourcepkgs) > 0L) { |
| 1636 |
|
| 1637 |
batch <- head(sourcepkgs, jobs) |
| 1638 |
sourcepkgs <- tail(sourcepkgs, -length(batch)) |
| 1639 |
|
| 1640 |
workers <- list() |
| 1641 |
|
| 1642 |
for (pkg in batch) { |
| 1643 |
|
| 1644 |
entry <- entries[[pkg]] |
| 1645 |
prepared <- catch(renv_graph_install_unpack_and_prepare( |
| 1646 |
entry$record, "source", staging, installdir |
| 1647 |
)) |
| 1648 |
if (inherits(prepared, "error")) { |
| 1649 |
msg <- conditionMessage(prepared) |
| 1650 |
if (showstatus) progress$clear() |
| 1651 |
writef("- Failed to prepare '%s': %s", pkg, msg) |
| 1652 |
errors$push(list(package = pkg, message = msg)) |
| 1653 |
failed$push(pkg) |
| 1654 |
if (showstatus) progress$tick() |
| 1655 |
remaining <- setdiff(remaining, pkg) |
| 1656 |
if (showstatus && length(remaining) > 0L) |
| 1657 |
progress$update(remaining) |
| 1658 |
next |
| 1659 |
} |
| 1660 |
|
| 1661 |
entry$prepared <- prepared |
| 1662 |
entries[[pkg]] <- entry |
| 1663 |
|
| 1664 |
callbacks[[pkg]] <- renv_graph_install_backup(installdir, pkg) |
| 1665 |
workers[[pkg]] <- renv_graph_install_launch(prepared) |
| 1666 |
|
| 1667 |
} |
| 1668 |
|
| 1669 |
for (pkg in names(workers)) { |
| 1670 |
result <- renv_graph_install_collect(workers[[pkg]]) |
| 1671 |
if (showstatus) { |
| 1672 |
progress$clear() |
| 1673 |
progress$tick() |
| 1674 |
} |
| 1675 |
handle(pkg, result) |
| 1676 |
remaining <- setdiff(remaining, pkg) |
| 1677 |
if (showstatus && length(remaining) > 0L) |
| 1678 |
progress$update(remaining) |
| 1679 |
} |
| 1680 |
|
| 1681 |
} |
| 1682 |
|
| 1683 |
} |
| 1684 |
|
| 1685 |
} |
| 1686 |
|
| 1687 |
# migrate staged packages into real library; |
| 1688 |
# if transactional and there were failures, skip migration entirely |
| 1689 |
# (the staging directory is cleaned up by defer, leaving the real |
| 1690 |
# library unchanged for a clean rollback) |
| 1691 |
trash <- NULL |
| 1692 |
transactional <- config$install.transactional() |
| 1693 |
if (staged && length(all) > 0L && !(transactional && !failed$empty())) { |
| 1694 |
|
| 1695 |
stagepaths <- file.path(templib, names(all)) |
| 1696 |
stagepaths <- stagepaths[file.exists(stagepaths)] |
| 1697 |
targets <- file.path(library, basename(stagepaths)) |
| 1698 |
|
| 1699 |
# move old installations into a single trash directory so they |
| 1700 |
# can be cleaned up in one pass after reporting success, rather |
| 1701 |
# than running a recursive unlink per package during migration |
| 1702 |
trash <- tempfile("renv-trash-", tmpdir = library) |
| 1703 |
dir.create(trash) |
| 1704 |
|
| 1705 |
for (target in targets) { |
| 1706 |
if (renv_file_exists(target)) { |
| 1707 |
ok <- file.rename(target, file.path(trash, basename(target))) |
| 1708 |
if (!ok) unlink(target, recursive = TRUE) |
| 1709 |
} else if (renv_file_broken(target)) { |
| 1710 |
renv_file_remove(target) |
| 1711 |
} |
| 1712 |
} |
| 1713 |
|
| 1714 |
names(targets) <- stagepaths |
| 1715 |
enumerate(targets, renv_file_move) |
| 1716 |
|
| 1717 |
# clear filebacked cache entries |
| 1718 |
descpaths <- file.path(targets, "DESCRIPTION") |
| 1719 |
renv_filebacked_clear("renv_description_read", descpaths) |
| 1720 |
renv_filebacked_clear("renv_hash_description", descpaths) |
| 1721 |
} |
| 1722 |
|
| 1723 |
n <- length(all) |
| 1724 |
if (n > 0L) { |
| 1725 |
fmt <- "Successfully installed %s in %s." |
| 1726 |
elapsed <- timer$tick() |
| 1727 |
writef(fmt, nplural("package", n), renv_difftime_format(elapsed)) |
| 1728 |
} |
| 1729 |
|
| 1730 |
# clean up old installations after reporting success |
| 1731 |
if (!is.null(trash)) |
| 1732 |
unlink(trash, recursive = TRUE) |
| 1733 |
|
| 1734 |
# report errors |
| 1735 |
renv_graph_install_errors(errors$data(), failed$data(), descriptions) |
| 1736 |
|
| 1737 |
invisible(all) |
| 1738 |
|
| 1739 |
} |
| 1740 |
|
| 1741 |
renv_graph_install_errors <- function(errors, failed, descriptions) { |
| 1742 |
|
| 1743 |
if (length(errors) == 0L && length(failed) == 0L) |
| 1744 |
return(invisible()) |
| 1745 |
|
| 1746 |
# build summary lines for packages that failed directly |
| 1747 |
messages <- map_chr(errors, function(item) { |
| 1748 |
sprintf("[%s]: %s", item$package, item$message) |
| 1749 |
}) |
| 1750 |
|
| 1751 |
# find packages skipped due to failed dependencies (not in errors themselves) |
| 1752 |
errpkgs <- map_chr(errors, `[[`, "package") |
| 1753 |
skipped <- setdiff(failed, errpkgs) |
| 1754 |
if (length(skipped) > 0L) { |
| 1755 |
skipmsgs <- map_chr(skipped, function(pkg) { |
| 1756 |
deps <- renv_graph_deps(descriptions[[pkg]]) |
| 1757 |
deps_failed <- intersect(deps, failed) |
| 1758 |
sprintf("[%s]: dependency failed (%s)", pkg, paste(deps_failed, collapse = ", ")) |
| 1759 |
}) |
| 1760 |
messages <- c(messages, skipmsgs) |
| 1761 |
} |
| 1762 |
|
| 1763 |
writef("") |
| 1764 |
bulletin( |
| 1765 |
"The following package(s) were not installed successfully:", |
| 1766 |
messages, |
| 1767 |
"You may need to manually download and install these packages." |
| 1768 |
) |
| 1769 |
|
| 1770 |
# show full R CMD INSTALL output for failed source installs |
| 1771 |
for (item in errors) { |
| 1772 |
|
| 1773 |
output <- item$output |
| 1774 |
if (is.null(output) || length(output) == 0L) |
| 1775 |
next |
| 1776 |
|
| 1777 |
writef(header(item$package)) |
| 1778 |
writef(output) |
| 1779 |
writef("") |
| 1780 |
|
| 1781 |
} |
| 1782 |
|
| 1783 |
invisible() |
| 1784 |
|
| 1785 |
} |
| 1786 |
|
| 1787 |
renv_graph_install_classify <- function(record) { |
| 1788 |
|
| 1789 |
# always inspect the archive or directory directly. The type attr |
| 1790 |
# on a repository record reflects the `type` argument passed to |
| 1791 |
# `available.packages()`, not the real contents of the downloaded |
| 1792 |
# file -- which is wrong for Posit Package Manager "binary" |
| 1793 |
# repositories, which serve binary packages at source-style URLs. |
| 1794 |
renv_package_type(record$Path, quiet = TRUE) |
| 1795 |
|
| 1796 |
} |
| 1797 |
|
| 1798 |
renv_graph_install_needs_unpack <- function(record, type) { |
| 1799 |
|
| 1800 |
# RemoteSubdir means the package lives in a subdirectory of the |
| 1801 |
# archive; we must extract to reach it |
| 1802 |
subdir <- record$RemoteSubdir %||% "" |
| 1803 |
if (nzchar(subdir)) |
| 1804 |
return(TRUE) |
| 1805 |
|
| 1806 |
# R CMD INSTALL only handles .tar.gz / .tgz archives; |
| 1807 |
# other formats (e.g. .zip) must be unpacked first |
| 1808 |
archtype <- renv_archive_type(record$Path) |
| 1809 |
if (!identical(archtype, "tar")) |
| 1810 |
return(TRUE) |
| 1811 |
|
| 1812 |
# install.build requires an unpacked directory for R CMD build; |
| 1813 |
# only relevant for source packages (binaries are already built) |
| 1814 |
identical(type, "source") && identical(config$install.build(), TRUE) |
| 1815 |
|
| 1816 |
} |
| 1817 |
|
| 1818 |
renv_graph_install_unpack_and_prepare <- function(record, type, staging, library) { |
| 1819 |
|
| 1820 |
package <- record$Package |
| 1821 |
path <- record$Path |
| 1822 |
isarchive <- identical(renv_file_info(path)$isdir, FALSE) |
| 1823 |
|
| 1824 |
# archives can often be used directly: R CMD INSTALL handles source |
| 1825 |
# archives natively, and binary archives can be extracted straight |
| 1826 |
# into the library. We only need to unpack up front when the |
| 1827 |
# package is nested (RemoteSubdir) or needs pre-building. |
| 1828 |
if (isarchive && !renv_graph_install_needs_unpack(record, type)) { |
| 1829 |
return(renv_graph_install_prepare(record, path, library, type, isarchive = TRUE)) |
| 1830 |
} |
| 1831 |
|
| 1832 |
# unpack archive or adjust path for RemoteSubdir |
| 1833 |
subdir <- record$RemoteSubdir %||% "" |
| 1834 |
if (isarchive) { |
| 1835 |
# renv_package_unpack creates a temp dir scoped to this function's |
| 1836 |
# caller (parent.frame()); move the result into the staging directory |
| 1837 |
# so it persists after this function returns |
| 1838 |
unpacked <- renv_package_unpack(package, path, subdir = subdir) |
| 1839 |
dest <- file.path(staging, package) |
| 1840 |
renv_file_move(unpacked, dest) |
| 1841 |
path <- dest |
| 1842 |
} else if (nzchar(subdir)) { |
| 1843 |
path <- paste(c(path, subdir), collapse = "/") |
| 1844 |
} |
| 1845 |
|
| 1846 |
# check whether we should pre-build |
| 1847 |
path <- renv_install_package_impl_prebuild(record, path, quiet = TRUE) |
| 1848 |
path <- renv_path_normalize(path, mustWork = TRUE) |
| 1849 |
|
| 1850 |
renv_graph_install_prepare(record, path, library, type, isarchive = FALSE) |
| 1851 |
|
| 1852 |
} |
| 1853 |
|
| 1854 |
renv_graph_install_prepare <- function(record, path, library, type, isarchive) { |
| 1855 |
|
| 1856 |
package <- record$Package |
| 1857 |
|
| 1858 |
if (identical(type, "binary")) { |
| 1859 |
|
| 1860 |
# binary directory: copy into library |
| 1861 |
# binary archive: extract directly into library |
| 1862 |
list( |
| 1863 |
type = "binary", |
| 1864 |
method = if (isarchive) "extract" else "copy", |
| 1865 |
path = path, |
| 1866 |
library = library, |
| 1867 |
package = package |
| 1868 |
) |
| 1869 |
|
| 1870 |
} else { |
| 1871 |
|
| 1872 |
# R CMD INSTALL handles both archives and directories |
| 1873 |
args <- c( |
| 1874 |
"--vanilla", |
| 1875 |
"CMD", "INSTALL", "--preclean", "--no-multiarch", |
| 1876 |
if (config$install.keep.source()) "--with-keep.source" else "--without-keep.source", |
| 1877 |
r_cmd_install_option(package, "configure.args", TRUE), |
| 1878 |
r_cmd_install_option(package, "configure.vars", TRUE), |
| 1879 |
r_cmd_install_option(package, c("install.opts", "INSTALL_opts"), FALSE), |
| 1880 |
"-l", renv_shell_path(library), |
| 1881 |
renv_shell_path(path) |
| 1882 |
) |
| 1883 |
|
| 1884 |
command <- paste(renv_shell_path(R()), paste(args, collapse = " ")) |
| 1885 |
|
| 1886 |
list( |
| 1887 |
type = "source", |
| 1888 |
method = "install", |
| 1889 |
command = command, |
| 1890 |
path = path, |
| 1891 |
library = library, |
| 1892 |
package = package |
| 1893 |
) |
| 1894 |
|
| 1895 |
} |
| 1896 |
|
| 1897 |
} |
| 1898 |
|
| 1899 |
renv_graph_install_launch <- function(prepared) { |
| 1900 |
|
| 1901 |
command <- paste(prepared$command, "2>&1") |
| 1902 |
|
| 1903 |
list( |
| 1904 |
connection = renv_pipe_create(command), |
| 1905 |
package = prepared$package, |
| 1906 |
start = Sys.time() |
| 1907 |
) |
| 1908 |
|
| 1909 |
} |
| 1910 |
|
| 1911 |
renv_graph_install_collect <- function(worker) { |
| 1912 |
|
| 1913 |
conn <- worker$connection |
| 1914 |
|
| 1915 |
# readLines blocks until the process exits (EOF on pipe); |
| 1916 |
# if the pipe breaks, the exit status from close() is the real signal |
| 1917 |
lines <- tryCatch(readLines(conn, warn = FALSE), error = identity) |
| 1918 |
if (inherits(lines, "error")) |
| 1919 |
lines <- character() |
| 1920 |
|
| 1921 |
# close() on a pipe returns the process status: 0 on success, |
| 1922 |
# non-zero on failure (encoding is platform-specific) |
| 1923 |
status <- close(conn) %||% 0L |
| 1924 |
elapsed <- difftime(Sys.time(), worker$start, units = "auto") |
| 1925 |
|
| 1926 |
list( |
| 1927 |
success = identical(status, 0L), |
| 1928 |
output = lines, |
| 1929 |
elapsed = elapsed |
| 1930 |
) |
| 1931 |
|
| 1932 |
} |
| 1933 |
|
| 1934 |
renv_graph_install_launch_socket <- function(prepared, port) { |
| 1935 |
|
| 1936 |
command <- paste(prepared$command, "2>&1") |
| 1937 |
package <- prepared$package |
| 1938 |
|
| 1939 |
# build a self-contained R script that: |
| 1940 |
# 1. connects to the parent immediately and sends the package name (hello) |
| 1941 |
# 2. runs the install command |
| 1942 |
# 3. sends the result on the same connection |
| 1943 |
# |
| 1944 |
# the persistent connection lets the parent detect worker death via |
| 1945 |
# socketSelect() + EOF, without needing PID files or polling |
| 1946 |
code <- expr({ |
| 1947 |
|
| 1948 |
main <- function() { |
| 1949 |
|
| 1950 |
# connect to socket server created by parent |
| 1951 |
conn <- socketConnection( |
| 1952 |
host = "127.0.0.1", |
| 1953 |
port = !!port, |
| 1954 |
open = "wb", |
| 1955 |
blocking = TRUE, |
| 1956 |
timeout = 60 |
| 1957 |
) |
| 1958 |
on.exit(close(conn), add = TRUE) |
| 1959 |
|
| 1960 |
# notify parent of the package we're installing |
| 1961 |
serialize(!!package, conn) |
| 1962 |
|
| 1963 |
# run the install command and send the result back; |
| 1964 |
# on success 'output' is a character vector with a "status" |
| 1965 |
# attribute; on error it's a condition object |
| 1966 |
output <- tryCatch( |
| 1967 |
suppressWarnings(system(!!command, intern = TRUE)), |
| 1968 |
error = identity |
| 1969 |
) |
| 1970 |
|
| 1971 |
serialize(output, conn) |
| 1972 |
|
| 1973 |
} |
| 1974 |
|
| 1975 |
main() |
| 1976 |
|
| 1977 |
}) |
| 1978 |
|
| 1979 |
script <- tempfile("renv-install-", fileext = ".R") |
| 1980 |
writeLines(deparse(code), con = script) |
| 1981 |
|
| 1982 |
system2( |
| 1983 |
command = R(), |
| 1984 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 1985 |
stdout = FALSE, |
| 1986 |
stderr = FALSE, |
| 1987 |
wait = FALSE |
| 1988 |
) |
| 1989 |
|
| 1990 |
list(package = package, start = Sys.time(), conn = NULL, script = script) |
| 1991 |
|
| 1992 |
} |
| 1993 |
|
| 1994 |
renv_graph_install_worker_cleanup <- function(worker) { |
| 1995 |
try(close(worker$conn), silent = TRUE) |
| 1996 |
unlink(worker$script) |
| 1997 |
} |
| 1998 |
|
| 1999 |
renv_graph_install_parse_result <- function(data, elapsed) { |
| 2000 |
|
| 2001 |
# NULL means the worker died before sending a result |
| 2002 |
if (is.null(data)) { |
| 2003 |
return(list( |
| 2004 |
success = FALSE, |
| 2005 |
output = "worker process exited unexpectedly", |
| 2006 |
elapsed = elapsed |
| 2007 |
)) |
| 2008 |
} |
| 2009 |
|
| 2010 |
# an error object means system() itself failed |
| 2011 |
if (inherits(data, "error")) { |
| 2012 |
return(list( |
| 2013 |
success = FALSE, |
| 2014 |
output = conditionMessage(data), |
| 2015 |
elapsed = elapsed |
| 2016 |
)) |
| 2017 |
} |
| 2018 |
|
| 2019 |
# otherwise, data is the character vector from system(intern = TRUE); |
| 2020 |
# the exit code lives in attr(data, "status") |
| 2021 |
status <- as.integer(attr(data, "status") %||% 0L) |
| 2022 |
|
| 2023 |
list( |
| 2024 |
success = identical(status, 0L), |
| 2025 |
output = data, |
| 2026 |
elapsed = elapsed |
| 2027 |
) |
| 2028 |
|
| 2029 |
} |
| 2030 |
|
| 2031 |
renv_graph_install_backup <- function(installdir, pkg) { |
| 2032 |
installpath <- file.path(installdir, pkg) |
| 2033 |
callback <- renv_file_backup(installpath) |
| 2034 |
if (renv_file_broken(installpath)) |
| 2035 |
renv_file_remove(installpath) |
| 2036 |
callback |
| 2037 |
} |
| 2038 |
|
| 2039 |
renv_graph_install_binary <- function(prepared) { |
| 2040 |
|
| 2041 |
package <- prepared$package |
| 2042 |
library <- prepared$library |
| 2043 |
installpath <- file.path(library, package) |
| 2044 |
|
| 2045 |
if (identical(prepared$method, "copy")) |
| 2046 |
renv_file_copy(prepared$path, installpath, overwrite = TRUE) |
| 2047 |
else |
| 2048 |
renv_archive_decompress(prepared$path, exdir = library) |
| 2049 |
|
| 2050 |
invisible(installpath) |
| 2051 |
|
| 2052 |
} |
| 2053 |
|
| 2054 |
renv_graph_install_finalize <- function(record, prepared, library, project, linkable) { |
| 2055 |
|
| 2056 |
package <- record$Package |
| 2057 |
installpath <- file.path(library, package) |
| 2058 |
|
| 2059 |
# test loading; R CMD INSTALL already tests source packages, |
| 2060 |
# so we only need this for binaries (copy / extract) |
| 2061 |
if (identical(prepared$type, "binary")) { |
| 2062 |
tryCatch( |
| 2063 |
renv_install_test(package), |
| 2064 |
error = function(err) { |
| 2065 |
unlink(installpath, recursive = TRUE) |
| 2066 |
stop(err) |
| 2067 |
} |
| 2068 |
) |
| 2069 |
} |
| 2070 |
|
| 2071 |
# augment package metadata |
| 2072 |
renv_package_augment(installpath, record) |
| 2073 |
|
| 2074 |
# synchronize with cache |
| 2075 |
if (renv_cache_config_enabled(project = project)) |
| 2076 |
renv_cache_synchronize(record, linkable = linkable) |
| 2077 |
|
| 2078 |
} |