rlang icon indicating copy to clipboard operation
rlang copied to clipboard

include variable values in backtrace when known?

Open kevinushey opened this issue 6 months ago • 0 comments

Consider something like:

> options(error = rlang::entrace)
> x <- "/tmp/no/such/file"
> local({
+   readLines(x)
+ })
Error in file(con, "r") : 
  (converted from warning) cannot open file '/tmp/no/such/file': No such file or directory
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/rlang_error>
Error:
! No such file or directory
---
Backtrace:
    ▆
 1. ├─base::local(...)
 2. │ └─base::eval.parent(substitute(eval(quote(expr), envir)))
 3. │   └─base::eval(expr, p)
 4. │     └─base::eval(expr, p)
 5. └─base::eval(...)
 6.   └─base::eval(...)
 7.     └─base::readLines(x)
 8.       └─base::file(con, "r")

Would it be possible for rlang to include "scalar" values for parameters in these error traces, so that we could get something like:

Backtrace:
    ▆
 1. ├─base::local(...)
 2. │ └─base::eval.parent(substitute(eval(quote(expr), envir)))
 3. │   └─base::eval(expr, p)
 4. │     └─base::eval(expr, p)
 5. └─base::eval(...)
 6.   └─base::eval(...)
 7.     └─base::readLines(x@"/tmp/no/such/file")
 8.       └─base::file(con, "r")

(Not sure if there's a more appropriate syntax for values presented in this way)


Motivation; it would help in diagnosing backtraces like this:

== Failed tests ================================================================
-- Error ('test-update.R:7:3'): update() finds packages requiring updates from CRAN --
Error in `file(con, "rb")`: cannot open the connection
Backtrace:
     x
  1. \-renv::init() at test-update.R:7:3
  2.   \-renv:::renv_init_fini(project, profile, load, restart) at renv/R/init.R:152:3
  3.     \-renv:::renv_activate_impl(...) at renv/R/init.R:158:3
  4.       \-renv::load(project) at renv/R/activate.R:92:5
  5.         \-renv:::renv_load_minimal(project) at renv/R/load.R:99:3
  6.           \-renv:::renv_load_finish(project, lockfile) at renv/R/load.R:[204](https://github.com/rstudio/renv/actions/runs/7148982902/job/19470660872#step:6:206):3
  7.             \-renv:::renv_load_report_synchronized(project, lockfile) at renv/R/load.R:756:3
  8.               \-(function() {... at renv/R/load.R:853:3
  9.                 \-renv::status(project = project, sources = FALSE) at renv/R/load.R:856:5
 10.                   \-renv:::renv_lockfile_create(...) at renv/R/status.R:147:3
 11.                     +-base::withRestarts(...) at renv/R/lockfile.R:186:3
 12.                     | \-base (local) withOneRestart(expr, restarts[[1L]])
 13.                     |   \-base (local) doWithOneRestart(return(expr), restart)
 14.                     \-renv:::renv_lockfile_create_impl(...) at renv/R/lockfile.R:186:3
 15.                       \-renv:::renv_snapshot_packages(...) at renv/R/lockfile.R:[207](https://github.com/rstudio/renv/actions/runs/7148982902/job/19470660872#step:6:209):3
 16.                         \-renv:::map(paths, renv_snapshot_description) at renv/R/snapshot.R:1057:3
 17.                           \-base::lapply(x, f, ...) at renv/R/utils-map.R:57:3
 18.                             \-renv (local) FUN(X[[i]], ...)
 19.                               \-renv:::renv_description_read(path, package) at renv/R/snapshot.R:705:3
 20.                                 \-renv:::filebacked(...) at renv/R/description.R:26:3
 21.                                   \-renv (local) callback(path, ...) at renv/R/filebacked.R:95:3
 22.                                     \-renv:::renv_dcf_read(path, ...) at renv/R/description.R:75:3
 23.                                       +-text %||% renv_dcf_read_impl(file, ...) at renv/R/dcf.R:9:3
 24.                                       \-renv:::renv_dcf_read_impl(file, ...) at renv/R/dcf.R:9:3
 25.                                         \-base::readBin(file, what = "raw", n = 8192L) at renv/R/dcf.R:88:3
 26.                                           \-base::file(con, "rb")

kevinushey avatar Dec 09 '23 04:12 kevinushey