From beb8d616a4a06c06644c7fdc25989438b6ad5715 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Wed, 29 May 2024 16:03:13 -0700 Subject: [PATCH 1/8] Re-order these checks --- R/load.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/load.R b/R/load.R index f2a438261..ab7e03407 100644 --- a/R/load.R +++ b/R/load.R @@ -146,12 +146,6 @@ renv_load_action <- function(project) { if (!interactive()) return("load") - # if this project already contains an 'renv' folder, assume it's - # already been initialized and we can directly load it - renv <- renv_paths_renv(project = project, profile = FALSE) - if (dir.exists(renv)) - return("load") - # if we're running within RStudio at this point, and we're running # within the auto-loader, we need to defer execution here so that # the console is able to properly receive user input and update @@ -162,6 +156,12 @@ renv_load_action <- function(project) { return("cancel") } + # if this project already contains an 'renv' folder, assume it's + # already been initialized and we can directly load it + renv <- renv_paths_renv(project = project, profile = FALSE) + if (dir.exists(renv)) + return("load") + # check and see if we're being called within a sub-directory path <- renv_file_find(dirname(project), function(parent) { if (file.exists(file.path(parent, "renv"))) From 2fb9e657eb794526e9df28976f6f6ed8af35160d Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Fri, 31 May 2024 11:46:47 -0700 Subject: [PATCH 2/8] Put some DOTS on it --- R/load.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/load.R b/R/load.R index ab7e03407..7c7a0d95d 100644 --- a/R/load.R +++ b/R/load.R @@ -152,7 +152,7 @@ renv_load_action <- function(project) { # https://github.com/rstudio/renv/issues/1650 autoloading <- getOption("renv.autoloader.running", default = FALSE) if (autoloading && renv_rstudio_available()) { - setHook("rstudio.sessionInit", function() { renv::load(project) }) + setHook("rstudio.sessionInit", function(...) { renv::load(project) }) return("cancel") } From f24aba793069cdac1efa1e9444f704d2db4f81c4 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Fri, 31 May 2024 12:46:05 -0700 Subject: [PATCH 3/8] Be precise about the recommended action --- R/load.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/load.R b/R/load.R index 7c7a0d95d..f35c701c8 100644 --- a/R/load.R +++ b/R/load.R @@ -881,7 +881,7 @@ renv_load_report_synchronized <- function(project = NULL, lockfile = NULL) { return(FALSE) } - response <- ask("Would you like to restore the project library?", default = FALSE) + response <- ask("Would you like to run `renv::restore()` to restore the project library?", default = FALSE) if (!response) return(FALSE) From 9d8635fbfc1b2f4307b3cdbf92a9a682ed5e892f Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Mon, 3 Jun 2024 19:17:55 -0700 Subject: [PATCH 4/8] Don't message about cancellation --- R/load.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/load.R b/R/load.R index f35c701c8..3ad5b8c0c 100644 --- a/R/load.R +++ b/R/load.R @@ -70,7 +70,7 @@ load <- function(project = NULL, quiet = FALSE, profile = NULL, ...) { action <- renv_load_action(project) if (action[[1L]] == "cancel") { - cancel() + quietly(cancel()) } else if (action[[1L]] == "init") { return(init(project)) } else if (action[[1L]] == "alt") { From 1989c79b380ffbbbfdaf639353f45f2047adc50c Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Fri, 7 Jun 2024 12:54:11 -0700 Subject: [PATCH 5/8] Refine action around renv::restore() during autoloading --- R/load.R | 17 ++++++++++++----- R/rstudio.R | 4 ---- R/utils.R | 14 ++++++++------ 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/R/load.R b/R/load.R index 3ad5b8c0c..3968eced9 100644 --- a/R/load.R +++ b/R/load.R @@ -146,11 +146,17 @@ renv_load_action <- function(project) { if (!interactive()) return("load") - # if we're running within RStudio at this point, and we're running - # within the auto-loader, we need to defer execution here so that - # the console is able to properly receive user input and update - # https://github.com/rstudio/renv/issues/1650 autoloading <- getOption("renv.autoloader.running", default = FALSE) + # if we're auto-loading, it's too early to interact with the user, which is + # often advisable, i.e. if we detect that the user needs to run renv::restore() + # https://github.com/rstudio/renv/issues/1650 + # + # if the frontend is known to support a session init hook, defer loading until + # R is fully initialized, at which time it will be possible to interact with + # the user (currently this just applies to RStudio) + # + # otherwise, proceed with the knowledge that, if the user needs to run + # renv::restore(), a message to that effect will be emitted if (autoloading && renv_rstudio_available()) { setHook("rstudio.sessionInit", function(...) { renv::load(project) }) return("cancel") @@ -876,7 +882,8 @@ renv_load_report_synchronized <- function(project = NULL, lockfile = NULL) { if (length(intersect(lockpkgs, libpkgs)) == 0 && length(lockpkgs) > 0L) { caution("- None of the packages recorded in the lockfile are currently installed.") - if (renv_rstudio_autoloading()) { + autoloading <- getOption("renv.autoloader.running", default = FALSE) + if (autoloading) { caution("- Use `renv::restore()` to restore the project library.") return(FALSE) } diff --git a/R/rstudio.R b/R/rstudio.R index 61fc25a2a..625b7cfd4 100644 --- a/R/rstudio.R +++ b/R/rstudio.R @@ -10,10 +10,6 @@ renv_rstudio_available <- function() { } -renv_rstudio_autoloading <- function() { - renv_rstudio_available() && getOption("renv.autoloader.running", default = FALSE) -} - renv_rstudio_initialize <- function(project) { tools <- catch(as.environment("tools:rstudio")) diff --git a/R/utils.R b/R/utils.R index 552d92d14..6c847cc7e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -126,13 +126,15 @@ ask <- function(question, default = FALSE) { if (!interactive()) return(default) - # can't prompt for input when running autoloader in RStudio + # can't prompt for input when autoloading; code run from `.Rprofile` should + # not attempt to interact with the user + # from `?Startup`: + # "It is not intended that there be interaction with the user during startup + # code. Attempting to do so can crash the R process." # https://github.com/rstudio/renv/issues/1879 - if (renv_rstudio_available()) { - autoloading <- getOption("renv.autoloader.running", default = FALSE) - if (autoloading) - return(default) - } + autoloading <- getOption("renv.autoloader.running", default = FALSE) + if (autoloading) + return(default) # be verbose in this scope, as we're asking the user for input renv_scope_options(renv.verbose = TRUE) From a11676f67491c2b27e4032fb2d71e627e5237321 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Fri, 7 Jun 2024 13:26:43 -0700 Subject: [PATCH 6/8] Make `renv::restore()` (maybe) a runnable hyperlink if cli is available --- R/load.R | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/load.R b/R/load.R index 3968eced9..84a2ad708 100644 --- a/R/load.R +++ b/R/load.R @@ -884,7 +884,13 @@ renv_load_report_synchronized <- function(project = NULL, lockfile = NULL) { caution("- None of the packages recorded in the lockfile are currently installed.") autoloading <- getOption("renv.autoloader.running", default = FALSE) if (autoloading) { - caution("- Use `renv::restore()` to restore the project library.") + if (requireNamespace("cli", quietly = TRUE)) { + cli::cli_bullets(c( + "*" = "Use {.run renv::restore()} to restore the project library." + )) + } else { + caution("- Use `renv::restore()` to restore the project library.") + } return(FALSE) } From ec7bf57613f21b6a33a2d5489c85c945c96d095e Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Mon, 24 Jun 2024 14:34:13 -0700 Subject: [PATCH 7/8] Introduce `cancel(verbose =)` --- R/load.R | 3 ++- R/utils.R | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/R/load.R b/R/load.R index 84a2ad708..12b0eca11 100644 --- a/R/load.R +++ b/R/load.R @@ -70,7 +70,8 @@ load <- function(project = NULL, quiet = FALSE, profile = NULL, ...) { action <- renv_load_action(project) if (action[[1L]] == "cancel") { - quietly(cancel()) + autoloading <- getOption("renv.autoloader.running", default = FALSE) + cancel(verbose = !autoloading) } else if (action[[1L]] == "init") { return(init(project)) } else if (action[[1L]] == "alt") { diff --git a/R/utils.R b/R/utils.R index 6c847cc7e..d6f24d3ed 100644 --- a/R/utils.R +++ b/R/utils.R @@ -521,13 +521,14 @@ take <- function(data, index = NULL) { if (is.null(index)) data else .subset2(data, index) } -cancel <- function() { +cancel <- function(verbose = TRUE) { renv_snapshot_auto_suppress_next() if (testing()) stop("Operation canceled", call. = FALSE) - message("- Operation canceled.") + if (verbose) + message("- Operation canceled.") invokeRestart("abort") } From f23c248eea0343307f1e7789267fec0d10513592 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Mon, 24 Jun 2024 15:57:21 -0700 Subject: [PATCH 8/8] Back out of the cli experiment --- R/load.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/R/load.R b/R/load.R index 12b0eca11..ff419ea6e 100644 --- a/R/load.R +++ b/R/load.R @@ -885,13 +885,7 @@ renv_load_report_synchronized <- function(project = NULL, lockfile = NULL) { caution("- None of the packages recorded in the lockfile are currently installed.") autoloading <- getOption("renv.autoloader.running", default = FALSE) if (autoloading) { - if (requireNamespace("cli", quietly = TRUE)) { - cli::cli_bullets(c( - "*" = "Use {.run renv::restore()} to restore the project library." - )) - } else { - caution("- Use `renv::restore()` to restore the project library.") - } + caution("- Use `renv::restore()` to restore the project library.") return(FALSE) }