| 1 |
|
| 2 |
.onLoad <- function(libname, pkgname) { |
| 3 |
|
| 4 |
# NOTE: needs to be visible to embedded instances of renv as well |
| 5 |
the$envir_self <<- renv_envir_self() |
| 6 |
|
| 7 |
# load extensions if available |
| 8 |
renv_ext_onload(libname, pkgname) |
| 9 |
|
| 10 |
# make sure renv (and packages using renv!!!) use tempdir for storage |
| 11 |
# when running tests, or R CMD check |
| 12 |
if (checking() || testing()) { |
| 13 |
|
| 14 |
# set root directory |
| 15 |
root <- Sys.getenv("RENV_PATHS_ROOT", unset = tempfile("renv-root-")) |
| 16 |
Sys.setenv(RENV_PATHS_ROOT = root) |
| 17 |
|
| 18 |
# unset on exit |
| 19 |
reg.finalizer(renv_envir_self(), function(envir) { |
| 20 |
if (identical(root, Sys.getenv("RENV_PATHS_ROOT", unset = NA))) |
| 21 |
Sys.unsetenv("RENV_PATHS_ROOT") |
| 22 |
}, onexit = TRUE) |
| 23 |
|
| 24 |
# set up sandbox -- only done on non-Windows due to strange intermittent |
| 25 |
# test failures that seemed to occur there? |
| 26 |
if (renv_platform_unix()) { |
| 27 |
sandbox <- Sys.getenv("RENV_PATHS_SANDBOX", unset = tempfile("renv-sandbox-")) |
| 28 |
Sys.setenv(RENV_PATHS_SANDBOX = sandbox) |
| 29 |
} |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
# don't lock sandbox while testing / checking |
| 34 |
if (testing() || checking() || devmode()) { |
| 35 |
options(renv.sandbox.locking_enabled = FALSE) |
| 36 |
Sys.setenv(RENV_SANDBOX_LOCKING_ENABLED = FALSE) |
| 37 |
} |
| 38 |
|
| 39 |
renv_defer_init() |
| 40 |
renv_metadata_init() |
| 41 |
renv_ext_init() |
| 42 |
renv_ansify_init() |
| 43 |
renv_platform_init() |
| 44 |
renv_virtualization_init() |
| 45 |
renv_envvars_init() |
| 46 |
renv_log_init() |
| 47 |
renv_methods_init() |
| 48 |
renv_libpaths_init() |
| 49 |
renv_patch_init() |
| 50 |
renv_sandbox_init() |
| 51 |
renv_sdkroot_init() |
| 52 |
renv_watchdog_init() |
| 53 |
renv_tempdir_init() |
| 54 |
|
| 55 |
if (!renv_metadata_embedded()) { |
| 56 |
|
| 57 |
# TODO: It's not clear if these callbacks are safe to use when renv is |
| 58 |
# embedded, but it's unlikely that clients would want them anyhow. |
| 59 |
renv_task_create(renv_sandbox_task) |
| 60 |
renv_task_create(renv_snapshot_task) |
| 61 |
} |
| 62 |
|
| 63 |
# if an renv project already appears to be loaded, then re-activate |
| 64 |
# the sandbox now -- this is primarily done to support suspend and |
| 65 |
# resume with RStudio where the user profile might not have been run, |
| 66 |
# but RStudio would have restored options from the prior session |
| 67 |
# |
| 68 |
# https://github.com/rstudio/renv/issues/2036 |
| 69 |
if (renv_rstudio_available()) { |
| 70 |
project <- getOption("renv.project.path") |
| 71 |
if (!is.null(project)) { |
| 72 |
renv_project_set(project) |
| 73 |
renv_sandbox_activate(project = project) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
# make sure renv is unloaded on exit, so locks etc. are released |
| 78 |
# we previously tried to orchestrate this via unloadNamespace(), |
| 79 |
# but this fails when a package importing renv is already loaded |
| 80 |
# https://github.com/rstudio/renv/issues/1621 |
| 81 |
reg.finalizer(renv_envir_self(), renv_unload_finalizer, onexit = TRUE) |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
.onAttach <- function(libname, pkgname) { |
| 86 |
renv_rstudio_fixup() |
| 87 |
} |
| 88 |
|
| 89 |
.onUnload <- function(libpath) { |
| 90 |
|
| 91 |
renv_lock_unload() |
| 92 |
renv_task_unload() |
| 93 |
renv_watchdog_unload() |
| 94 |
|
| 95 |
# do some extra cleanup when running R CMD check |
| 96 |
if (renv_platform_unix() && checking() && !ci()) |
| 97 |
cleanse() |
| 98 |
|
| 99 |
# flush the help db to avoid errors on reload |
| 100 |
# https://github.com/rstudio/renv/issues/1294 |
| 101 |
helpdb <- file.path(libpath, "help/renv.rdb") |
| 102 |
.Internal <- .Internal |
| 103 |
lazyLoadDBflush <- function(...) {} |
| 104 |
|
| 105 |
tryCatch( |
| 106 |
.Internal(lazyLoadDBflush(helpdb)), |
| 107 |
error = function(e) NULL |
| 108 |
) |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
# NOTE: required for devtools::load_all() |
| 113 |
.onDetach <- function(libpath) { |
| 114 |
if (devmode()) |
| 115 |
.onUnload(libpath) |
| 116 |
} |
| 117 |
|
| 118 |
renv_zzz_run <- function() { |
| 119 |
|
| 120 |
# check if we're in pkgload::load_all() |
| 121 |
# if so, then create some files |
| 122 |
if (devmode()) { |
| 123 |
renv_zzz_bootstrap_activate() |
| 124 |
renv_zzz_bootstrap_config() |
| 125 |
} |
| 126 |
|
| 127 |
# check if we're running as part of R CMD build |
| 128 |
# if so, build our local repository with a copy of ourselves |
| 129 |
if (building()) |
| 130 |
renv_zzz_repos() |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
renv_zzz_bootstrap_activate <- function() { |
| 135 |
|
| 136 |
source <- "templates/template-activate.R" |
| 137 |
target <- "inst/resources/activate.R" |
| 138 |
scripts <- c("R/ansify.R", "R/bootstrap.R", "R/json-read.R") |
| 139 |
|
| 140 |
# Do we need an update |
| 141 |
source_mtime <- max(renv_file_info(c(source, scripts))$mtime) |
| 142 |
target_mtime <- renv_file_info(target)$mtime |
| 143 |
|
| 144 |
if (!is.na(target_mtime) && target_mtime > source_mtime) |
| 145 |
return() |
| 146 |
|
| 147 |
# read the necessary bootstrap scripts |
| 148 |
contents <- map(scripts, readLines) |
| 149 |
bootstrap <- unlist(contents) |
| 150 |
|
| 151 |
# format nicely for insertion |
| 152 |
bootstrap <- paste(" ", bootstrap) |
| 153 |
bootstrap <- paste(bootstrap, collapse = "\n") |
| 154 |
|
| 155 |
# replace template with bootstrap code |
| 156 |
template <- renv_file_read(source) |
| 157 |
replaced <- renv_template_replace(template, list(BOOTSTRAP = bootstrap)) |
| 158 |
|
| 159 |
# write to resources |
| 160 |
printf("- Generating 'inst/resources/activate.R' ... ") |
| 161 |
writeLines(replaced, con = target) |
| 162 |
writef("Done!") |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
renv_zzz_bootstrap_config <- function() { |
| 167 |
|
| 168 |
source <- "inst/config.yml" |
| 169 |
target <- "R/config-defaults.R" |
| 170 |
|
| 171 |
source_mtime <- renv_file_info(source)$mtime |
| 172 |
target_mtime <- renv_file_info(target)$mtime |
| 173 |
|
| 174 |
if (target_mtime > source_mtime) |
| 175 |
return() |
| 176 |
|
| 177 |
template <- renv_template_create(heredoc(leave = 2, ' |
| 178 |
${NAME} = function(..., default = ${DEFAULT}) { |
| 179 |
renv_config_get( |
| 180 |
name = "${NAME}", |
| 181 |
type = "${TYPE}", |
| 182 |
default = default, |
| 183 |
args = list(...) |
| 184 |
) |
| 185 |
} |
| 186 |
')) |
| 187 |
|
| 188 |
template <- gsub("^\\n+|\\n+$", "", template) |
| 189 |
|
| 190 |
generate <- function(entry) { |
| 191 |
|
| 192 |
name <- entry$name |
| 193 |
type <- entry$type |
| 194 |
default <- entry$default |
| 195 |
code <- entry$code |
| 196 |
|
| 197 |
default <- if (length(code)) trimws(code) else deparse(default) |
| 198 |
|
| 199 |
replacements <- list( |
| 200 |
NAME = name, |
| 201 |
TYPE = type, |
| 202 |
DEFAULT = default |
| 203 |
) |
| 204 |
|
| 205 |
renv_template_replace(template, replacements) |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
config <- yaml::read_yaml("inst/config.yml") |
| 210 |
code <- map_chr(config, generate) |
| 211 |
all <- c( |
| 212 |
"", |
| 213 |
"# Auto-generated by renv_zzz_bootstrap_config()", |
| 214 |
"", |
| 215 |
"#' @rdname config", |
| 216 |
"#' @export", |
| 217 |
"#' @format NULL", |
| 218 |
"config <- list(", |
| 219 |
"", |
| 220 |
paste(code, collapse = ",\n\n"), |
| 221 |
"", |
| 222 |
")" |
| 223 |
) |
| 224 |
|
| 225 |
printf("- Generating 'R/config-defaults.R' ... ") |
| 226 |
writeLines(all, con = target) |
| 227 |
writef("Done!") |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
renv_zzz_repos <- function() { |
| 232 |
|
| 233 |
# don't run if we're running tests |
| 234 |
if (checking()) |
| 235 |
return() |
| 236 |
|
| 237 |
# prevent recursion |
| 238 |
installing <- Sys.getenv("RENV_INSTALLING_REPOS", unset = NA) |
| 239 |
if (!is.na(installing)) |
| 240 |
return() |
| 241 |
|
| 242 |
renv_scope_envvars(RENV_INSTALLING_REPOS = "TRUE") |
| 243 |
writeLines("** installing renv to package-local repository") |
| 244 |
|
| 245 |
# get package directory |
| 246 |
pkgdir <- getwd() |
| 247 |
|
| 248 |
# move to build directory |
| 249 |
tdir <- tempfile("renv-build-") |
| 250 |
ensure_directory(tdir) |
| 251 |
renv_scope_wd(tdir) |
| 252 |
|
| 253 |
# build renv again |
| 254 |
r_cmd_build("renv", path = pkgdir, "--no-build-vignettes") |
| 255 |
|
| 256 |
# copy built tarball to inst folder |
| 257 |
src <- list.files(tdir, full.names = TRUE) |
| 258 |
tgt <- file.path(pkgdir, "inst/repos/src/contrib") |
| 259 |
|
| 260 |
ensure_directory(tgt) |
| 261 |
file.copy(src, tgt) |
| 262 |
|
| 263 |
# write PACKAGES |
| 264 |
renv_scope_envvars(R_DEFAULT_SERIALIZE_VERSION = "2") |
| 265 |
write_PACKAGES(tgt, type = "source") |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
if (identical(.packageName, "renv")) { |
| 270 |
renv_zzz_run() |
| 271 |
} |