Debian Codegraph @

R/load.R

raw
1
2 # are we currently running 'load()'?
3 the$load_running <- FALSE
4
5 #' Load a project
6 #'
7 #' @description
8 #' `renv::load()` sets the library paths to use a project-local library,
9 #' sets up the system library [sandbox], if needed, and creates shims
10 #' for `install.packages()`, `update.packages()`, and `remove.packages()`.
11 #'
12 #' You should not generally need to call `renv::load()` yourself, as it's
13 #' called automatically by the project auto-loader created by [renv::init()]/
14 #' [renv::activate()]. However, if needed, you can use `renv::load("<project>")`
15 #' to explicitly load an renv project located at a particular path.
16 #'
17 #' # Shims
18 #'
19 #' To help you take advantage of the package cache, renv places a couple of
20 #' shims on the search path:
21 #'
22 #' * `install.packages()` instead calls `renv::install()`.
23 #' * `remove.packages()` instead calls `renv::remove()`.
24 #' * `update.packages()` instead calls `renv::update()`.
25 #'
26 #' This allows you to keep using your existing muscle memory for installing,
27 #' updating, and remove packages, while taking advantage of renv features
28 #' like the package cache.
29 #'
30 #' If you'd like to bypass these shims within an \R session, you can explicitly
31 #' call the version of these functions from the utils package, e.g. with
32 #' `utils::install.packages(<...>)`.
33 #'
34 #' If you'd prefer not to use the renv shims at all, they can be disabled by
35 #' setting the R option `options(renv.config.shims.enabled = FALSE)` or by
36 #' setting the environment variable `RENV_CONFIG_SHIMS_ENABLED = FALSE`. See
37 #' `?config` for more details.
38 #'
39 #' @inherit renv-params
40 #'
41 #' @param quiet Boolean; be quiet during load?
42 #'
43 #' @export
44 #'
45 #' @examples
46 #' \dontrun{
47 #'
48 #' # load a project -- note that this is normally done automatically
49 #' # by the project's auto-loader, but calling this explicitly to
50 #' # load a particular project may be useful in some circumstances
51 #' renv::load()
52 #'
53 #' }
54 load <- function(project = NULL, quiet = FALSE, profile = NULL, ...) {
55
56 # forward to base::load() if it looks like that was the intention
57 base <- renv_load_base(sys.call(), envir = parent.frame())
58 if (!is.null(base))
59 return(invisible(base))
60
61 # eagerly load package namespaces which we rely on
62 requireNamespace("compiler", quietly = TRUE)
63
64 renv_scope_error_handler()
65
66 project <- renv_path_normalize(
67 project %||% renv_project_find(project),
68 mustWork = TRUE
69 )
70
71 if (!is.null(profile))
72 renv_profile_set(profile)
73
74 action <- renv_load_action(project)
75 if (action[[1L]] == "cancel") {
76 cancel(verbose = !autoloading())
77 } else if (action[[1L]] == "init") {
78 return(init(project))
79 } else if (action[[1L]] == "alt") {
80 project <- action[[2L]]
81 } else if (action[[1L]] == "none") {
82 return(invisible(project))
83 }
84
85 renv_project_lock(project = project)
86
87 # indicate that we're now loading the project
88 renv_scope_binding(the, "load_running", TRUE)
89
90 # if load is being called via the autoloader,
91 # then ensure RENV_PROJECT is unset
92 # https://github.com/rstudio/renv/issues/887
93 if (autoloading())
94 renv_project_clear()
95
96 # if we're loading a project different from the one currently loaded,
97 # then unload the current project and reload the requested one
98 switch <-
99 !renv_metadata_embedded() &&
100 !is.null(the$project_path) &&
101 !identical(project, the$project_path)
102
103 if (switch)
104 return(renv_load_switch(project))
105
106 if (quiet || renv_load_quiet())
107 renv_scope_options(renv.verbose = FALSE)
108
109 renv_envvars_save()
110
111 # load a minimal amount of state when testing
112 if (renv_tests_running())
113 return(renv_load_minimal(project))
114
115 # load rest of renv components
116 renv_load_init(project)
117 renv_load_path(project)
118 renv_load_shims(project)
119 renv_load_renviron(project)
120 renv_load_profile(project)
121 renv_load_settings(project)
122 renv_load_project(project)
123 renv_load_sandbox(project)
124 renv_load_libpaths(project)
125 renv_load_rprofile(project)
126 renv_load_cache(project)
127
128 # load components encoded in lockfile
129 lockfile <- renv_lockfile_load(project)
130 if (length(lockfile)) {
131 renv_load_r(project, lockfile$R)
132 renv_load_python(project, lockfile$Python)
133 renv_load_bioconductor(project, lockfile$Bioconductor)
134 }
135
136 # allow failure to write infrastructure here to be non-fatal
137 # https://github.com/rstudio/renv/issues/574#issuecomment-731159197
138 catch({
139 renv_infrastructure_write_rbuildignore(project)
140 renv_infrastructure_write_gitignore(project)
141 })
142
143 renv_load_finish(project, lockfile)
144
145 invisible(project)
146 }
147
148 renv_load_action <- function(project) {
149
150 # don't do anything in non-interactive sessions
151 if (!interactive())
152 return("load")
153
154 # if this project already contains an 'renv' folder, assume it's
155 # already been initialized and we can directly load it
156 renv <- renv_paths_renv(project = project, profile = FALSE)
157 if (dir.exists(renv))
158 return("load")
159
160 # check and see if we're being called within a sub-directory
161 path <- renv_file_find(dirname(project), function(parent) {
162 if (file.exists(file.path(parent, "renv")))
163 return(parent)
164 })
165
166 # the project has not yet been initialized; notify the user and ask
167 # what they would like to do
168 fmt <- "The project located at %s has not yet been initialized."
169 header <- sprintf(fmt, renv_path_pretty(project))
170
171 # if we're running the autoloader in RStudio, we cannot ask
172 # the user for input at this stage -- instead, just notify them
173 # of the choices available
174 if (autoloading() && renv_rstudio_available()) {
175 initmsg <- "- Use `renv::init()` to initialize this project."
176 loadmsg <- "- Use `renv::load()` to load this project as-is."
177 caution(c(header, initmsg, loadmsg))
178 return("none")
179 }
180
181 # otherwise, prompt the user and provide them choices to proceed
182 title <- paste("", header, "", "What would you like to do?", sep = "\n")
183 choices <- c(
184 init = "Initialize this project with `renv::init()`.",
185 load = "Continue loading this project as-is.",
186 cancel = "Cancel loading this project."
187 )
188
189 if (!is.null(path)) {
190 fmt <- "Load the project located at %s instead."
191 msg <- sprintf(fmt, renv_path_pretty(path))
192 choices <- c(choices, alt = msg)
193 }
194
195
196 selection <- tryCatch(
197 utils::select.list(choices, title = title, graphics = FALSE),
198 interrupt = identity
199 )
200
201 if (inherits(selection, "interrupt")) {
202 writef()
203 selection <- choices["cancel"]
204 }
205
206 list(names(selection), path)
207
208 }
209
210 renv_load_minimal <- function(project) {
211
212 renv_load_libpaths(project)
213
214 lockfile <- renv_lockfile_load(project)
215 if (length(lockfile)) {
216 renv_load_r(project, lockfile$R)
217 renv_load_python(project, lockfile$Python)
218 }
219
220 renv_load_finish(project, lockfile)
221 invisible(project)
222
223 }
224
225 renv_load_r <- function(project, fields) {
226
227 # check for missing fields
228 if (is.null(fields)) {
229 warning("missing required [R] section in lockfile")
230 return(NULL)
231 }
232
233 # load repositories
234 renv_load_r_repos(fields$Repositories)
235
236 # load (check) version
237 version <- fields$Version
238 if (is.null(version)) {
239 warning("no R version recorded in this lockfile")
240 return(NULL)
241 }
242
243 # normalize versions as strings
244 requested <- renv_version_maj_min(version)
245 current <- renv_version_maj_min(getRversion())
246
247 # only compare major, minor versions
248 if (!identical(requested, current)) {
249 fmt <- "Using R %s (lockfile was generated with R %s)"
250 msg <- fyi(fmt, getRversion(), version)
251 writef(msg)
252 }
253
254 }
255
256 renv_load_r_repos <- function(repos) {
257
258 # force a character vector (https://github.com/rstudio/renv/issues/127)
259 repos <- convert(repos, "character")
260
261 # remove trailing slashes
262 nms <- names(repos)
263 repos <- sub("/+$", "", repos)
264 names(repos) <- nms
265
266 # transform PPM URLs if enabled
267 # this ensures that install.packages() uses binaries by default on Linux,
268 # where 'getOption("pkgType")' is "source" by default
269 if (renv_ppm_enabled())
270 repos <- renv_ppm_transform(repos)
271
272 # normalize option
273 repos <- renv_repos_normalize(repos)
274
275 # set sanitized repos
276 options(repos = repos)
277
278 # and return
279 repos
280
281 }
282
283 renv_load_init <- function(project) {
284
285 # warn if the project path cannot be translated into the native encoding,
286 # as (especially on Windows) this will likely prevent renv from working
287 actual <- enc2utf8(project)
288 expected <- catch(enc2utf8(enc2native(actual)))
289 if (identical(actual, expected))
290 return(TRUE)
291
292 msg <- paste(
293 "the project path cannot be represented in the native encoding;",
294 "renv may not function as expected"
295 )
296
297 warning(msg)
298
299 }
300
301 renv_load_path <- function(project) {
302
303 # only required when running in RStudio
304 if (!renv_rstudio_available())
305 return(FALSE)
306
307 # on macOS, read paths from /etc/paths and friends
308
309 # nocov start
310 if (renv_platform_macos()) {
311
312 # read the current PATH
313 old <- Sys.getenv("PATH", unset = "") %>%
314 strsplit(split = .Platform$path.sep, fixed = TRUE) %>%
315 unlist()
316
317 # get the new PATH entries
318 files <- c(
319 if (file.exists("/etc/paths")) "/etc/paths",
320 list.files("/etc/paths.d", full.names = TRUE)
321 )
322
323 new <- uapply(files, readLines, warn = FALSE)
324
325 # mix them together, preferring things in /etc/paths
326 mix <- unique(c(new, old))
327
328 # update the PATH
329 Sys.setenv(PATH = paste(mix, collapse = .Platform$path.sep))
330
331 }
332 # nocov end
333
334 }
335
336 renv_load_shims <- function(project) {
337 if (renv_shims_enabled())
338 renv_shims_activate()
339 }
340
341 renv_load_renviron <- function(project) {
342
343 environs <- c(
344 renv_paths_root(".Renviron"),
345 if (config$user.environ())
346 Sys.getenv("R_ENVIRON_USER", unset = "~/.Renviron"),
347 file.path(project, ".Renviron")
348 )
349
350 for (environ in environs)
351 if (file.exists(environ))
352 readRenviron(environ)
353
354 renv_envvars_normalize()
355
356 }
357
358 renv_load_profile <- function(project) {
359
360 renv_bootstrap_profile_load(project = project)
361
362 }
363
364 renv_load_settings <- function(project) {
365
366 # migrate settings.dcf => settings.json
367 renv_settings_migrate(project = project)
368
369 # load settings.R
370 settings <- renv_paths_renv("settings.R", project = project)
371 if (!file.exists(settings))
372 return(FALSE)
373
374 tryCatch(
375 eval(parse(settings), envir = baseenv()),
376 error = warnify
377 )
378
379 TRUE
380
381 }
382
383 renv_load_project <- function(project) {
384
385 # update project list if enabled
386 if (renv_cache_config_enabled(project = project)) {
387 project <- renv_path_normalize(project)
388 renv_load_project_projlist(project)
389 }
390
391 TRUE
392
393 }
394
395 renv_load_project_projlist <- function(project) {
396
397 # read project list
398 projects <- renv_paths_root("projects")
399 projlist <- character()
400 if (file.exists(projects))
401 projlist <- readLines(projects, warn = FALSE, encoding = "UTF-8")
402
403 # if the project is already recorded, nothing to do
404 if (project %in% projlist)
405 return(TRUE)
406
407 # sort with C locale (ensure consistent sorting across OSes)
408 projlist <- csort(c(projlist, project))
409
410 # update the project list
411 ensure_parent_directory(projects)
412 catchall(writeLines(enc2utf8(projlist), con = projects, useBytes = TRUE))
413
414 TRUE
415
416 }
417
418 renv_load_rprofile <- function(project = NULL) {
419
420 project <- renv_project_resolve(project)
421
422 # bail if not enabled by user
423 enabled <- identical(config$user.profile(), TRUE)
424 if (!enabled)
425 return(FALSE)
426
427 # callr will manage sourcing of user profile, so don't try
428 # to source the user profile if we're in callr
429 callr <- Sys.getenv("CALLR_CHILD_R_LIBS", unset = NA)
430 if (!is.na(callr))
431 return(FALSE)
432
433 # check for existence of profile
434 profile <- Sys.getenv("R_PROFILE_USER", unset = "~/.Rprofile")
435 if (!file.exists(profile))
436 return(FALSE)
437
438 renv_scope_libpaths()
439 renv_load_rprofile_impl(profile)
440
441 TRUE
442
443 }
444
445 renv_load_rprofile_impl <- function(profile) {
446
447 # NOTE: We'd like to use a regular tryCatch() handler here, but
448 # that will cause issues for user profiles which attempt to add
449 # global calling handlers. For that reason, we just register a
450 # bare restart handler, so at least we can catch the jump.
451 #
452 # https://github.com/rstudio/renv/issues/1036
453 status <- withRestarts(
454 sys.source(profile, envir = globalenv()),
455 abort = function() { structure(list(), class = "_renv_error") }
456 )
457
458 if (inherits(status, "_renv_error")) {
459 fmt <- "an error occurred while sourcing %s"
460 warningf(fmt, renv_path_pretty(profile))
461 }
462
463 FALSE
464
465 }
466
467 renv_load_libpaths <- function(project = NULL) {
468 libpaths <- renv_init_libpaths(project)
469 lapply(libpaths, renv_library_diagnose, project = project)
470 Sys.setenv(R_LIBS_USER = paste(libpaths, collapse = .Platform$path.sep))
471 renv_libpaths_set(libpaths)
472 }
473
474 renv_load_sandbox <- function(project) {
475 renv_sandbox_activate(project)
476 }
477
478 renv_load_python <- function(project, fields) {
479
480 python <- tryCatch(
481 renv_load_python_impl(project, fields),
482 error = function(e) {
483 warning(e)
484 NULL
485 }
486 )
487
488 if (is.null(python))
489 return(FALSE)
490
491 # set environment variables
492 # - RENV_PYTHON is the version of Python renv was configured to use
493 # - RETICULATE_PYTHON used to configure version of Python used by reticulate
494 Sys.setenv(
495 RENV_PYTHON = python,
496 RETICULATE_PYTHON = python
497 )
498
499 # place python + relevant utilities on the PATH
500 bindir <- dirname(python)
501 if (bindir %in% c("/usr/bin", "/usr/local/bin", "/opt/local/bin")) {
502
503 # create a temporary directory to host symlinks
504 toolspath <- tempfile("python-tools")
505 ensure_directory(toolspath)
506
507 # symlink common python binaries into that directory
508 for (binary in c("python", "python3", "pip", "pip3")) {
509 src <- file.path(bindir, binary)
510 if (file.exists(src)) {
511 tgt <- file.path(toolspath, binary)
512 renv_file_link(src, tgt)
513 }
514 }
515
516 # put it on the PATH
517 renv_envvar_path_add("PATH", normalizePath(toolspath))
518
519 } else {
520 bindir <- normalizePath(bindir, mustWork = FALSE)
521 renv_envvar_path_add("PATH", bindir)
522 }
523
524
525 # on Windows, for conda environments, we may also have a Scripts directory
526 # which will need to be pre-pended to the PATH
527 if (renv_platform_windows()) {
528 scriptsdir <- file.path(bindir, "Scripts")
529 if (file.exists(scriptsdir))
530 renv_envvar_path_add("PATH", scriptsdir)
531 }
532
533 # for conda environments, we should try to find conda and place the conda
534 # executable on the PATH, in case users want to use conda e.g. from
535 # the terminal or even via R system calls
536 #
537 # we'll also need to set some environment variables to ensure that conda
538 # uses this environment by default
539 info <- renv_python_info(python)
540 if (identical(info$type, "conda")) {
541 conda <- renv_conda_find(python)
542 if (file.exists(conda)) {
543 renv_envvar_path_add("PATH", dirname(conda))
544 Sys.setenv(CONDA_PREFIX = info$root)
545 }
546 }
547
548 TRUE
549
550 }
551
552 renv_load_python_impl <- function(project, fields) {
553
554 # if RENV_PYTHON is already set, just use it
555 python <- Sys.getenv("RENV_PYTHON", unset = NA)
556 if (!is.na(python))
557 return(python)
558
559 # set a default reticulate Python environment path
560 envpath <- renv_paths_renv("python/r-reticulate", project = project)
561 Sys.setenv(RETICULATE_MINICONDA_PYTHON_ENVPATH = envpath)
562
563 # nothing more to do if no lockfile fields set
564 if (is.null(fields))
565 return(NULL)
566
567 # delegate based on type appropriately
568 type <- fields$Type
569 if (is.null(type))
570 return(NULL)
571
572 python <- switch(type,
573 system = renv_load_python_default(project, fields),
574 virtualenv = renv_load_python_virtualenv(project, fields),
575 conda = renv_load_python_condaenv(project, fields),
576 stopf("unrecognized Python type '%s'", type)
577 )
578
579 renv_path_canonicalize(python)
580
581 }
582
583 renv_load_python_default <- function(project, fields) {
584
585 # if 'Name' points to a valid copy of Python, use it
586 name <- fields$Name
587 if (!is.null(name) && file.exists(name))
588 return(name)
589
590 # otherwise, try to find a compatible version of Python
591 renv_python_find(fields$Version)
592
593 }
594
595 renv_load_python_virtualenv <- function(project, fields) {
596
597 renv_use_python_virtualenv_impl(
598 project = project,
599 name = fields[["Name"]] %NA% NULL,
600 version = fields[["Version"]] %NA% NULL,
601 python = fields[["Python"]] %NA% NULL
602 )
603
604 }
605
606 renv_load_python_condaenv <- function(project, fields) {
607
608 renv_use_python_condaenv_impl(
609 project = project,
610 name = fields[["Name"]] %NA% NULL,
611 version = fields[["Version"]] %NA% NULL,
612 python = fields[["Python"]] %NA% NULL
613 )
614
615 }
616
617 renv_load_bioconductor <- function(project, bioconductor) {
618
619 # we don't try to support older R anymore
620 if (getRversion() < "3.4")
621 return()
622
623 # if we don't have a valid Bioconductor version, bail
624 version <- getOption("renv.bioconductor.version") %||% bioconductor[["Version"]]
625 if (is.null(version))
626 return()
627
628 # initialize bioconductor
629 renv_bioconductor_init()
630
631 # validate version if necessary
632 # avoid doing this in non-interactive sessions, as it can rely on
633 # a web request to https://bioconductor.org/config.yaml, which can be slow
634 validate <- getOption("renv.bioconductor.validate", default = interactive())
635 if (truthy(validate, default = FALSE))
636 renv_load_bioconductor_validate(project, version)
637
638 # update the R repositories
639 repos <- renv_bioconductor_repos(project, version)
640 options(repos = repos)
641
642 # notify the user
643 sprintf("- Using Bioconductor '%s'.", version)
644
645 }
646
647 renv_load_bioconductor_validate <- function(project, version) {
648 renv_bioconductor_validate(version, prompt = FALSE)
649 }
650
651 renv_load_switch <- function(project) {
652
653 # skip when testing
654 if (testing())
655 return(project)
656
657 # safety check: avoid recursive unload attempts
658 unloading <- getOption("renv.unload.project")
659 if (!is.null(unloading)) {
660 fmt <- "ignoring recursive attempt to load project '%s'"
661 warningf(fmt, renv_path_pretty(project))
662 return(project)
663 }
664
665 # unset the RENV_PATHS_RENV environment variable
666 # TODO: is there a path forward if different projects use
667 # different RENV_PATHS_RENV paths?
668 renvpath <- Sys.getenv("RENV_PATHS_RENV", unset = NA)
669 Sys.unsetenv("RENV_PATHS_RENV")
670
671 # validate that this project has an activate script
672 script <- renv_paths_activate(project = project)
673 if (!file.exists(script)) {
674 fmt <- "project %s has no activate script and so cannot be activated"
675 stopf(fmt, renv_path_pretty(project))
676 }
677
678 # signal that we're unloading now
679 # also ensure that the autoloader will be run when we source the active script
680 # https://github.com/rstudio/renv/issues/1959
681 renv_scope_options(
682 renv.unload.project = project,
683 renv.config.autoloader.enabled = TRUE
684 )
685
686 # perform the unload
687 unload()
688
689 # unload the current version of renv (but keep track of position
690 # on search path in case we need to revert later)
691 path <- renv_namespace_path("renv")
692 pos <- match("package:renv", search())
693 unloadNamespace("renv")
694
695 # move to new project directory
696 owd <- setwd(project)
697 on.exit(setwd(owd), add = TRUE)
698
699 # source the activate script
700 source(script)
701
702 # check and see if renv was successfully loaded
703 if (!"renv" %in% loadedNamespaces()) {
704 fmt <- "could not load renv from project %s; reloading previously-loaded renv"
705 warning(sprintf(fmt, project))
706 loadNamespace("renv", lib.loc = dirname(path))
707 Sys.setenv(RENV_PATHS_RENV = renvpath)
708 if (!is.na(pos)) {
709 args <- list(package = "renv", pos = pos, character.only = TRUE)
710 do.call(base::library, args)
711 }
712 }
713
714 }
715
716 renv_load_cache_renvignore <- function(project) {
717
718 if (testing() || checking())
719 return()
720
721 if (!renv_cache_config_enabled(project))
722 return()
723
724 caches <- renv_paths_cache()
725 ensure_directory(caches)
726 renv_renvignore_create(
727 paths = caches,
728 create = TRUE
729 )
730
731 }
732
733 renv_load_cache <- function(project) {
734
735 renv_load_cache_renvignore(project)
736 if (!interactive())
737 return(FALSE)
738
739 oldcache <- renv_paths_cache(version = renv_cache_version_previous())[[1L]]
740 newcache <- renv_paths_cache(version = renv_cache_version())[[1L]]
741 if (!file.exists(oldcache) || file.exists(newcache))
742 return(FALSE)
743
744 msg <- lines(
745 "- The cache version has been updated in this version of renv.",
746 "- Use `renv::rehash()` to migrate packages from the old renv cache."
747 )
748 printf(msg)
749
750 }
751
752 renv_load_check <- function(project) {
753 renv_load_check_description(project)
754 }
755
756 renv_load_check_description <- function(project) {
757
758 descpath <- file.path(project, "DESCRIPTION")
759 if (!file.exists(descpath))
760 return(TRUE)
761
762 # read description file, with whitespace trimmed
763 contents <- read(descpath) %>% trim() %>% chop()
764 bad <- which(grepl("^\\s*$", contents, perl = TRUE))
765 if (empty(bad))
766 return(TRUE)
767
768 values <- sprintf("[line %i is blank]", bad)
769
770 bulletin(
771 sprintf("%s contains blank lines:", renv_path_pretty(descpath)),
772 values,
773 c(
774 "DESCRIPTION files cannot contain blank lines between fields.",
775 "Please remove these blank lines from the file."
776 )
777 )
778
779 return(FALSE)
780
781 }
782
783 renv_load_quiet <- function() {
784 default <- identical(renv_verbose(), FALSE) || renv_session_quiet()
785 config$startup.quiet(default = default)
786 }
787
788 renv_load_finish <- function(project = NULL, lockfile = NULL) {
789
790 renv_project_set(project)
791
792 renv_load_check(project)
793 renv_load_report_project(project)
794 renv_load_report_python(project)
795
796 if (config$updates.check())
797 renv_load_report_updates(project)
798
799 if (config$synchronized.check())
800 renv_load_report_synchronized(project, lockfile)
801
802 renv_snapshot_auto_update(project = project)
803
804 }
805
806 renv_load_report_project <- function(project) {
807
808 profile <- renv_profile_get()
809 version <- renv_metadata_version_friendly(shafmt = "; sha: %s")
810
811 if (!is.null(profile)) {
812 fmt <- "- Project '%s' loaded. [renv %s; using profile '%s']"
813 writef(fmt, renv_path_aliased(project), version, profile)
814 } else {
815 fmt <- "- Project '%s' loaded. [renv %s]"
816 writef(fmt, renv_path_aliased(project), version)
817 }
818
819 }
820
821 renv_load_report_python <- function(project) {
822 # TODO
823 }
824
825 # nocov start
826 renv_load_report_updates <- function(project) {
827
828 lockpath <- renv_lockfile_path(project = project)
829 if (!file.exists(lockpath))
830 return(FALSE)
831
832 status <- update(project = project, check = TRUE)
833 available <- inherits(status, "renv_updates") && length(status$diff)
834 if (!available)
835 return(FALSE)
836
837 writef("- Use `renv::update()` to install updated packages.")
838 if (!interactive())
839 print(status)
840
841 TRUE
842
843 }
844 # nocov end
845
846
847 renv_load_report_synchronized <- function(project = NULL, lockfile = NULL) {
848
849 project <- renv_project_resolve(project)
850 lockfile <- lockfile %||% renv_lockfile_load(project)
851
852 # signal that we're running synchronization checks
853 renv_scope_binding(the, "project_synchronized_check_running", TRUE)
854
855 # be quiet when checking for dependencies in this scope
856 # https://github.com/rstudio/renv/issues/1181
857 renv_scope_options(renv.config.dependency.errors = "ignored")
858
859 # check for packages referenced in the lockfile which are not installed
860 lockpkgs <- names(lockfile$Packages)
861 libpkgs <- renv_snapshot_library(
862 library = renv_libpaths_all(),
863 project = project,
864 records = FALSE
865 )
866
867 # ignore renv
868 lockpkgs <- setdiff(lockpkgs, "renv")
869 libpkgs <- setdiff(libpkgs, "renv")
870
871 # check for case where no packages are installed (except renv)
872 if (length(intersect(lockpkgs, libpkgs)) == 0 && length(lockpkgs) > 0L) {
873
874 caution("- None of the packages recorded in the lockfile are currently installed.")
875 if (autoloading()) {
876 caution("- Use `renv::restore()` to restore the project library.")
877 return(FALSE)
878 }
879
880 response <- ask("Would you like to run `renv::restore()` to restore the project library?", default = FALSE)
881 if (!response)
882 return(FALSE)
883
884 restore(project, prompt = FALSE, exclude = "renv")
885 return(TRUE)
886
887 }
888
889 # check for case where one or more packages are missing
890 missing <- setdiff(lockpkgs, basename(libpkgs))
891 if (length(missing)) {
892 msg <- lines(
893 "- One or more packages recorded in the lockfile are not installed.",
894 "- Use `renv::status()` for more details."
895 )
896 caution(msg)
897 return(FALSE)
898 }
899
900 # otherwise, use status to detect if we're synchronized
901 info <- local({
902 renv_scope_options(renv.verbose = FALSE)
903 renv_scope_caution(FALSE)
904 status(project = project, sources = FALSE)
905 })
906
907 if (!identical(info$synchronized, TRUE)) {
908 caution("- The project is out-of-sync -- use `renv::status()` for details.")
909 return(FALSE)
910 }
911
912 TRUE
913
914 }
915
916 renv_load_base <- function(call, envir) {
917
918 # if we were called without arguments, assume we should handle it
919 if (length(call) == 0L)
920 return(NULL)
921
922 # if the call was namespace-qualified, assume we should handle it
923 if (renv_call_matches(call[[1L]], names = c("::", ":::")))
924 return(NULL)
925
926 # if any of the formals normally associated with base::load
927 # were provided, then delegate to base::load()
928 if (any(c("file", "envir") %in% names(call)))
929 return(renv_load_base_impl(call, envir))
930
931 # attempt to match the call
932 matched <- tryCatch(match.call(base::load, call), error = identity)
933 if (inherits(matched, "error"))
934 return(NULL)
935
936 # check for a 'file' argument that looks like a file
937 file <- eval(matched[["file"]], envir = envir)
938 if (is.character(file) && endsWith(file, ".RData"))
939 return(renv_load_base_impl(call, envir))
940
941 }
942
943 renv_load_base_impl <- function(call, envir) {
944 call[[1L]] <- quote(base::load)
945 eval(call, envir = envir)
946 }