r-lib / debugme

Easy and efficient debugging for R packages
https://r-lib.github.io/debugme/
Other
147 stars 10 forks source link

error with functions without arguments #17

Closed kforner closed 6 years ago

kforner commented 7 years ago

Hi,

I believe I stumbled on a bug with 0-args functions. Here's a reproducible example:

library(debugme)
debugme:::initialize_colors('dummy')
env <- new.env()
assign('noarg', function() 'noarg', env)
debugme(env, 'dummy')
# Error in as.function.default(x, envir) : list argument expected

traceback()
# 9: as.function.default(c(value, if (is.null(bd) || is.list(bd)) list(bd) else bd), 
#        envir)
# 8: as.function(c(value, if (is.null(bd) || is.list(bd)) list(bd) else bd), 
#        envir)
# 7: `formals<-`(`*tmp*`, value = NULL)
# 6: instrument(get(x, envir = env))
# 5: assign(x, instrument(get(x, envir = env)), envir = env)
# 4: (function (x) 
#    assign(x, instrument(get(x, envir = env)), envir = env))(dots[[1L]][[1L]])
# 3: mapply(FUN = f, ..., SIMPLIFY = FALSE)
# 2: Map(function(x) assign(x, instrument(get(x, envir = env)), envir = env), 
#        funcs)
# 1: debugme(env, "dummy")

From my understanding, it comes from here: https://github.com/gaborcsardi/debugme/blob/master/R/instrument.R#L12

Because the formals are NULL, but formals<- does not seem to accept a NULL value for the new formals. I don't understand how I did not see this behavior before, maybe the .onLoad() hook is run in a try() ?

By the way, looking at the code: https://github.com/gaborcsardi/debugme/blob/master/R/instrument.R#L1 , I have the feeling that the pkg arg is never used...