Debian Codegraph @

R/settings.R

raw
1
2 the$settings <- new.env(parent = emptyenv())
3
4 renv_settings_default <- function(name) {
5 default <- the$settings[[name]]$default
6 renv_options_override("renv.settings", name, default)
7 }
8
9 renv_settings_defaults <- function() {
10
11 keys <- ls(envir = the$settings, all.names = TRUE)
12 vals <- lapply(keys, renv_settings_default)
13 names(vals) <- keys
14 vals[order(names(vals))]
15
16 }
17
18 renv_settings_validate <- function(name, value) {
19
20 # NULL implies restore default value
21 if (is.null(value))
22 return(renv_settings_default(name))
23
24 # run coercion method
25 value <- the$settings[[name]]$coerce(value)
26
27 # validate the user-provided value
28 validate <- the$settings[[name]]$validate
29 ok <- case(
30 is.character(validate) ~ value %in% validate,
31 is.function(validate) ~ validate(value),
32 TRUE
33 )
34
35 if (identical(ok, TRUE))
36 return(value)
37
38 # validation failed; warn the user and use default
39 fmt <- "%s is an invalid value for setting '%s'; using default %s instead"
40 default <- renv_settings_default(name)
41 warningf(fmt, deparsed(value), name, deparsed(default))
42 default
43
44 }
45
46 renv_settings_read <- function(path) {
47
48 filebacked(
49 context = "renv_settings_read",
50 path = path,
51 callback = renv_settings_read_impl
52 )
53
54 }
55
56 renv_settings_read_impl <- function(path) {
57
58 # check that file exists
59 if (!file.exists(path))
60 return(NULL)
61
62 # read settings
63 settings <- case(
64 endsWith(path, ".dcf") ~ renv_settings_read_impl_dcf(path),
65 endsWith(path, ".json") ~ renv_settings_read_impl_json(path),
66 ~ stopf("don't know how to read settings file %s", renv_path_pretty(path))
67 )
68
69 # keep only known settings
70 known <- ls(envir = the$settings, all.names = TRUE)
71 settings <- keep(settings, known)
72
73 # validate
74 settings <- enumerate(settings, renv_settings_validate)
75
76 # merge in defaults
77 defaults <- renv_settings_defaults()
78 missing <- renv_vector_diff(names(defaults), names(settings))
79 settings[missing] <- defaults[missing]
80
81 # and return
82 settings
83
84 }
85
86 renv_settings_read_impl_dcf <- function(path) {
87
88 # try to read it
89 dcf <- catch(renv_dcf_read(path))
90 if (inherits(dcf, "error")) {
91 warning(dcf)
92 return(NULL)
93 }
94
95 # decode encoded values
96 enumerate(dcf, function(name, value) {
97
98 case(
99 value == "NULL" ~ NULL,
100 value == "NA" ~ NA,
101 value == "NaN" ~ NaN,
102 value == "TRUE" ~ TRUE,
103 value == "FALSE" ~ FALSE,
104 ~ strsplit(value, "\\s*,\\s*")[[1]]
105 )
106
107 })
108
109 }
110
111 renv_settings_read_impl_json <- function(path) {
112
113 json <- catch(renv_json_read(path))
114 if (inherits(json, "error")) {
115 warning(json)
116 return(NULL)
117 }
118
119 json
120
121 }
122
123 renv_settings_get <- function(project, name = NULL, default = NULL) {
124
125 # when 'name' is NULL, return all settings
126 if (is.null(name)) {
127 names <- ls(envir = the$settings, all.names = TRUE)
128 settings <- lapply(names, renv_settings_get, project = project)
129 names(settings) <- names
130 return(settings[order(names(settings))])
131 }
132
133 # check for an override via option
134 override <- renv_options_override("renv.settings", name)
135 if (!is.null(override))
136 return(override)
137
138 # try to read settings file
139 path <- renv_settings_path(project)
140 settings <- renv_settings_read(path)
141 if (!is.null(settings))
142 return(settings[[name]])
143
144 # if a 'default' value was provided, use it
145 if (!missing(default))
146 return(default)
147
148 # no value recorded; use default
149 renv_settings_default(name)
150
151 }
152
153 renv_settings_set <- function(project, name, value, persist = TRUE) {
154
155 # read old settings
156 settings <- renv_settings_get(project)
157
158 # update setting value
159 old <- settings[[name]] %||% renv_settings_default(name)
160 new <- renv_settings_validate(name, value)
161 settings[[name]] <- new
162
163 # persist if requested
164 if (persist)
165 renv_settings_persist(project, settings)
166
167 # save session-cached value
168 path <- renv_settings_path(project)
169 value <- renv_filebacked_set("renv_settings_read", path, settings)
170
171 # invoke update callback if value changed
172 if (!identical(old, new))
173 renv_settings_updated(project, name, old, new)
174
175 # return value
176 invisible(value)
177
178 }
179
180 renv_settings_updated <- function(project, name, old, new) {
181 update <- the$settings[[name]]$update %||% function(...) {}
182 update(project, old, new)
183 }
184
185 renv_settings_persist <- function(project, settings) {
186
187 path <- renv_settings_path(project)
188 settings <- settings[order(names(settings))]
189
190 # figure out which settings are scalar
191 scalar <- map_lgl(names(settings), function(name) {
192 the$settings[[name]]$scalar
193 })
194
195 # use that to determine which objects should be boxed
196 config <- renv_json_config(box = names(settings)[!scalar])
197
198 # write json
199 ensure_parent_directory(path)
200 renv_json_write(
201 object = settings,
202 config = config,
203 file = path
204 )
205
206 }
207
208 renv_settings_merge <- function(settings, merge) {
209 settings[names(merge)] <- merge
210 settings
211 }
212
213 renv_settings_path <- function(project) {
214 renv_paths_settings(project = project)
215 }
216
217
218 # nocov start
219
220 renv_settings_updated_cache <- function(project, old, new) {
221
222 # if the cache is being disabled, then copy packages from their
223 # symlinks back into the library. note that we don't use symlinks
224 # on windows (we use hard links) so in that case there's nothing
225 # to be done
226 if (renv_platform_windows())
227 return(FALSE)
228
229 library <- renv_paths_library(project = project)
230 pkgpaths <- list.files(library, full.names = TRUE)
231 cachepaths <- map_chr(pkgpaths, renv_cache_path)
232 names(pkgpaths) <- cachepaths
233
234 if (empty(pkgpaths)) {
235 fmt <- "- The cache has been %s for this project."
236 writef(fmt, if (new) "enabled" else "disabled")
237 return(TRUE)
238 }
239
240 printf("- Synchronizing project library with the cache ... ")
241
242 if (new) {
243
244 # enabling the cache: for any package in the project library, replace
245 # that copy with a symlink into the cache, moving the associated package
246 # into the cache if appropriate
247
248 # ignore existing symlinks; only copy 'real' packages into the cache
249 pkgtypes <- renv_file_type(pkgpaths)
250 cachepaths <- cachepaths[pkgtypes != "symlink"]
251
252 # move packages from project library into cache
253 callback <- renv_progress_callback(renv_cache_move, length(cachepaths))
254 enumerate(cachepaths, callback, overwrite = FALSE)
255
256 } else {
257
258 # disabling the cache: for any package which is a symlink into the cache,
259 # replace that symlink with a copy of the cached package
260
261 # figure out which package directories are symlinks
262 pkgtypes <- renv_file_type(pkgpaths)
263 pkgpaths <- pkgpaths[pkgtypes == "symlink"]
264
265 # remove the existing symlinks
266 unlink(pkgpaths)
267
268 # overwrite these symlinks with packages from the cache
269 callback <- renv_progress_callback(renv_file_copy, length(pkgpaths))
270 enumerate(pkgpaths, callback, overwrite = TRUE)
271
272 }
273
274 writef("Done!")
275
276 fmt <- "- The cache has been %s for this project."
277 writef(fmt, if (new) "enabled" else "disabled")
278
279 }
280
281 renv_settings_updated_ignore <- function(project, old, new) {
282 renv_infrastructure_write_gitignore(project = project)
283 }
284
285 renv_settings_migrate <- function(project) {
286
287 old <- renv_paths_renv("settings.dcf", project = project)
288 if (!file.exists(old))
289 return()
290
291 new <- renv_paths_renv("settings.json", project = project)
292 if (file.exists(new))
293 return()
294
295 # update settings
296 settings <- renv_settings_read(old)
297 renv_settings_persist(project, settings)
298
299 }
300
301 renv_settings_impl <- function(name, default, scalar, validate, coerce, update) {
302
303 force(name)
304
305 the$settings[[name]] <- list(
306 default = default,
307 coerce = coerce,
308 scalar = scalar,
309 validate = validate,
310 update = update
311 )
312
313 function(value, project = NULL, persist = TRUE) {
314 project <- renv_project_resolve(project)
315 if (missing(value))
316 renv_settings_get(project, name)
317 else
318 renv_settings_set(project, name, value, persist)
319 }
320
321 }
322
323
324 # nocov end
325
326 #' Project settings
327 #'
328 #' @description
329 #' Define project-local settings that can be used to adjust the behavior of
330 #' renv with your particular project.
331 #'
332 #' * Get the current value of a setting with (e.g.) `settings$snapshot.type()`
333 #' * Set current value of a setting with (e.g.)
334 #' `settings$snapshot.type("explicit")`.
335 #'
336 #' Settings are automatically persisted across project sessions by writing to
337 #' `renv/settings.json`. You can also edit this file by hand, but you'll need
338 #' to restart the session for those changes to take effect.
339 #'
340 #' ## `bioconductor.version`
341 #'
342 #' The Bioconductor version to be used with this project. Use this if you'd
343 #' like to lock the version of Bioconductor used on a per-project basis.
344 #' When unset, renv will try to infer the appropriate Bioconductor release
345 #' using the BiocVersion package if installed; if not, renv uses
346 #' `BiocManager::version()` to infer the appropriate Bioconductor version.
347 #'
348 #' ## `external.libraries`
349 #'
350 #' A vector of library paths, to be used in addition to the project's own
351 #' private library. This can be useful if you have a package available for use
352 #' in some system library, but for some reason renv is not able to install
353 #' that package (e.g. sources or binaries for that package are not publicly
354 #' available, or you have been unable to orchestrate the pre-requisites for
355 #' installing some packages from source on your machine).
356 #'
357 #' ## `lockfile.sanitize`
358 #'
359 #' Should renv sanitize repository URLs when writing the lockfile? When
360 #' `TRUE`, embedded credentials are stripped from URLs (e.g.
361 #' `https://user:token@host/path` becomes `https://host/path`) to prevent
362 #' accidental credential leakage. Set this to `FALSE` if your repository
363 #' URLs contain credentials required for [restore] to function correctly.
364 #'
365 #' ## `ignored.packages`
366 #'
367 #' A vector of packages, which should be ignored when attempting to snapshot
368 #' the project's private library. Note that if a package has already been
369 #' added to the lockfile, that entry in the lockfile will not be ignored.
370 #'
371 #' ## `package.dependency.fields`
372 #'
373 #' When installing a package with `install()`, what `DESCRIPTION` fields should
374 #' be used to determine that package's dependencies? The default uses
375 #' `c("Imports", "Depends", "LinkingTo")`, but if you also want to install
376 #' `Suggests` dependencies for a package, you can set this to
377 #' `c("Imports", "Depends", "LinkingTo", "Suggests")`.
378 #'
379 #' ## `ppm.enabled`
380 #'
381 #' Enable [Posit Package Manager](https://packagemanager.posit.co/)
382 #' integration in this project? When `TRUE`, renv will attempt to transform
383 #' repository URLs used by PPM into binary URLs as appropriate for the
384 #' current Linux platform. Set this to `FALSE` if you'd like to continue using
385 #' source-only PPM URLs, or if you find that renv is improperly transforming
386 #' your repository URLs. You can still set and use PPM repositories with this
387 #' option disabled; it only controls whether renv tries to transform source
388 #' repository URLs into binary URLs on your behalf.
389 #'
390 #' ## `ppm.ignored.urls`
391 #'
392 #' When [Posit Package Manager](https://packagemanager.posit.co/) integration
393 #' is enabled, `renv` will attempt to transform source repository URLs into
394 #' binary repository URLs. This setting can be used if you'd like to avoid this
395 #' transformation with some subset of repository URLs.
396 #'
397 #' ## `r.version`
398 #'
399 #' The version of \R to encode within the lockfile. This can be set as a
400 #' project-specific option if you'd like to allow multiple users to use
401 #' the same renv project with different versions of \R. renv will
402 #' still warn the user if the major + minor version of \R used in a project
403 #' does not match what is encoded in the lockfile.
404 #'
405 #' ## `snapshot.type`
406 #'
407 #' The type of snapshot to perform by default. See [snapshot] for more
408 #' details.
409 #'
410 #' ## `snapshot.dev`
411 #'
412 #' Whether to include development dependencies by default when calling
413 #' `renv::snapshot()` or `renv::status()`. When `TRUE`, development
414 #' dependencies (e.g., packages listed in `Suggests` or development tools
415 #' like `devtools`) will be included. Defaults to `FALSE`.
416 #'
417 #' ## `use.cache`
418 #'
419 #' Enable the renv package cache with this project. When active, renv will
420 #' install packages into a global cache, and link packages from the cache into
421 #' your renv projects as appropriate. This can greatly save on disk space
422 #' and install time when for \R packages which are used across multiple
423 #' projects in the same environment.
424 #'
425 #' ## `vcs.manage.ignores`
426 #'
427 #' Should renv attempt to manage the version control system's ignore files
428 #' (e.g. `.gitignore`) within this project? Set this to `FALSE` if you'd
429 #' prefer to take control. Note that if this setting is enabled, you will
430 #' need to manually ensure internal data in the project's `renv/` folder
431 #' is explicitly ignored.
432 #'
433 #' ## `vcs.ignore.cellar`
434 #'
435 #' Set whether packages within a project-local package cellar are excluded
436 #' from version control. See `vignette("package-sources", package = "renv")`
437 #' for more information.
438 #'
439 #' ## `vcs.ignore.library`
440 #'
441 #' Set whether the renv project library is excluded from version control.
442 #'
443 #' ## `vcs.ignore.local`
444 #'
445 #' Set whether renv project-specific local sources are excluded from version
446 #' control.
447 #'
448 #' # Defaults
449 #'
450 #' You can change the default values of these settings for newly-created renv
451 #' projects by setting \R options for `renv.settings` or `renv.settings.<name>`.
452 #' For example:
453 #'
454 #' ```R
455 #' options(renv.settings = list(snapshot.type = "all"))
456 #' options(renv.settings.snapshot.type = "all")
457 #' ```
458 #'
459 #' If both of the `renv.settings` and `renv.settings.<name>` options are set
460 #' for a particular key, the option associated with `renv.settings.<name>` is
461 #' used instead. We recommend setting these in an appropriate startup profile,
462 #' e.g. `~/.Rprofile` or similar.
463 #'
464 #' @return
465 #' A named list of renv settings.
466 #'
467 #' @format NULL
468 #'
469 #' @export
470 #'
471 #' @examples
472 #'
473 #' \dontrun{
474 #'
475 #' # view currently-ignored packaged
476 #' renv::settings$ignored.packages()
477 #'
478 #' # ignore a set of packages
479 #' renv::settings$ignored.packages("devtools", persist = FALSE)
480 #'
481 #' }
482 settings <- list(
483
484 bioconductor.version = renv_settings_impl(
485 name = "bioconductor.version",
486 default = NULL,
487 scalar = TRUE,
488 validate = is.character,
489 coerce = as.character,
490 update = NULL
491 ),
492
493 lockfile.sanitize = renv_settings_impl(
494 name = "lockfile.sanitize",
495 default = TRUE,
496 scalar = TRUE,
497 validate = is.logical,
498 coerce = as.logical,
499 update = NULL
500 ),
501
502 ignored.packages = renv_settings_impl(
503 name = "ignored.packages",
504 default = character(),
505 scalar = FALSE,
506 validate = is.character,
507 coerce = as.character,
508 update = NULL
509 ),
510
511 external.libraries = renv_settings_impl(
512 name = "external.libraries",
513 default = character(),
514 scalar = FALSE,
515 validate = is.character,
516 coerce = as.character,
517 update = NULL
518 ),
519
520 package.dependency.fields = renv_settings_impl(
521 name = "package.dependency.fields",
522 default = c("Imports", "Depends", "LinkingTo"),
523 scalar = FALSE,
524 validate = is.character,
525 coerce = as.character,
526 update = NULL
527 ),
528
529 ppm.enabled = renv_settings_impl(
530 name = "ppm.enabled",
531 default = NULL,
532 scalar = TRUE,
533 validate = is.logical,
534 coerce = as.logical,
535 update = NULL
536 ),
537
538 ppm.ignored.urls = renv_settings_impl(
539 name = "ppm.ignored.urls",
540 default = NULL,
541 scalar = FALSE,
542 validate = is.character,
543 coerce = as.character,
544 update = NULL
545 ),
546
547 r.version = renv_settings_impl(
548 name = "r.version",
549 default = NULL,
550 scalar = TRUE,
551 validate = is.character,
552 coerce = as.character,
553 update = NULL
554 ),
555
556 snapshot.type = renv_settings_impl(
557 name = "snapshot.type",
558 default = "implicit",
559 scalar = TRUE,
560 validate = c("all", "custom", "implicit", "explicit", "packrat", "simple"),
561 coerce = as.character,
562 update = NULL
563 ),
564
565 snapshot.dev = renv_settings_impl(
566 name = "snapshot.dev",
567 default = FALSE,
568 scalar = TRUE,
569 validate = is.logical,
570 coerce = as.logical,
571 update = NULL
572 ),
573
574 use.cache = renv_settings_impl(
575 name = "use.cache",
576 default = TRUE,
577 scalar = TRUE,
578 validate = is.logical,
579 coerce = as.logical,
580 update = renv_settings_updated_cache
581 ),
582
583 vcs.manage.ignores = renv_settings_impl(
584 name = "vcs.manage.ignores",
585 default = TRUE,
586 scalar = TRUE,
587 validate = is.logical,
588 coerce = as.logical,
589 update = NULL
590 ),
591
592 vcs.ignore.cellar = renv_settings_impl(
593 name = "vcs.ignore.cellar",
594 default = TRUE,
595 scalar = TRUE,
596 validate = is.logical,
597 coerce = as.logical,
598 update = renv_settings_updated_ignore
599 ),
600
601 vcs.ignore.library = renv_settings_impl(
602 name = "vcs.ignore.library",
603 default = TRUE,
604 scalar = TRUE,
605 validate = is.logical,
606 coerce = as.logical,
607 update = renv_settings_updated_ignore
608 ),
609
610 vcs.ignore.local = renv_settings_impl(
611 name = "vcs.ignore.local",
612 default = TRUE,
613 scalar = TRUE,
614 validate = is.logical,
615 coerce = as.logical,
616 update = renv_settings_updated_ignore
617 )
618
619 )