Skip to content

Commit

Permalink
Record exactly 5 backtraces
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Sep 8, 2022
1 parent 46b6344 commit 57dd573
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ mutate_cols <- function(.data, dots, caller_env, error_call = caller_env()) {
},
warning = function(w) {
# Don't entrace more than 5 warnings because this is very costly
if (is_null(w$trace) && length(warnings) <= 5) {
if (is_null(w$trace) && length(warnings) < 5) {
# The frame skipping assumes a regular `base::warning()` stack.
# Also works with warnings emitted from C with `Rf_warning()`.
w$trace <- trace_back(bottom = sys.frame(-5))
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-conditions.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,27 @@ test_that("complex backtraces with base and rlang warnings", {
last_dplyr_warnings()
})
})

test_that("`last_dplyr_warnings()` only records 5 backtraces", {
reset_dplyr_warnings()

f <- function() {
warning("msg")
1
}
df <- tibble(id = 1:10)

expect_warning(
df |>
group_by(id) |>
mutate(x = f())
)

warnings <- last_dplyr_warnings(Inf)

traces <- map(warnings, `[[`, "trace")
expect_equal(
sum(map_lgl(traces, is_null)),
5
)
})

0 comments on commit 57dd573

Please sign in to comment.