Skip to content

Commit 4cbe53f

Browse files
committed
Fix check_installed() within catch-all tryCatch(error = )
Closes #1402 Part of tidyverse/ggplot2#4845
1 parent 6d04348 commit 4cbe53f

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# rlang (development version)
22

3+
* `check_installed()` now works within catch-all `tryCatch(error = )`
4+
expressions (#1402, tidyverse/ggplot2#4845).
5+
36
* `arg_match()` and `arg_match0()` now mention the correct call in
47
case of type error (#1388).
58

R/session.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,17 @@ check_installed <- function(pkg,
244244
"Would you like to install it?",
245245
"Would you like to install them?"
246246
)
247-
248-
cat(paste_line(
247+
question <- paste_line(
249248
paste0(info(), " ", header),
250249
paste0(cross(), " ", question),
251250
.trailing = TRUE
252-
))
251+
)
252+
253+
if (is_true(peek_option("rlang:::check_installed_test_hook"))) {
254+
return(question)
255+
}
256+
257+
cat(question)
253258

254259
if (utils::menu(c("Yes", "No")) != 1) {
255260
# Pass condition in case caller sets up an `abort` restart
@@ -371,6 +376,8 @@ cnd_header.rlib_error_package_not_found <- function(cnd, ...) {
371376
}
372377

373378
signal_package_not_found <- function(cnd) {
379+
class(cnd) <- vec_remove(class(cnd), "error")
380+
374381
withRestarts({
375382
signalCondition(cnd)
376383
FALSE

R/utils.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,12 @@ style_rlang_run <- function(code) {
365365
paste0("rstudio:run:rlang::", code)
366366
)
367367
}
368+
369+
vec_remove <- function(x, values) {
370+
loc <- match(values, x, nomatch = 0)
371+
if (sum(loc) == 0) {
372+
x
373+
} else {
374+
x[-loc]
375+
}
376+
}

tests/testthat/_snaps/session.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,11 @@
186186
Error in `check_installed()`:
187187
! `action` must take a `...` argument.
188188

189+
# `check_installed()` works within `tryCatch(error = )` (#1402, tidyverse/ggplot2#4845)
190+
191+
Code
192+
cat(tryCatch(error = function(cnd) NULL, check_installed("rlangFoo")))
193+
Output
194+
i The package `rlangFoo` is required.
195+
x Would you like to install it?
196+

tests/testthat/test-session.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,15 @@ test_that("`action` is checked", {
204204
err(check_installed("foo", action = identity))
205205
})
206206
})
207+
208+
test_that("`check_installed()` works within `tryCatch(error = )` (#1402, tidyverse/ggplot2#4845)", {
209+
local_options("rlang:::check_installed_test_hook" = TRUE)
210+
local_interactive()
211+
212+
expect_snapshot({
213+
cat(tryCatch(
214+
error = function(cnd) NULL,
215+
check_installed("rlangFoo")
216+
))
217+
})
218+
})

0 commit comments

Comments
 (0)