Debian Codegraph @

R/diagnostics.R

raw
1
2 #' Print a diagnostics report
3 #'
4 #' Print a diagnostics report, summarizing the state of a project using renv.
5 #' This report can occasionally be useful when diagnosing issues with renv.
6 #'
7 #' @inheritParams renv-params
8 #'
9 #' @return This function is normally called for its side effects.
10 #'
11 #' @export
12 diagnostics <- function(project = NULL) {
13
14 renv_scope_error_handler()
15
16 project <- renv_project_resolve(project)
17 renv_project_lock(project = project)
18
19 if (renv_file_type(project, symlinks = FALSE) != "directory") {
20 fmt <- "project %s is not a directory"
21 stopf(fmt, renv_path_pretty(project))
22 }
23
24 renv_scope_options(renv.verbose = TRUE)
25
26 reporters <- list(
27 renv_diagnostics_os,
28 renv_diagnostics_session,
29 renv_diagnostics_project,
30 renv_diagnostics_status,
31 renv_diagnostics_packages,
32 renv_diagnostics_sysreqs,
33 renv_diagnostics_r,
34 renv_diagnostics_abi,
35 renv_diagnostics_profile,
36 renv_diagnostics_settings,
37 renv_diagnostics_options,
38 renv_diagnostics_envvars,
39 renv_diagnostics_path,
40 renv_diagnostics_cache
41 )
42
43 fmt <- "Diagnostics Report [renv %s]"
44 title <- sprintf(fmt, renv_metadata_version_friendly())
45 lines <- paste(rep.int("=", nchar(title)), collapse = "")
46 writef(c(title, lines, ""))
47
48 for (reporter in reporters) {
49 tryCatch(reporter(project), error = renv_error_handler)
50 writef()
51 }
52
53 }
54
55 renv_diagnostics_os <- function(project) {
56
57 if (renv_platform_linux()) {
58 releases <- list.files("/etc", pattern = "-release$", full.names = TRUE)
59 for (release in releases) {
60 writef(header(release))
61 writeLines(readLines(release))
62 writef()
63 }
64 }
65
66 }
67
68 renv_diagnostics_session <- function(project) {
69 writef(header("Session Info"))
70 renv_scope_options(width = 80)
71 print(sessionInfo())
72 }
73
74 renv_diagnostics_project <- function(project) {
75 writef(header("Project"))
76 writef("Project path: %s", renv_path_pretty(project))
77 }
78
79 renv_diagnostics_status <- function(project) {
80 writef(header("Status"))
81 status(project = project)
82 }
83
84 renv_diagnostics_packages <- function(project) {
85
86 writef(header("Packages"))
87
88 # collect state of lockfile, library, dependencies
89 lockfile <- renv_diagnostics_packages_lockfile(project)
90 libstate <- renv_diagnostics_packages_library(project)
91 used <- unique(renv_diagnostics_packages_dependencies(project)$Package)
92
93 # collect recursive package dependencies
94 recdeps <- renv_package_dependencies(
95 packages = used,
96 project = project
97 )
98
99 # bundle together
100 all <- c(
101 names(lockfile$Packages),
102 names(libstate$Packages),
103 names(recdeps),
104 used
105 )
106
107 # sort
108 all <- csort(unique(all))
109
110 # check which packages are direct, indirect requirements
111 deps <- rep.int(NA_character_, length(all))
112 names(deps) <- all
113 deps[names(recdeps)] <- "indirect"
114 deps[used] <- "direct"
115
116 # build libpaths for installed packages
117 libpaths <- dirname(map_chr(all, renv_package_find))
118
119 # use short form
120 flibpaths <- factor(libpaths, levels = .libPaths())
121
122 # construct integer codes (to be reported in data output)
123 libcodes <- as.integer(flibpaths)
124 libcodes[!is.na(libcodes)] <- sprintf("[%i]", libcodes[!is.na(libcodes)])
125
126 # add in packages in library
127 data <- data_frame(
128 Library = renv_diagnostics_packages_version(libstate, all),
129 Source = renv_diagnostics_packages_sources(libstate, all),
130 Lockfile = renv_diagnostics_packages_version(lockfile, all),
131 Source = renv_diagnostics_packages_sources(lockfile, all),
132 Path = libcodes,
133 Dependency = deps
134 )
135
136 # we explicitly want to use rownames here
137 row.names(data) <- names(deps)
138
139 # print it out
140 renv_scope_options(width = 9000)
141 print(data, max = 10000)
142
143 # print library codes
144 fmt <- "[%s]: %s"
145 writef()
146 writef(fmt, format(seq_along(levels(flibpaths))), format(levels(flibpaths)))
147
148 }
149
150 renv_diagnostics_r <- function(project) {
151
152 makeconf <- file.path(R.home("etc"), "Makeconf")
153 if (file.exists(makeconf)) {
154 writef(header(makeconf))
155 writeLines(readLines(makeconf))
156 writeLines("")
157 }
158
159 tools <- renv_namespace_load("tools")
160 keys <- c("makevars_user", "makevars_site")
161 for (key in keys) {
162 if (is.function(tools[[key]])) {
163 paths <- tools[[key]]()
164 for (path in paths) {
165 if (file.exists(path)) {
166 writef(header(path))
167 writeLines(readLines(path))
168 writeLines("")
169 }
170 }
171 }
172 }
173
174 if (getRversion() >= "3.6.0") {
175 writef(header("R CMD config --all"))
176 system2(R(), c("CMD", "config", "--all"))
177 }
178
179 }
180
181 renv_diagnostics_sysreqs <- function(project) {
182
183 if (!renv_platform_linux())
184 return()
185
186 writef(header("R System Requirements"))
187
188 lockfile <- renv_lockfile_create(project)
189 records <- renv_lockfile_records(lockfile)
190 sysreqs <- map(records, `[[`, "SystemRequirements")
191 ok <- renv_sysreqs_check(sysreqs, prompt = FALSE)
192 invisible(ok)
193
194 }
195
196 renv_diagnostics_packages_version <- function(lockfile, all) {
197
198 data <- rep.int(NA_character_, length(all))
199 names(data) <- all
200
201 formatted <- map_chr(lockfile$Packages, `[[`, "Version")
202 data[names(formatted)] <- formatted
203
204 data
205
206 }
207
208 renv_diagnostics_packages_sources <- function(lockfile, all) {
209
210 data <- rep.int(NA_character_, length(all))
211 names(data) <- all
212
213 sources <- map_chr(lockfile$Packages, function(record) {
214 record$Repository %||% record$Source %||% "<unknown>"
215 })
216
217 data[names(sources)] <- sources
218 data
219
220 }
221
222 renv_diagnostics_packages_lockfile <- function(project) {
223
224 lockpath <- renv_lockfile_path(project = project)
225 if (!file.exists(lockpath)) {
226 writef("This project has not yet been snapshotted: 'renv.lock' does not exist.")
227 return(list())
228 }
229
230 renv_lockfile_read(lockpath)
231
232 }
233
234 renv_diagnostics_packages_library <- function(project) {
235
236 library <- renv_paths_library(project = project)
237 if (!file.exists(library)) {
238 fmt <- "The project library %s does not exist."
239 writef(fmt, renv_path_pretty(library))
240 }
241
242 snapshot(project = project, lockfile = NULL, type = "all")
243
244 }
245
246 renv_diagnostics_packages_dependencies <- function(project) {
247
248 renv_dependencies_impl(
249 project,
250 errors = "reported",
251 dev = TRUE
252 )
253
254 }
255
256 renv_diagnostics_abi <- function(project) {
257
258 if (!renv_platform_linux())
259 return()
260
261 writef(header("ABI"))
262 tryCatch(
263 renv_abi_check(),
264 error = function(e) {
265 writef(conditionMessage(e))
266 }
267 )
268
269 }
270
271 renv_diagnostics_profile <- function(project) {
272
273 writef(header("User Profile"))
274
275 userprofile <- "~/.Rprofile"
276 if (!file.exists(userprofile))
277 return(writef("[no user profile detected]"))
278
279 deps <- renv_dependencies_impl(
280 userprofile,
281 errors = "reported",
282 dev = TRUE
283 )
284
285 if (empty(deps))
286 return(writef("[no R packages referenced in user profile"))
287
288 renv_scope_options(width = 200)
289 print(deps)
290
291 }
292
293 renv_diagnostics_settings <- function(project) {
294 writef(header("Settings"))
295 str(renv_settings_get(project))
296 }
297
298 renv_diagnostics_options <- function(project) {
299
300 writef(header("Options"))
301
302 keys <- c(
303 "defaultPackages",
304 "download.file.method",
305 "download.file.extra",
306 "install.packages.compile.from.source",
307 "pkgType",
308 "repos",
309 grep("^renv[.]", names(.Options), value = TRUE)
310 )
311
312 vals <- .Options[keys]
313 names(vals) <- keys
314
315 str(vals)
316
317 }
318
319 renv_diagnostics_envvars <- function(project) {
320
321 writef(header("Environment Variables"))
322
323 envvars <- convert(as.list(Sys.getenv()), "character")
324
325 useful <- c(
326 "CC", "CXX", "CPPFLAGS", "CFLAGS", "CXXFLAGS", "LDFLAGS",
327 "R_LIBS_USER", "R_LIBS_SITE", "R_LIBS",
328 "HOME", "LANG", "MAKE",
329 grep("_proxy", names(envvars), ignore.case = TRUE, value = TRUE),
330 grep("^RENV_", names(envvars), value = TRUE)
331 )
332
333 matches <- envvars[useful]
334 if (empty(matches))
335 return(writef("[no renv environment variables available]"))
336
337 names(matches) <- useful
338 matches[is.na(matches)] <- "<NA>"
339 matches <- matches[order(names(matches))]
340
341 keys <- names(matches)
342 vals <- matches
343 formatted <- paste(format(keys), vals, sep = " = ")
344 writef(formatted)
345
346 }
347
348 renv_diagnostics_path <- function(project) {
349 writef(header("PATH"))
350 path <- strsplit(Sys.getenv("PATH"), .Platform$path.sep, fixed = TRUE)[[1]]
351 writef(paste("-", path))
352 }
353
354 renv_diagnostics_cache <- function(project) {
355
356 writef(header("Cache"))
357
358 fmt <- "There are a total of %s installed in the renv cache."
359 cachelist <- renv_cache_list()
360 writef(fmt, nplural("package", length(cachelist)))
361 writef("Cache path: %s", renv_path_pretty(renv_paths_cache()))
362
363 }