Breakpoints not triggered in VS Code R Debugger when using box
Error description
When using the box package to modularize code, breakpoints in VS Code's R Debugger do not seem to be triggered. This issue does not occur when using source() to load files. Since box isolates environments, it might be causing the debugger to lose track of the code context.
The function hello_world defined in func1.R runs correctly, but breakpoints set inside this function are not triggered when called
code to reproduce : main.R
box::use(
. / func1[hello_world]
)
hello_world()
func1.R
#' @export
hello_world <- function(){
x = c(1,2,3) # Breakpoint added here
print("In here")
return("Hello World")
}
R Debugger Version - v0.5.5
R version
platform x86_64-w64-mingw32
arch x86_64
os mingw32
crt ucrt
system x86_64, mingw32
status
major 4
minor 4.1
year 2024
month 06
day 14
svn rev 86737
language R
version.string R version 4.4.1 (2024-06-14 ucrt)
nickname Race for Your Life
‘box’ version
'1.2.0'
For me, the following setup works:
- Include
"setBreakpointsInPackages": truein the debug config (.vscode/launch.json). This is necessary sinceboxloads functions to an attached environment that is normally ignored by the debugger. - Add the command
vscDebugger:::setStoredBreakpoints()between thebox::usecommand and the call tohello_world. This is necessary since thehello_worldfunction does not exist yet at the beginning of the script, when the debugger tries to set breakpoints.
For this to work out of the box, we would have to overwrite/shim box::use, which I'd rather avoid given the complexity of that function. Note that at the moment, setStoredBreakpoints is not an exported function, but probably that will change given this usecase.
Thanks it worked. I can trigger break points now. Is there a way to not write this line directly in the code : vscDebugger:::setStoredBreakpoints(). Can this be globally called/ enabled without the need of directly writing this in code
Hello I found that it does not work if I use R6 classes. Any suggestions to get this working is helpful.
main.R
box::use(./src/test_module[ yeet, YeetClass ])
vscDebugger:::setStoredBreakpoints()
yeet() # this works
yeet_class <- YeetClass$new() # break point from inside function is not hit
print("done!")
./src/test_module.R
box::use(R6)
#' @export
yeet <- function() {
print("Yeet")
}
#' @export
YeetClass <- R6$R6Class(
"YeetClass",
public = list(
initialize = function() {
print("YeetClass initialized") # <--- breakpoint using red dot gutter set here is never hit
}
)
)
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "R-Debugger",
"name": "R test box modules",
"request": "launch",
"debugMode": "file",
"workingDirectory": "",
"file": "${workspaceFolder}/main.R",
"setBreakpointsInPackages": true
}
]
}
This issue seems to be related but I don't have enough background: https://github.com/ManuelHentschel/VSCode-R-Debugger/issues/165