r-lib / debugme

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

still calculating unused debugs? #43

Open r2evans opened 3 years ago

r2evans commented 3 years ago

Q: when a "!DEBUG ..." message does not meet the !-threshold, why is its `expression` still evaluated?

.onLoad <- function(libname, pkgname) {
  if (requireNamespace("debugme", quietly = TRUE)) {
    # https://github.com/r-lib/debugme/issues/26#issuecomment-773615879
    get(".onLoad", asNamespace("debugme"))(libname, pkgname)
    debugme::debugme()
  }
}

#' @export
testme <- function(...) {
  msg <- "!DEBUG This is a test of debug level `0+1`"
  message(msg)
  msg <- "!!DEBUG This is a test of debug level `0+2`"
  message(msg)
  TRUE
}

Sys.setenv(DEBUGME="!mypackage")
devtools::document()
testme()
# mypackage This is a test of debug level 1 +5872ms 
# This is a test of debug level 1
# !!DEBUG This is a test of debug level 2
# [1] TRUE

I expected the first expression `0+1` to be evaluated, since it should be displayed. But I expected the second msg with `0+2` to instead message the raw string. This is the output I expected:

testme()
# mypackage This is a test of debug level 1 +5872ms 
# This is a test of debug level 1
# !!DEBUG This is a test of debug level `0+2`
# [1] TRUE

(I'm not suggesting that every use of "!DEBUG" will have a corresponding message or such (by definition of this package).)

sessionInfo()
```r > sessionInfo() R version 4.0.3 (2020-10-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19042) Matrix products: default locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] compiler_4.0.3 ```
gaborcsardi commented 3 years ago

You are right, it should not evaluate the statements that are not printed.