Debian Codegraph @

R/repair.R

raw
1
2 #' Repair a project
3 #'
4 #' Use `repair()` to recover from some common issues that can occur with
5 #' a project. Currently, two operations are performed:
6 #'
7 #' 1. Packages with broken symlinks into the cache will be re-installed.
8 #'
9 #' 2. Packages that were installed from sources, but appear to be from
10 #' an remote source (e.g. GitHub), will have their `DESCRIPTION` files
11 #' updated to record that remote source explicitly.
12 #'
13 #' @inheritParams renv-params
14 #'
15 #' @param lockfile The path to a lockfile (if any). When available, renv
16 #' will use the lockfile when attempting to infer the remote associated
17 #' with the inaccessible version of each missing package. When `NULL`
18 #' (the default), the project lockfile will be used.
19 #'
20 #' @export
21 repair <- function(library = NULL,
22 lockfile = NULL,
23 project = NULL)
24 {
25 renv_consent_check()
26 renv_scope_error_handler()
27
28 project <- renv_project_resolve(project)
29 renv_project_lock(project = project)
30
31 libpaths <- renv_path_normalize(library %||% renv_libpaths_all())
32 library <- libpaths[[1L]]
33
34 writef(header("Library cache links"))
35 renv_repair_links(library, lockfile, project)
36 writef()
37
38 writef(header("Package sources"))
39 renv_repair_sources(library, lockfile, project)
40 writef()
41
42 invisible()
43 }
44
45 renv_repair_links <- function(library, lockfile, project) {
46
47
48 # figure out which library paths (junction points?) appear to be broken
49 paths <- list.files(library, full.names = TRUE)
50 broken <- renv_file_broken(paths)
51 packages <- basename(paths[broken])
52 if (empty(packages)) {
53 writef("- No issues found with the project library's cache links.")
54 return(invisible(packages))
55 }
56
57 # try to find records for these packages in the lockfile
58 # TODO: what if one of the requested packages isn't in the lockfile?
59 lockfile <- lockfile %||% renv_lockfile_load(project = project)
60 records <- renv_repair_records(packages, lockfile, project)
61
62 # install these records
63 install(
64 packages = records,
65 library = library,
66 project = project
67 )
68
69 }
70
71 renv_repair_records <- function(packages, lockfile, project) {
72 map(packages, function(package) {
73 lockfile$Packages[[package]] %||% package
74 })
75 }
76
77 renv_repair_sources <- function(library, lockfile, project) {
78
79 # get package description files
80 db <- installed_packages(lib.loc = library, priority = NA_character_)
81 descpaths <- with(db, file.path(LibPath, Package, "DESCRIPTION"))
82 dcfs <- map(descpaths, renv_description_read)
83 names(dcfs) <- map_chr(dcfs, `[[`, "Package")
84
85 # try to infer sources as necessary
86 inferred <- map(dcfs, renv_repair_sources_infer)
87 inferred <- filter(inferred, Negate(is.null))
88 if (length(inferred) == 0L) {
89 writef("- All installed packages appear to be from a known source.")
90 return(TRUE)
91 }
92
93 # ask used
94 renv_scope_options(renv.verbose = TRUE)
95 bulletin(
96 c(
97 "The following package(s) do not have an explicitly-declared remote source.",
98 "However, renv was available to infer remote sources from their DESCRIPTION file."
99 ),
100 sprintf("%s [%s]", format(names(inferred)), inferred),
101 "`renv::restore()` may fail for packages without an explicitly-declared remote source."
102 )
103
104 choice <- menu(
105
106 choices = c(
107 update = "Let renv infer the remote sources for these packages.",
108 cancel = "Do nothing and resolve the situation another way."
109 ),
110
111 title = "What would you like to do?"
112
113 )
114
115 cancel_if(identical(choice, "cancel"))
116
117 enumerate(inferred, function(package, remote) {
118 record <- renv_remotes_resolve(remote)
119 record[["RemoteSha"]] <- NULL
120 renv_package_augment(file.path(library, package), record)
121 })
122
123 n <- length(inferred)
124 writef("- Updated %i package DESCRIPTION %s.", n, nplural("file", n))
125
126 TRUE
127
128 }
129
130 renv_repair_sources_infer <- function(dcf) {
131
132 # if this package appears to have a declared remote, use as-is
133 for (field in c("RemoteType", "Repository", "biocViews")) {
134 value <- dcf[[field]]
135 if (nzchar(value %||% ""))
136 return(NULL)
137 }
138
139 # ok, this is a package installed from sources that "looks" like
140 # the development version of a package; try to guess its remote
141 guess <- function(pattern, field) {
142 urls <- strsplit(dcf[[field]] %||% "", "\\s*,\\s*")[[1L]]
143 for (url in urls) {
144 matches <- regmatches(url, regexec(pattern, url, perl = TRUE))[[1L]]
145 if (length(matches) == 3L)
146 return(paste(matches[[2L]], matches[[3L]], sep = "/"))
147 }
148 }
149
150 # first, check bug reports
151 remote <- guess("^https://(?:www\\.)?github\\.com/([^/]+)/([^/]+)/issues$", "BugReports")
152 if (!is.null(remote))
153 return(remote)
154
155 # next, check the URL field
156 remote <- guess("^https://(?:www\\.)?github\\.com/([^/]+)/([^/]+)", "URL")
157 if (!is.null(remote))
158 return(remote)
159
160 }