Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error for dirname receiving a non-character input on reperx that nearly crashes R #388

Closed
billdenney opened this issue Aug 13, 2021 · 3 comments

Comments

@billdenney
Copy link

billdenney commented Aug 13, 2021

When trying to replicate a base R bug (https://bugs.r-project.org/show_bug.cgi?id=16441), I wanted to give a reprex for it. When trying to generate the reprex, there was an error in reprex.

reprex::reprex({
e <- new.env()
attr(e, "e") <- e
object.size(e)
})

That resulted in the following output on the R console.

i Rendering reprex...
Error in dirname(path[!is_missing]) : 
  a character vector argument expected

This is with R 4.1.0 on Windows 10.

@billdenney
Copy link
Author

The error appears to be that out is NULL here, and that appears to be a unique output that is crashing R.

reprex/R/reprex_render.R

Lines 94 to 111 in 945d63d

withr::with_dir(
path_dir(input),
out <- tryCatch(
callr::r(
function(input) {
rmarkdown::render(
input,
quiet = TRUE, envir = globalenv(), encoding = "UTF-8"
)
},
args = list(input = path_file(input)),
spinner = is_interactive(),
stdout = if (is.null(std_file)) NULL else path_file(std_file),
stderr = if (is.null(std_file)) NULL else path_file(std_file)
),
error = function(e) e
)
)

@jennybc jennybc closed this as completed in b96611f Nov 1, 2021
@jennybc
Copy link
Member

jennybc commented Nov 1, 2021

The root problem is that this error (C stack usage ... is too close to the limit) causes the rendering to stop.

For some reason, the error caught in reprex is NULL, which I was not expecting, which lead to us trying to proceed with a non-existent path.

I think the only thing I can do in reprex is to throw a marginally more informative error.

It is interesting to ask what's special about this error and whether some other package (evaluate? knitr?) or R itself could do something differently to allow it to be knit.

If I surround the tricky code with tryCatch(), it can be reprexed:

e <- new.env()
attr(e, "e") <- e
blah <- tryCatch(
  object.size(e),
  error = function(e) e
)
blah
#> <simpleError: C stack usage  7969228 is too close to the limit>
str(blah)
#> List of 2
#>  $ message: chr "C stack usage  7969228 is too close to the limit"
#>  $ call   : NULL
#>  - attr(*, "class")= chr [1:3] "simpleError" "error" "condition"

Created on 2021-11-01 by the reprex package (v2.0.1.9000)

cc @cderv

@cderv
Copy link
Contributor

cderv commented Nov 2, 2021

Thanks Jenny for the ping. It seems this error (type of error) is not catched the same as other by evaluate

> evaluate::evaluate("stop('a')")
[[1]]
$src
[1] "stop('a')"

attr(,"class")
[1] "source"

[[2]]
<simpleError in eval(expr, envir, enclos): a>

> evaluate::evaluate('e <- new.env()
+                    attr(e, "e") <- e
+                    object.size(e)')
Error: C stack usage  15922884 is too close to the limit

So we should indeed look there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants