Repository: rstudio/rstudio

Issue #13925: browser() statement opens read only view on execution unless entire function or file is sourced
Created: 2023-11-13T23:18:06Z
System details
RStudio Edition : Desktop [Open Source]
RStudio Version : 2023.12.0.278
OS Version      : macOS 13.6
R Version       : R version 4.2.0 (2022-04-22)

Steps to reproduce the problem
Any user-defined functions that aren't Sourced will open up a read-only Source Window.
Use the following code in a .R file named single-fn.R and highlight the entire script and Cmd+Enter or click on "Run".
get_index <- function(currIndex){
  print(currIndex)

  browser()
}

get_index(5)

Alternatively, create a second .R file named debug-index.R and also try running it with Cmd+Enter or via "Run". Each time you step through the function it will open/return to the read-only view.
get_index <- function(currIndex){
  print(currIndex)

  browser()
}


lapply(
  X = 1:10,
  FUN = get_index
)
Describe the problem in detail
Per the docs:

If RStudio can’t find a .R file that matches the function, it will show the code in the Source Viewer. This can happen either because no .R file exists or because the function object doesn’t match the definition in the .R file.

Even if the code for the function hasn't changed AND the source is available it is still opening a read-only view.
Overall I don't think we NEED to open a read-only view unless the function comes from a malformed package or some other location where the source code is not retrievable, unlike the interactive workflow of a local file.
I have seen a few issues that #6740 #9943 and have split off this topic from: #12505 . There's a lot of chatter on a few issues and some emails.
Describe the behavior you expected
If I source() or use the Source button in RStudio, it will keep focus on the sourced open file. I believe that this behavior should be present unless we truly find an externally defined function that the user cannot access. I think this is a regression because the source IS available and at worst, it should return focus to the file where the function exists. As a potential mitigation, perhaps we could allow users to turn off the "read only view".
I think the source for this is: 
  
    
      rstudio/src/gwt/src/org/rstudio/studio/client/workbench/views/source/editors/codebrowser/CodeBrowserEditingTarget.java
    
    
        Lines 760 to 772
      in
      68a2b9b
    
  
  
    

        
          
           @Override 
        

        
          
           public void highlightDebugLocation( 
        

        
          
                 SourcePosition startPos, 
        

        
          
                 SourcePosition endPos, 
        

        
          
                 boolean executing) 
        

        
          
           { 
        

        
          
              docDisplay_.highlightDebugLocation(startPos, endPos, executing); 
        

        
          
              if (!shownWarningBar_) 
        

        
          
              { 
        

        
          
                 view_.showWarningBar(constants_.debugLocationIsApproximate()); 
        

        
          
                 shownWarningBar_ = true; 
        

        
          
              } 
        

        
          
           } 
        
    
  


And perhaps we want to follow a heuristic that if the user closes the "Warning bar for approximate location" we should also not open the readonly view?

  
    
      rstudio/src/gwt/src/org/rstudio/studio/client/workbench/views/source/editors/codebrowser/CodeBrowserEditingTarget.java
    
    
        Lines 224 to 226
      in
      68a2b9b
    
  
  
    

        
          
           // we only show the warning bar (for debug line matching) once per 
        

        
          
           // function; don't keep showing it if the user dismisses 
        

        
          
           shownWarningBar_ = false; 
        
    
  



Steps to reproduce the problem
Describe the problem in detail
Describe the behavior you expected


 I have read the guide for submitting good bug reports.
 I have installed the latest version of RStudio, and confirmed that the issue still persists.
 If I am reporting an RStudio crash, I have included a diagnostics report.
 I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

Duplicate issue #14276: Stepping with duplicated lines can be wrong when `keep.source = FALSE`
Created: 2024-02-15T17:06:14Z
CC @kevinushey, this is what I mentioned yesterday about seeing cases where duplicate lines make the debugger bounce around
Let's fake the idea of stepping into a function from a package where you don't have source refs (pretty common)
eval(parse(text = "fn <- function() {
  x <- 1
  1 + 1
  x <- 1
  2 + 2
  3 + 3
  x <- 1
  1
}", keep.source = FALSE))

debugonce(fn)
fn()
Note that x <- 1 is duplicated here. This results in the following stepping behavior:

  
    
    

    Screen.Recording.2024-02-15.at.11.58.41.AM.mov
    
  

  

  


If you remove the keep.source = FALSE part, then things do work as expected.
I know that when there aren't source refs, we essentially end up grepping for x <- 1 in the sources of the function itself. It seems like we try and keep track of the previous debug line and use it as a minimum "line preference" here:

  
    
      rstudio/src/cpp/session/modules/SessionEnvironment.R
    
    
         Line 290
      in
      5ebce31
    
  
  
    

        
          
           linepref <- attr(var, "_rs_lastline") 
        
    
  


In theory that should work well when it is used here:

  
    
      rstudio/src/cpp/session/modules/SessionEnvironment.R
    
    
        Lines 350 to 352
      in
      5ebce31
    
  
  
    

        
          
           # There is more than one instance of the call text in the function; try 
        

        
          
           # to pick the first match past the preferred line. 
        

        
          
           best <- which(pos > offsets[linepref]) 
        
    
  


I have a feeling that somehow this lastDebugLine is not being updated correctly for some reason in the no-srcref case:

  
    
      rstudio/src/cpp/session/modules/environment/SessionEnvironment.cpp
    
    
         Line 748
      in
      5ebce31
    
  
  
    

        
          
           pLineDebugState->lastDebugLine = stepLine;

Closing comment (2024-02-29T19:43:15Z): Might be related to #13925