Debian Codegraph @

R/use.R

raw
1
2 the$use_libpath <- NULL
3
4 #' @rdname embed
5 #'
6 #' @param ...
7 #' The \R packages to be used with this script. Ignored if `lockfile` is
8 #' non-`NULL`.
9 #'
10 #' @param lockfile
11 #' The lockfile to use. When supplied, renv will use the packages as
12 #' declared in the lockfile.
13 #'
14 #' @param library
15 #' The library path into which the requested packages should be installed.
16 #' When `NULL` (the default), a library path within the \R temporary
17 #' directory will be generated and used. Note that this same library path
18 #' will be re-used on future calls to `renv::use()`, allowing `renv::use()`
19 #' to be used multiple times within a single script.
20 #'
21 #' @param isolate
22 #' Boolean; should the active library paths be included in the set of library
23 #' paths activated for this script? Set this to `TRUE` if you only want the
24 #' packages provided to `renv::use()` to be visible on the library paths.
25 #'
26 #' @param sandbox
27 #' Should the system library be sandboxed? See the sandbox documentation in
28 #' [renv::config] for more details. You can also provide an explicit sandbox
29 #' path if you want to configure where `renv::use()` generates its sandbox.
30 #' By default, the sandbox is generated within the \R temporary directory.
31 #'
32 #' @param attach
33 #' Boolean; should the set of requested packages be automatically attached?
34 #' If `TRUE`, packages will be loaded and attached via a call
35 #' to [library()] after install. Ignored if `lockfile` is non-`NULL`.
36 #'
37 #' @param verbose
38 #' Boolean; be verbose while installing packages?
39 #'
40 #' @param repos
41 #' The \R package repositories to use. When `NULL`, packages will be
42 #' resolved from the renv cache, without querying any external repositories.
43 #' This can be useful if you'd like to use packages that have already been
44 #' cached by renv, even when no active package repositories have been
45 #' configured.
46 #'
47 #' @return
48 #' This function is normally called for its side effects.
49 #'
50 #' @export
51 use <- function(...,
52 lockfile = NULL,
53 library = NULL,
54 repos = getOption("repos"),
55 isolate = TRUE,
56 sandbox = TRUE,
57 attach = FALSE,
58 verbose = TRUE)
59 {
60 # allow use of the cache in this context
61 renv_scope_options(renv.cache.linkable = TRUE)
62
63 # treat NULL repos as an implicit request to only use the cache
64 cacheonly <- is.null(repos)
65
66 # set up sandbox if requested
67 renv_use_sandbox(sandbox)
68
69 # prepare library and activate library
70 library <- library %||% renv_use_libpath()
71 ensure_directory(library)
72
73 # set library paths
74 libpaths <- c(library, if (!isolate) .libPaths())
75 renv_libpaths_set(libpaths)
76
77 # override repos
78 renv_scope_options(repos = repos)
79
80 # if we were supplied a lockfile, use it
81 if (!is.null(lockfile)) {
82 renv_scope_options(renv.verbose = verbose)
83 if (cacheonly)
84 records <- renv_use_cacheonly_restore(lockfile = lockfile, library = library)
85 else
86 records <- restore(lockfile = lockfile, clean = FALSE, prompt = FALSE)
87 return(invisible(records))
88 }
89
90 dots <- list(...)
91 if (empty(dots))
92 return(invisible())
93
94 # resolve the provided remotes
95 records <- lapply(dots, renv_remotes_resolve, latest = !cacheonly)
96 names(records) <- map_chr(records, `[[`, "Package")
97
98 # for cache-only installs, resolve dependencies before filtering
99 # so that dependencies of already-installed packages are still considered
100 if (cacheonly)
101 records <- renv_use_cacheonly_resolve(records = records, library = library)
102
103 # remove any remotes which already appear to be installed
104 compat <- enum_lgl(records, function(package, record) {
105
106 # check if the package is installed
107 if (!renv_package_installed(package, lib.loc = library))
108 return(FALSE)
109
110 # check if the installed package is compatible
111 record <- resolve(record)
112 current <- renv_snapshot_description(package = package)
113 diff <- renv_lockfile_diff_record(record, current)
114
115 # a null diff implies the two records are compatible
116 is.null(diff)
117
118 })
119
120 # drop the already-installed compatible records
121 records <- records[!compat]
122 if (empty(records))
123 return(invisible())
124
125 # install packages
126 if (cacheonly) {
127 records <- renv_use_cacheonly_install(records = records, library = library)
128 } else {
129 records <- local({
130 renv_scope_options(renv.verbose = verbose)
131 install(packages = records, library = library, rebuild = character(), prompt = FALSE)
132 })
133 }
134
135 # automatically load the requested remotes
136 if (attach) {
137 enumerate(records, function(package, remote) {
138 library(package, character.only = TRUE)
139 })
140 }
141
142 # return set of installed packages
143 invisible(records)
144
145 }
146
147 renv_use_libpath <- function() {
148 (the$use_libpath <- the$use_libpath %||% tempfile("renv-use-libpath-"))
149 }
150
151 renv_use_cacheonly_restore <- function(lockfile, library) {
152
153 # read the lockfile
154 if (is.character(lockfile))
155 lockfile <- renv_lockfile_read(lockfile)
156
157 # apply overrides
158 lockfile <- renv_lockfile_override(lockfile)
159
160 # extract and install records from cache
161 records <- renv_lockfile_records(lockfile)
162 renv_use_cacheonly_install(records = records, library = library)
163
164 }
165
166 renv_use_cacheonly_resolve <- function(records, library) {
167
168 # resolve transitive dependencies from cached package DESCRIPTION files
169 visited <- new.env(parent = emptyenv())
170 base <- renv_packages_base()
171
172 enumerate(records, function(package, record) {
173 renv_use_cacheonly_resolve_impl(package, record, visited, base)
174 })
175
176 as.list(visited)
177
178 }
179
180 renv_use_cacheonly_resolve_impl <- function(package, record, visited, base,
181 require = "", version = "") {
182
183 # skip if already visited or a base package
184 if (!is.null(visited[[package]]) || package %in% base)
185 return()
186
187 # find a suitable cached path for this record
188 path <- renv_use_cacheonly_find(record, require, version)
189 valid <- nzchar(path) && renv_cache_package_validate(path)
190
191 # update the record with the resolved version from the cache path,
192 # so the install phase uses the same version we selected here
193 if (valid)
194 record$Version <- renv_path_component(path, 3L)
195
196 # always store the record so the install phase can report cache misses
197 visited[[package]] <- record
198
199 # can't resolve sub-dependencies without a valid cache entry
200 if (!valid)
201 return()
202
203 # read dependencies from the cached package DESCRIPTION
204 deps <- renv_dependencies_discover_description(path, fields = "strong")
205 if (!is.data.frame(deps) || nrow(deps) == 0L)
206 return()
207
208 needed <- renv_vector_diff(deps$Package, c(ls(visited), base))
209 for (dep in needed) {
210 idx <- match(dep, deps$Package)
211 deprecord <- renv_remotes_resolve(dep, latest = FALSE)
212 renv_use_cacheonly_resolve_impl(
213 dep, deprecord, visited, base,
214 require = deps$Require[[idx]],
215 version = deps$Version[[idx]]
216 )
217 }
218
219 }
220
221 renv_use_cacheonly_find <- function(record, require = "", version = "") {
222
223 # if the record has a specific version, use it directly
224 path <- renv_cache_find(record)
225 if (nzchar(path))
226 return(path)
227
228 # if a specific version was requested but not found, don't fall back
229 if (!is.null(record$Version))
230 return("")
231
232 # no specific version requested; search the cache for a suitable version
233 paths <- renv_cache_list(packages = record$Package)
234 if (!length(paths))
235 return("")
236
237 # extract versions from cache paths (<package>/<version>/<hash>/<package>)
238 versions <- renv_path_component(paths, 3L)
239
240 # sort by version descending so we prefer the newest
241 order <- order(renv_version_rank(versions), decreasing = TRUE)
242 paths <- paths[order]
243 versions <- versions[order]
244
245 # if we have a version constraint, filter to satisfying versions
246 if (nzchar(require) && nzchar(version)) {
247 constraint <- paste(require, version)
248 satisfied <- map_lgl(versions, function(v) {
249 renv_version_requirement_satisfied(v, constraint)
250 })
251 paths <- paths[satisfied]
252 }
253
254 head(paths, n = 1L) %||% ""
255
256 }
257
258 renv_use_cacheonly_install <- function(records, library) {
259
260 # determine how to place packages from cache into library
261 linkable <- getOption("renv.cache.linkable", default = FALSE)
262 linker <- if (linkable) renv_file_link else renv_file_copy
263
264 installed <- list()
265
266 enumerate(records, function(package, record) {
267
268 # skip base packages
269 if (package %in% renv_packages_base())
270 return()
271
272 # skip packages that aren't cacheable
273 if (!renv_record_cacheable(record))
274 return()
275
276 # look up package in the cache
277 path <- renv_cache_find(record)
278
279 # if the record has no version (e.g. resolved with latest = FALSE),
280 # fall back to searching the cache for the newest available version
281 if (!nzchar(path) && is.null(record$Version))
282 path <- renv_use_cacheonly_find(record)
283
284 if (!nzchar(path) || !renv_cache_package_validate(path)) {
285 writef("- Package '%s' is not available in the cache.", package)
286 return()
287 }
288
289 # skip if the target is already up-to-date
290 target <- file.path(library, package)
291 if (renv_file_same(path, target))
292 return()
293
294 # back up any existing installation, then link / copy from cache
295 callback <- renv_file_backup(target)
296 defer(callback())
297 linker(path, target)
298
299 installed[[package]] <<- record
300
301 })
302
303 invisible(installed)
304
305 }
306
307 renv_use_sandbox <- function(sandbox) {
308
309 if (identical(sandbox, FALSE))
310 return(FALSE)
311
312 if (renv_sandbox_activated())
313 return(TRUE)
314
315 sandbox <- if (is.character(sandbox))
316 sandbox
317 else
318 file.path(tempdir(), "renv-sandbox")
319
320 renv_scope_options(renv.config.sandbox.enabled = TRUE)
321 renv_sandbox_activate_impl(sandbox = sandbox)
322
323 reg.finalizer(renv_envir_self(), function(envir) {
324 tryCatch(
325 renv_sandbox_unlock(sandbox),
326 condition = identity
327 )
328 }, onexit = TRUE)
329
330 }