diff --git a/NEWS.md b/NEWS.md index 8f4ea29a9..19f351537 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,12 @@ editor_options: markdown: wrap: 72 --- +# precommit v0.3.2.9015 + +* Update dependencies. +* Refactored hook dependency update scripts. +* Please see tag diff for more details. + # precommit v0.3.2.9013 * Don't depend on dev version of styler to avoid GitHub rate limit hit (#486). diff --git a/renv.lock b/renv.lock index 547c5d1bf..b22793e13 100644 --- a/renv.lock +++ b/renv.lock @@ -1,10 +1,14 @@ { "R": { - "Version": "4.2.1", + "Version": "4.3.0", "Repositories": [ { - "Name": "CRAN", + "Name": "RSPM", "URL": "https://packagemanager.rstudio.com/all/latest" + }, + { + "Name": "CRAN", + "URL": "https://cran.rstudio.com" } ] }, @@ -13,7 +17,7 @@ "Package": "R.cache", "Version": "0.16.0", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "R.methodsS3", @@ -28,7 +32,7 @@ "Package": "R.methodsS3", "Version": "1.8.2", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "utils" @@ -39,7 +43,7 @@ "Package": "R.oo", "Version": "1.25.0", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "R.methodsS3", @@ -67,7 +71,7 @@ "Package": "R6", "Version": "2.5.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R" ], @@ -75,20 +79,20 @@ }, "Rcpp": { "Package": "Rcpp", - "Version": "1.0.10", + "Version": "1.0.11", "Source": "Repository", "Repository": "RSPM", "Requirements": [ "methods", "utils" ], - "Hash": "e749cae40fa9ef469b6050959517453c" + "Hash": "ae6cbbe1492f4de79c45fce06f967ce8" }, "backports": { "Package": "backports", "Version": "1.4.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R" ], @@ -144,10 +148,10 @@ }, "cpp11": { "Package": "cpp11", - "Version": "0.4.3", + "Version": "0.4.5", "Source": "Repository", "Repository": "RSPM", - "Hash": "ed588261931ee3be2c700d22e94a29ab" + "Hash": "f07821e9b0aada6c999d4692e22a2ea7" }, "crayon": { "Package": "crayon", @@ -165,7 +169,7 @@ "Package": "cyclocomp", "Version": "1.1.0", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "callr", "crayon", @@ -179,7 +183,7 @@ "Package": "desc", "Version": "1.4.2", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "R6", @@ -191,20 +195,20 @@ }, "digest": { "Package": "digest", - "Version": "0.6.31", + "Version": "0.6.33", "Source": "Repository", "Repository": "RSPM", "Requirements": [ "R", "utils" ], - "Hash": "8b708f296afd9ae69f450f9640be8990" + "Hash": "b18a9cf3c003977b0cc49d5e76ebe48d" }, "docopt": { "Package": "docopt", "Version": "0.7.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "methods" ], @@ -212,39 +216,43 @@ }, "evaluate": { "Package": "evaluate", - "Version": "0.20", + "Version": "0.21", "Source": "Repository", "Repository": "RSPM", "Requirements": [ "R", "methods" ], - "Hash": "4b68aa51edd89a0e044a66e75ae3cc6c" + "Hash": "d59f3b464e8da1aef82dc04b588b8dfb" }, "fs": { "Package": "fs", - "Version": "1.6.1", + "Version": "1.6.3", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "methods" ], - "Hash": "f4dcd23b67e33d851d2079f703e8b985" + "Hash": "47b5f30c720c23999b913a1a635cf0bb" }, "git2r": { "Package": "git2r", - "Version": "0.30.1", + "Version": "0.32.0", "Source": "Repository", - "Repository": "CRAN", - "Hash": "e0c6a04a3e7b90e64213d09128f74f1b", - "Requirements": [] + "Repository": "RSPM", + "Requirements": [ + "R", + "graphics", + "utils" + ], + "Hash": "1882d7a76fd8c14b2322865f74c9a348" }, "glue": { "Package": "glue", "Version": "1.6.2", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "methods" @@ -255,7 +263,7 @@ "Package": "here", "Version": "1.0.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "rprojroot" ], @@ -265,7 +273,7 @@ "Package": "highr", "Version": "0.10", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "xfun" @@ -276,7 +284,7 @@ "Package": "hunspell", "Version": "3.0.2", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "Rcpp", @@ -284,19 +292,9 @@ ], "Hash": "656219b6f3f605499d7cdbe208656639" }, - "jsonlite": { - "Package": "jsonlite", - "Version": "1.8.4", - "Source": "Repository", - "Repository": "RSPM", - "Requirements": [ - "methods" - ], - "Hash": "a4269a09a9b865579b2635c77e572374" - }, "knitr": { "Package": "knitr", - "Version": "1.42", + "Version": "1.43", "Source": "Repository", "Repository": "RSPM", "Requirements": [ @@ -308,13 +306,13 @@ "xfun", "yaml" ], - "Hash": "8329a9bcc82943c8069104d4be3ee22d" + "Hash": "9775eb076713f627c07ce41d8199d8f6" }, "lazyeval": { "Package": "lazyeval", "Version": "0.2.2", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R" ], @@ -335,18 +333,21 @@ }, "lintr": { "Package": "lintr", - "Version": "3.0.2", - "Source": "Repository", - "Repository": "RSPM", + "Version": "3.0.2.9000", + "Source": "GitHub", + "RemoteType": "github", + "RemoteHost": "api.github.com", + "RemoteUsername": "r-lib", + "RemoteRepo": "lintr", + "RemoteRef": "main", + "RemoteSha": "4ef15f9689e7384e83a8e3250825006e95293bf5", "Requirements": [ "R", "backports", "codetools", - "crayon", "cyclocomp", "digest", "glue", - "jsonlite", "knitr", "rex", "stats", @@ -354,13 +355,13 @@ "xml2", "xmlparsedata" ], - "Hash": "b21ebd652d940f099915221f3328ab7b" + "Hash": "fd19bcfedd9515297fdc318cbe7505d3" }, "magrittr": { "Package": "magrittr", "Version": "2.0.3", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R" ], @@ -368,7 +369,7 @@ }, "pkgload": { "Package": "pkgload", - "Version": "1.3.2", + "Version": "1.3.2.1", "Source": "Repository", "Repository": "RSPM", "Requirements": [ @@ -384,11 +385,11 @@ "utils", "withr" ], - "Hash": "6b0c222c5071efe0f3baf3dae9aa40e2" + "Hash": "a7f498a1b2a4a6816148e498509f6e1d" }, "processx": { "Package": "processx", - "Version": "3.8.0", + "Version": "3.8.2", "Source": "Repository", "Repository": "RSPM", "Requirements": [ @@ -397,24 +398,24 @@ "ps", "utils" ], - "Hash": "a33ee2d9bf07564efb888ad98410da84" + "Hash": "3efbd8ac1be0296a46c55387aeace0f3" }, "ps": { "Package": "ps", - "Version": "1.7.4", + "Version": "1.7.5", "Source": "Repository", "Repository": "RSPM", "Requirements": [ "R", "utils" ], - "Hash": "d88be14c6790aa6fd7b27a2079a45a85" + "Hash": "709d852d33178db54b17c722e5b1e594" }, "purrr": { "Package": "purrr", "Version": "1.0.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "cli", @@ -427,9 +428,9 @@ }, "remotes": { "Package": "remotes", - "Version": "2.4.2", + "Version": "2.4.2.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "methods", @@ -437,13 +438,23 @@ "tools", "utils" ], - "Hash": "227045be9aee47e6dda9bb38ac870d67" + "Hash": "63d15047eb239f95160112bcadc4fcb9" + }, + "renv": { + "Package": "renv", + "Version": "1.0.0", + "Source": "Repository", + "Repository": "RSPM", + "Requirements": [ + "utils" + ], + "Hash": "c321cd99d56443dbffd1c9e673c0c1a2" }, "rex": { "Package": "rex", "Version": "1.2.1", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "lazyeval" ], @@ -451,14 +462,14 @@ }, "rlang": { "Package": "rlang", - "Version": "1.1.0", + "Version": "1.1.1", "Source": "Repository", "Repository": "RSPM", "Requirements": [ "R", "utils" ], - "Hash": "dc079ccd156cde8647360f473c1fa718" + "Hash": "a85c767b55f0bf9b7ad16c6d7baee5bb" }, "roxygen2": { "Package": "roxygen2", @@ -490,7 +501,7 @@ "Package": "rprojroot", "Version": "2.0.3", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R" ], @@ -541,14 +552,9 @@ }, "styler": { "Package": "styler", - "Version": "1.9.1", - "Source": "GitHub", - "RemoteType": "github", - "RemoteUsername": "r-lib", - "RemoteRepo": "styler", - "RemoteRef": "HEAD", - "RemoteSha": "940010d41129746d44e52d44664a37e44559bd39", - "RemoteHost": "api.github.com", + "Version": "1.10.1", + "Source": "Repository", + "Repository": "RSPM", "Requirements": [ "R", "R.cache", @@ -561,11 +567,11 @@ "vctrs", "withr" ], - "Hash": "9d009491751064fc1cccc64ae7e248a7" + "Hash": "b0911fdb2c682f526f6e9c131fd40a1f" }, "vctrs": { "Package": "vctrs", - "Version": "0.6.1", + "Version": "0.6.3", "Source": "Repository", "Repository": "RSPM", "Requirements": [ @@ -575,13 +581,13 @@ "lifecycle", "rlang" ], - "Hash": "06eceb3a5d716fd0654cc23ca3d71a99" + "Hash": "d0ef2856b83dc33ea6e255caf6229ee2" }, "withr": { "Package": "withr", "Version": "2.5.0", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "grDevices", @@ -592,31 +598,31 @@ }, "xfun": { "Package": "xfun", - "Version": "0.38", + "Version": "0.39", "Source": "Repository", "Repository": "RSPM", "Requirements": [ "stats", "tools" ], - "Hash": "1ed71215d45e85562d3b1b29a068ccec" + "Hash": "8f56e9acb54fb525e66464d57ab58bcb" }, "xml2": { "Package": "xml2", - "Version": "1.3.3", + "Version": "1.3.5", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R", "methods" ], - "Hash": "40682ed6a969ea5abfd351eb67833adc" + "Hash": "6c40e5cfcc6aefd88110666e18c31f40" }, "xmlparsedata": { "Package": "xmlparsedata", "Version": "1.0.5", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Requirements": [ "R" ], @@ -626,7 +632,7 @@ "Package": "yaml", "Version": "2.3.7", "Source": "Repository", - "Repository": "CRAN", + "Repository": "RSPM", "Hash": "0d0056cc5383fbc240ccd0cb584bf436" } } diff --git a/renv/activate.R b/renv/activate.R index 4564b9ddf..cc742fc96 100644 --- a/renv/activate.R +++ b/renv/activate.R @@ -2,7 +2,8 @@ local({ # the requested version of renv - version <- "0.16.0" + version <- "1.0.0" + attr(version, "sha") <- NULL # the project directory project <- getwd() @@ -60,25 +61,75 @@ local({ # load bootstrap tools `%||%` <- function(x, y) { - if (is.environment(x) || length(x)) x else y + if (is.null(x)) y else x } - `%??%` <- function(x, y) { - if (is.null(x)) y else x + catf <- function(fmt, ..., appendLF = TRUE) { + + quiet <- getOption("renv.bootstrap.quiet", default = FALSE) + if (quiet) + return(invisible()) + + msg <- sprintf(fmt, ...) + cat(msg, file = stdout(), sep = if (appendLF) "\n" else "") + + invisible(msg) + + } + + header <- function(label, + ..., + prefix = "#", + suffix = "-", + n = min(getOption("width"), 78)) + { + label <- sprintf(label, ...) + n <- max(n - nchar(label) - nchar(prefix) - 2L, 8L) + if (n <= 0) + return(paste(prefix, label)) + + tail <- paste(rep.int(suffix, n), collapse = "") + paste0(prefix, " ", label, " ", tail) + + } + + startswith <- function(string, prefix) { + substring(string, 1, nchar(prefix)) == prefix } bootstrap <- function(version, library) { + friendly <- renv_bootstrap_version_friendly(version) + section <- header(sprintf("Bootstrapping renv %s", friendly)) + catf(section) + # attempt to download renv - tarball <- tryCatch(renv_bootstrap_download(version), error = identity) - if (inherits(tarball, "error")) - stop("failed to download renv ", version) + catf("- Downloading renv ... ", appendLF = FALSE) + withCallingHandlers( + tarball <- renv_bootstrap_download(version), + error = function(err) { + catf("FAILED") + stop("failed to download:\n", conditionMessage(err)) + } + ) + catf("OK") + on.exit(unlink(tarball), add = TRUE) # now attempt to install - status <- tryCatch(renv_bootstrap_install(version, tarball, library), error = identity) - if (inherits(status, "error")) - stop("failed to install renv ", version) + catf("- Installing renv ... ", appendLF = FALSE) + withCallingHandlers( + status <- renv_bootstrap_install(version, tarball, library), + error = function(err) { + catf("FAILED") + stop("failed to install:\n", conditionMessage(err)) + } + ) + catf("OK") + + # add empty line to break up bootstrapping from normal output + catf("") + return(invisible()) } renv_bootstrap_tests_running <- function() { @@ -108,13 +159,6 @@ local({ if (!inherits(repos, "error") && length(repos)) return(repos) - # if we're testing, re-use the test repositories - if (renv_bootstrap_tests_running()) { - repos <- getOption("renv.tests.repos") - if (!is.null(repos)) - return(repos) - } - # retrieve current repos repos <- getOption("repos") @@ -158,33 +202,34 @@ local({ renv_bootstrap_download <- function(version) { - # if the renv version number has 4 components, assume it must - # be retrieved via github - nv <- numeric_version(version) - components <- unclass(nv)[[1]] - - # if this appears to be a development version of 'renv', we'll - # try to restore from github - dev <- length(components) == 4L - - # begin collecting different methods for finding renv - methods <- c( - renv_bootstrap_download_tarball, - if (dev) - renv_bootstrap_download_github - else c( - renv_bootstrap_download_cran_latest, - renv_bootstrap_download_cran_archive + sha <- attr(version, "sha", exact = TRUE) + + methods <- if (!is.null(sha)) { + + # attempting to bootstrap a development version of renv + c( + function() renv_bootstrap_download_tarball(sha), + function() renv_bootstrap_download_github(sha) ) - ) + + } else { + + # attempting to bootstrap a release version of renv + c( + function() renv_bootstrap_download_tarball(version), + function() renv_bootstrap_download_cran_latest(version), + function() renv_bootstrap_download_cran_archive(version) + ) + + } for (method in methods) { - path <- tryCatch(method(version), error = identity) + path <- tryCatch(method(), error = identity) if (is.character(path) && file.exists(path)) return(path) } - stop("failed to download renv ", version) + stop("All download methods failed") } @@ -248,8 +293,6 @@ local({ type <- spec$type repos <- spec$repos - message("* Downloading renv ", version, " ... ", appendLF = FALSE) - baseurl <- utils::contrib.url(repos = repos, type = type) ext <- if (identical(type, "source")) ".tar.gz" @@ -266,13 +309,10 @@ local({ condition = identity ) - if (inherits(status, "condition")) { - message("FAILED") + if (inherits(status, "condition")) return(FALSE) - } # report success and return - message("OK (downloaded ", type, ")") destfile } @@ -329,8 +369,6 @@ local({ urls <- file.path(repos, "src/contrib/Archive/renv", name) destfile <- file.path(tempdir(), name) - message("* Downloading renv ", version, " ... ", appendLF = FALSE) - for (url in urls) { status <- tryCatch( @@ -338,14 +376,11 @@ local({ condition = identity ) - if (identical(status, 0L)) { - message("OK") + if (identical(status, 0L)) return(destfile) - } } - message("FAILED") return(FALSE) } @@ -368,7 +403,7 @@ local({ if (!file.exists(tarball)) { # let the user know we weren't able to honour their request - fmt <- "* RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist." + fmt <- "- RENV_BOOTSTRAP_TARBALL is set (%s) but does not exist." msg <- sprintf(fmt, tarball) warning(msg) @@ -377,10 +412,7 @@ local({ } - fmt <- "* Bootstrapping with tarball at path '%s'." - msg <- sprintf(fmt, tarball) - message(msg) - + catf("- Using local tarball '%s'.", tarball) tarball } @@ -407,8 +439,6 @@ local({ on.exit(do.call(base::options, saved), add = TRUE) } - message("* Downloading renv ", version, " from GitHub ... ", appendLF = FALSE) - url <- file.path("https://api.github.com/repos/rstudio/renv/tarball", version) name <- sprintf("renv_%s.tar.gz", version) destfile <- file.path(tempdir(), name) @@ -418,26 +448,105 @@ local({ condition = identity ) - if (!identical(status, 0L)) { - message("FAILED") + if (!identical(status, 0L)) return(FALSE) - } - message("OK") + renv_bootstrap_download_augment(destfile) + return(destfile) } + # Add Sha to DESCRIPTION. This is stop gap until #890, after which we + # can use renv::install() to fully capture metadata. + renv_bootstrap_download_augment <- function(destfile) { + sha <- renv_bootstrap_git_extract_sha1_tar(destfile) + if (is.null(sha)) { + return() + } + + # Untar + tempdir <- tempfile("renv-github-") + on.exit(unlink(tempdir, recursive = TRUE), add = TRUE) + untar(destfile, exdir = tempdir) + pkgdir <- dir(tempdir, full.names = TRUE)[[1]] + + # Modify description + desc_path <- file.path(pkgdir, "DESCRIPTION") + desc_lines <- readLines(desc_path) + remotes_fields <- c( + "RemoteType: github", + "RemoteHost: api.github.com", + "RemoteRepo: renv", + "RemoteUsername: rstudio", + "RemotePkgRef: rstudio/renv", + paste("RemoteRef: ", sha), + paste("RemoteSha: ", sha) + ) + writeLines(c(desc_lines[desc_lines != ""], remotes_fields), con = desc_path) + + # Re-tar + local({ + old <- setwd(tempdir) + on.exit(setwd(old), add = TRUE) + + tar(destfile, compression = "gzip") + }) + invisible() + } + + # Extract the commit hash from a git archive. Git archives include the SHA1 + # hash as the comment field of the tarball pax extended header + # (see https://www.kernel.org/pub/software/scm/git/docs/git-archive.html) + # For GitHub archives this should be the first header after the default one + # (512 byte) header. + renv_bootstrap_git_extract_sha1_tar <- function(bundle) { + + # open the bundle for reading + # We use gzcon for everything because (from ?gzcon) + # > Reading from a connection which does not supply a ‘gzip’ magic + # > header is equivalent to reading from the original connection + conn <- gzcon(file(bundle, open = "rb", raw = TRUE)) + on.exit(close(conn)) + + # The default pax header is 512 bytes long and the first pax extended header + # with the comment should be 51 bytes long + # `52 comment=` (11 chars) + 40 byte SHA1 hash + len <- 0x200 + 0x33 + res <- rawToChar(readBin(conn, "raw", n = len)[0x201:len]) + + if (grepl("^52 comment=", res)) { + sub("52 comment=", "", res) + } else { + NULL + } + } + renv_bootstrap_install <- function(version, tarball, library) { # attempt to install it into project library - message("* Installing renv ", version, " ... ", appendLF = FALSE) dir.create(library, showWarnings = FALSE, recursive = TRUE) + output <- renv_bootstrap_install_impl(library, tarball) + + # check for successful install + status <- attr(output, "status") + if (is.null(status) || identical(status, 0L)) + return(status) + + # an error occurred; report it + header <- "installation of renv failed" + lines <- paste(rep.int("=", nchar(header)), collapse = "") + text <- paste(c(header, lines, output), collapse = "\n") + stop(text) + + } + + renv_bootstrap_install_impl <- function(library, tarball) { # invoke using system2 so we can capture and report output bin <- R.home("bin") exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R" - r <- file.path(bin, exe) + R <- file.path(bin, exe) args <- c( "--vanilla", "CMD", "INSTALL", "--no-multiarch", @@ -445,19 +554,7 @@ local({ shQuote(path.expand(tarball)) ) - output <- system2(r, args, stdout = TRUE, stderr = TRUE) - message("Done!") - - # check for successful install - status <- attr(output, "status") - if (is.numeric(status) && !identical(status, 0L)) { - header <- "Error installing renv:" - lines <- paste(rep.int("=", nchar(header)), collapse = "") - text <- c(header, lines, output) - writeLines(text, con = stderr()) - } - - status + system2(R, args, stdout = TRUE, stderr = TRUE) } @@ -667,34 +764,60 @@ local({ } - renv_bootstrap_validate_version <- function(version) { + renv_bootstrap_validate_version <- function(version, description = NULL) { - loadedversion <- utils::packageDescription("renv", fields = "Version") - if (version == loadedversion) - return(TRUE) + # resolve description file + description <- description %||% { + path <- getNamespaceInfo("renv", "path") + packageDescription("renv", lib.loc = dirname(path)) + } - # assume four-component versions are from GitHub; - # three-component versions are from CRAN - components <- strsplit(loadedversion, "[.-]")[[1]] - remote <- if (length(components) == 4L) - paste("rstudio/renv", loadedversion, sep = "@") + # check whether requested version 'version' matches loaded version of renv + sha <- attr(version, "sha", exact = TRUE) + valid <- if (!is.null(sha)) + renv_bootstrap_validate_version_dev(sha, description) else - paste("renv", loadedversion, sep = "@") + renv_bootstrap_validate_version_release(version, description) + + if (valid) + return(TRUE) + + # the loaded version of renv doesn't match the requested version; + # give the user instructions on how to proceed + remote <- if (!is.null(description[["RemoteSha"]])) { + paste("rstudio/renv", description[["RemoteSha"]], sep = "@") + } else { + paste("renv", description[["Version"]], sep = "@") + } + + # display both loaded version + sha if available + friendly <- renv_bootstrap_version_friendly( + version = description[["Version"]], + sha = description[["RemoteSha"]] + ) fmt <- paste( "renv %1$s was loaded from project library, but this project is configured to use renv %2$s.", - "Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.", - "Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.", + "- Use `renv::record(\"%3$s\")` to record renv %1$s in the lockfile.", + "- Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.", sep = "\n" ) - - msg <- sprintf(fmt, loadedversion, version, remote) - warning(msg, call. = FALSE) + catf(fmt, friendly, renv_bootstrap_version_friendly(version), remote) FALSE } + renv_bootstrap_validate_version_dev <- function(version, description) { + expected <- description[["RemoteSha"]] + is.character(expected) && startswith(expected, version) + } + + renv_bootstrap_validate_version_release <- function(version, description) { + expected <- description[["Version"]] + is.character(expected) && identical(expected, version) + } + renv_bootstrap_hash_text <- function(text) { hashfile <- tempfile("renv-hash-") @@ -859,6 +982,40 @@ local({ } + renv_bootstrap_version_friendly <- function(version, sha = NULL) { + sha <- sha %||% attr(version, "sha", exact = TRUE) + parts <- c(version, sprintf("[sha: %s]", substring(sha, 1L, 7L))) + paste(parts, collapse = " ") + } + + renv_bootstrap_run <- function(version, libpath) { + + # perform bootstrap + bootstrap(version, libpath) + + # exit early if we're just testing bootstrap + if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA))) + return(TRUE) + + # try again to load + if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) { + return(renv::load(project = getwd())) + } + + # failed to download or load renv; warn the user + msg <- c( + "Failed to find an renv installation: the project will not be loaded.", + "Use `renv::activate()` to re-initialize the project." + ) + + warning(paste(msg, collapse = "\n"), call. = FALSE) + + } + + + renv_bootstrap_in_rstudio <- function() { + commandArgs()[[1]] == "RStudio" + } renv_json_read <- function(file = NULL, text = NULL) { @@ -1002,31 +1159,23 @@ local({ if (renv_bootstrap_load(project, libpath, version)) return(TRUE) - # load failed; inform user we're about to bootstrap - prefix <- paste("# Bootstrapping renv", version) - postfix <- paste(rep.int("-", 77L - nchar(prefix)), collapse = "") - header <- paste(prefix, postfix) - message(header) + if (renv_bootstrap_in_rstudio()) { + setHook("rstudio.sessionInit", function(...) { + renv_bootstrap_run(version, libpath) - # perform bootstrap - bootstrap(version, libpath) - - # exit early if we're just testing bootstrap - if (!is.na(Sys.getenv("RENV_BOOTSTRAP_INSTALL_ONLY", unset = NA))) - return(TRUE) - - # try again to load - if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) { - message("* Successfully installed and loaded renv ", version, ".") - return(renv::load()) + # Work around buglet in RStudio if hook uses readline + tryCatch( + { + tools <- as.environment("tools:rstudio") + tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE) + }, + error = function(cnd) {} + ) + }) + } else { + renv_bootstrap_run(version, libpath) } - # failed to download or load renv; warn the user - msg <- c( - "Failed to find an renv installation: the project will not be loaded.", - "Use `renv::activate()` to re-initialize the project." - ) - - warning(paste(msg, collapse = "\n"), call. = FALSE) + invisible() })