Debian Codegraph @

R/status.R

raw
1
2 the$status_running <- FALSE
3
4 #' Report inconsistencies between lockfile, library, and dependencies
5 #'
6 #' @description
7 #' `renv::status()` reports issues caused by inconsistencies across the project
8 #' lockfile, library, and [dependencies()]. In general, you should strive to
9 #' ensure that `status()` reports no issues, as this maximizes your chances of
10 #' successfully `restore()`ing the project in the future or on another machine.
11 #'
12 #' `renv::load()` will report if any issues are detected when starting an
13 #' renv project; we recommend resolving these issues before doing any
14 #' further work on your project.
15 #'
16 #' See the headings below for specific advice on resolving any issues
17 #' revealed by `status()`.
18 #'
19 #' # Missing packages
20 #'
21 #' `status()` first checks that all packages used by the project are installed.
22 #' This must be done first because if any packages are missing we can't tell for
23 #' sure that a package isn't used; it might be a dependency that we don't know
24 #' about. Once you have resolve any installation issues, you'll need to run
25 #' `status()` again to reveal the next set of potential problems.
26 #'
27 #' There are four possibilities for an uninstalled package:
28 #'
29 #' * If it's used and recorded, call `renv::restore()` to install the version
30 #' specified in the lockfile.
31 #' * If it's used and not recorded, call `renv::install()` to install it
32 #' from CRAN or elsewhere.
33 #' * If it's not used and recorded, call `renv::snapshot()` to
34 #' remove it from the lockfile.
35 #' * If it's not used and not recorded, there's nothing to do. This the most
36 #' common state because you only use a small fraction of all available
37 #' packages in any one project.
38 #'
39 #' If you have multiple packages in an inconsistent state, we recommend
40 #' `renv::restore()`, then `renv::install()`, then `renv::snapshot()`, but
41 #' that also suggests you should be running status more frequently.
42 #'
43 #' # Lockfile vs `dependencies()`
44 #'
45 #' Next we need to ensure that packages are recorded in the lockfile if and
46 #' only if they are used by the project. Fixing issues of this nature only
47 #' requires calling `snapshot()` because there are four possibilities for
48 #' a package:
49 #'
50 #' * If it's used and recorded, it's ok.
51 #' * If it's used and not recorded, call `renv::snapshot()` to add it to the
52 #' lockfile.
53 #' * If it's not used but is recorded, call `renv::snapshot()` to remove
54 #' it from the lockfile.
55 #' * If it's not used and not recorded, it's also ok, as it may be a
56 #' development dependency.
57 #'
58 #' # Out-of-sync sources
59 #'
60 #' The final issue to resolve is any inconsistencies between the version of
61 #' the package recorded in the lockfile and the version installed in your
62 #' library. To fix these issues you'll need to either call `renv::restore()`
63 #' or `renv::snapshot()`:
64 #'
65 #' * Call `renv::snapshot()` if your project code is working. This implies that
66 #' the library is correct and you need to update your lockfile.
67 #' * Call `renv::restore()` if your project code isn't working. This probably
68 #' implies that you have the wrong package versions installed and you need
69 #' to restore from known good state in the lockfile.
70 #'
71 #' If you're not sure which case applies, it's generally safer to call
72 #' `renv::snapshot()`. If you want to rollback to an earlier known good
73 #' status, see [renv::history()] and [renv::revert()].
74 #'
75 #' # Different R Version
76 #'
77 #' renv will also notify you if the version of R used when the lockfile was
78 #' generated, and the version of R currently in use, do not match. In this
79 #' scenario, you'll need to consider:
80 #'
81 #' - Is the version of R recorded in the lockfile correct? If so, you'll want
82 #' to ensure that version of R is installed and used when working in this
83 #' project.
84 #'
85 #' - Otherwise, you can call `renv::snapshot()` to update the version of R
86 #' recorded in the lockfile, to match the version of R currently in use.
87 #'
88 #' If you'd like to set the version of R recorded in a lockfile independently
89 #' of the version of R currently in use, you can set the `r.version` project
90 #' setting -- see [settings] for more details.
91 #'
92 #' @inherit renv-params
93 #'
94 #' @param library The library paths. By default, the library paths associated
95 #' with the requested project are used.
96 #'
97 #' @param sources Boolean; check that each of the recorded packages have a
98 #' known installation source? If a package has an unknown source, renv
99 #' may be unable to restore it.
100 #'
101 #' @param cache Boolean; perform diagnostics on the global package cache?
102 #' When `TRUE`, renv will validate that the packages installed into the
103 #' cache are installed at the expected + proper locations, and validate the
104 #' hashes used for those storage locations.
105 #'
106 #' @inheritParams dependencies
107 #'
108 #' @return This function is normally called for its side effects, but
109 #' it invisibly returns a list containing the following components:
110 #'
111 #' * `library`: packages in your library.
112 #' * `lockfile`: packages in the lockfile.
113 #' * `synchronized`: are the library and lockfile in sync?
114 #'
115 #' @export
116 #'
117 #' @example examples/examples-init.R
118 status <- function(project = NULL,
119 ...,
120 library = NULL,
121 lockfile = NULL,
122 sources = TRUE,
123 cache = FALSE,
124 dev = NULL)
125 {
126 renv_scope_error_handler()
127 renv_dots_check(...)
128
129 renv_snapshot_auto_suppress_next()
130 renv_scope_options(renv.prompt.enabled = FALSE)
131
132 the$status_running <- TRUE
133 defer(the$status_running <- FALSE)
134
135 project <- renv_project_resolve(project)
136 renv_project_lock(project = project)
137
138 # use setting as default if dev not explicitly provided
139 if (is.null(dev))
140 dev <- settings$snapshot.dev(project = project)
141
142 # check to see if we've initialized this project
143 if (!renv_status_check_initialized(project, library, lockfile)) {
144 result <- list(
145 library = list(Packages = named(list())),
146 lockfile = list(Packages = named(list())),
147 synchronized = FALSE
148 )
149 return(invisible(result))
150 }
151
152 libpaths <- library %||% renv_libpaths_resolve()
153 lockpath <- lockfile %||% renv_paths_lockfile(project = project)
154
155 # get all dependencies, including transitive
156 dependencies <- renv_snapshot_dependencies(project, dev = dev)
157 packages <- sort(union(dependencies, "renv"))
158 paths <- renv_package_dependencies(packages, libpaths = libpaths, project = project)
159 packages <- as.character(names(paths))
160
161 # read project lockfile
162 lockfile <- if (file.exists(lockpath))
163 renv_lockfile_read(lockpath)
164 else
165 renv_lockfile_init(project = project)
166
167 # get lockfile capturing current library state
168 library <- renv_lockfile_create(
169 libpaths = libpaths,
170 type = "all",
171 prompt = FALSE,
172 project = project
173 )
174
175 # remove ignored packages
176 ignored <- c(
177 renv_project_ignored_packages(project),
178 renv_packages_base(),
179 if (renv_tests_running()) "renv"
180 )
181
182 packages <- setdiff(packages, ignored)
183 renv_lockfile_records(lockfile) <- omit(renv_lockfile_records(lockfile), ignored)
184 renv_lockfile_records(library) <- omit(renv_lockfile_records(library), ignored)
185
186 synchronized <- all(
187 renv_status_check_consistent(lockfile, library, packages),
188 renv_status_check_synchronized(lockfile, library),
189 renv_status_check_version(lockfile)
190 )
191
192 if (sources) {
193 synchronized <- synchronized &&
194 renv_status_check_unknown_sources(project, lockfile)
195 }
196
197 if (cache)
198 renv_status_check_cache(project)
199
200 if (synchronized)
201 writef("No issues found -- the project is in a consistent state.")
202 else
203 writef("See `?renv::status` for advice on resolving these issues.")
204
205 result <- list(
206 library = library,
207 lockfile = lockfile,
208 synchronized = synchronized
209 )
210
211 invisible(result)
212
213 }
214
215 renv_status_check_unknown_sources <- function(project, lockfile) {
216 renv_check_unknown_source(renv_lockfile_records(lockfile), project)
217 }
218
219 renv_status_check_consistent <- function(lockfile, library, used) {
220
221 lockfile <- renv_lockfile_records(lockfile)
222 library <- renv_lockfile_records(library)
223
224 packages <- sort(unique(c(names(library), names(lockfile), used)))
225
226 status <- data_frame(
227 package = packages,
228 installed = packages %in% names(library),
229 recorded = packages %in% names(lockfile),
230 used = packages %in% used
231 )
232
233 ok <- status$installed & (status$used == status$recorded)
234 if (all(ok))
235 return(TRUE)
236
237 if (!renv_verbose())
238 return(FALSE)
239
240 issues <- status[!ok, , drop = FALSE]
241 missing <- issues$used & !issues$installed
242 if (all(missing)) {
243
244 bulletin(
245 preamble = "The following package(s) are used in this project, but are not installed:",
246 values = issues$package[missing]
247 )
248
249 return(FALSE)
250
251 }
252
253 issues$installed <- ifelse(issues$installed, "y", "n")
254 issues$recorded <- ifelse(issues$recorded, "y", "n")
255 issues$used <- ifelse(issues$used, "y", if (any(missing)) "?" else "n")
256
257 preamble <- "The following package(s) are in an inconsistent state:"
258
259 writef(preamble)
260 writef()
261 print(issues, row.names = FALSE, right = FALSE)
262 writef()
263
264 FALSE
265
266 }
267
268 renv_status_check_enabled <- function(parent) {
269
270 # get the name of the calling function
271 ok <- is.call(parent) && length(parent) && is.symbol(parent[[1L]])
272 if (!ok)
273 return(TRUE)
274
275 invoker <- as.character(parent[[1L]])
276 parts <- strsplit(invoker, "_", fixed = TRUE)[[1L]]
277 if (length(parts) < 3L)
278 return(TRUE)
279
280 # check the relevant config option
281 scope <- parts[[2L]]
282 name <- paste(tail(parts, n = -2L), collapse = "_")
283 value <- renv_config_get(
284 name = name,
285 scope = scope,
286 default = TRUE
287 )
288
289 truthy(value, default = TRUE)
290
291 }
292
293 renv_status_check_initialized <- function(project, library = NULL, lockfile = NULL) {
294
295 if (!renv_status_check_enabled(sys.call()))
296 return(TRUE)
297
298 # only done if library and lockfile are NULL; that is, if the user
299 # is calling `renv::status()` without arguments
300 if (!is.null(library) || !is.null(lockfile))
301 return(TRUE)
302
303 # resolve paths to lockfile, primary library path
304 library <- library %||% renv_paths_library(project = project)
305 lockfile <- lockfile %||% renv_paths_lockfile(project = project)
306
307 # check whether the lockfile + library exist
308 haslib <- all(file.exists(library))
309 haslock <- file.exists(lockfile)
310 if (haslib && haslock)
311 return(TRUE)
312
313 # TODO: what about the case where the library exists but no packages are installed?
314 # TODO: should this check for an 'renv/activate.R' script?
315 # TODO: what if a different project is loaded?
316 if (haslib && !haslock) {
317 writef(c(
318 "This project does not contain a lockfile.",
319 "Use `renv::snapshot()` to create a lockfile."
320 ))
321 } else if (!haslib && haslock) {
322 writef(c(
323 "There are no packages installed in the project library.",
324 "Use `renv::restore()` to install the packages defined in lockfile."
325 ))
326 } else {
327 writef(c(
328 "This project does not appear to be using renv.",
329 "Use `renv::init()` to initialize the project."
330 ))
331 }
332
333 FALSE
334
335 }
336
337 renv_status_check_synchronized <- function(lockfile, library) {
338
339 if (!renv_status_check_enabled(sys.call()))
340 return(TRUE)
341
342 lockfile <- renv_lockfile_records(lockfile)
343 library <- renv_lockfile_records(library)
344
345 actions <- renv_lockfile_diff_packages(lockfile, library)
346 rest <- c("upgrade", "downgrade", "crossgrade")
347
348 if (all(!rest %in% actions))
349 return(TRUE)
350
351 pkgs <- names(actions[actions %in% rest])
352 formatter <- function(lhs, rhs)
353 renv_record_format_pair(lhs, rhs, separator = "!=")
354
355 renv_pretty_print_records_pair(
356 preamble = "The following package(s) are out of sync [lockfile != library]:",
357 old = lockfile[pkgs],
358 new = library[pkgs],
359 formatter = formatter
360 )
361
362 FALSE
363
364 }
365
366 renv_status_check_version <- function(lockfile) {
367
368 if (!renv_status_check_enabled(sys.call()))
369 return(TRUE)
370
371 version <- lockfile$R$Version
372 if (renv_version_eq(version, getRversion(), n = 2L))
373 return(TRUE)
374
375 fmt <- "The lockfile was generated with R %s, but you're using R %s."
376 writef(fmt, version, getRversion())
377 writef()
378
379 FALSE
380
381 }
382
383 renv_status_check_cache <- function(project) {
384
385 if (renv_cache_config_enabled(project = project))
386 renv_cache_diagnose()
387
388 }
389