diff --git a/NEWS.md b/NEWS.md index ae9bf37af..f512993bd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # renv (development version) +* Fixed an issue that could cause code within a project `.Rprofile` to execute + before the project had been loaded in RStudio. (#1650) + * `renv::snapshot()` and `renv::status()` gain the `dev` argument. This can be used when you'd like to capture package dependencies from the *Suggests* field of your package's DESCRIPTION file. (#1019) diff --git a/R/bootstrap.R b/R/bootstrap.R index a314eecef..61a0bca5f 100644 --- a/R/bootstrap.R +++ b/R/bootstrap.R @@ -957,16 +957,3 @@ renv_bootstrap_run <- function(version, libpath) { warning(paste(msg, collapse = "\n"), call. = FALSE) } - - -renv_bootstrap_in_rstudio <- function() { - commandArgs()[[1]] == "RStudio" -} - -# Used to work around buglet in RStudio if hook uses readline -renv_bootstrap_flush_console <- function() { - tryCatch({ - tools <- as.environment("tools:rstudio") - tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE) - }, error = function(cnd) {}) -} diff --git a/R/load.R b/R/load.R index dd0ef7b45..9325ebb4a 100644 --- a/R/load.R +++ b/R/load.R @@ -139,12 +139,23 @@ renv_load_action <- function(project) { if (!interactive()) return("load") - # if this project doesn't yet contain an 'renv' folder, assume - # that it has not yet been initialized, and prompt the user + # 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 + # 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) + }) + } + # 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"))) diff --git a/inst/resources/activate.R b/inst/resources/activate.R index aad8bc2bb..857bd0dc9 100644 --- a/inst/resources/activate.R +++ b/inst/resources/activate.R @@ -1034,19 +1034,6 @@ local({ } - - renv_bootstrap_in_rstudio <- function() { - commandArgs()[[1]] == "RStudio" - } - - # Used to work around buglet in RStudio if hook uses readline - renv_bootstrap_flush_console <- function() { - tryCatch({ - tools <- as.environment("tools:rstudio") - tools$.rs.api.sendToConsole("", echo = FALSE, focus = FALSE) - }, error = function(cnd) {}) - } - renv_json_read <- function(file = NULL, text = NULL) { jlerr <- NULL @@ -1185,16 +1172,8 @@ local({ # construct full libpath libpath <- file.path(root, prefix) - if (renv_bootstrap_in_rstudio()) { - # RStudio only updates console once .Rprofile is finished, so - # instead run code on sessionInit - setHook("rstudio.sessionInit", function(...) { - renv_bootstrap_exec(project, libpath, version) - renv_bootstrap_flush_console() - }) - } else { - renv_bootstrap_exec(project, libpath, version) - } + # run bootstrap code + renv_bootstrap_exec(project, libpath, version) invisible() diff --git a/templates/template-activate.R b/templates/template-activate.R index 05fb9f145..8d0801d26 100644 --- a/templates/template-activate.R +++ b/templates/template-activate.R @@ -88,16 +88,8 @@ local({ # construct full libpath libpath <- file.path(root, prefix) - if (renv_bootstrap_in_rstudio()) { - # RStudio only updates console once .Rprofile is finished, so - # instead run code on sessionInit - setHook("rstudio.sessionInit", function(...) { - renv_bootstrap_exec(project, libpath, version) - renv_bootstrap_flush_console() - }) - } else { - renv_bootstrap_exec(project, libpath, version) - } + # run bootstrap code + renv_bootstrap_exec(project, libpath, version) invisible()