Skip to content

Debugging seems off by one frame and focuses on the parent frame #15072

Closed

Description

@olivroy

System details

RStudio Edition : Desktop [Open Source]
RStudio Version : 2024.8.0.331
OS Version      : Windows 10 x64 (build 19045)
R Version       : R version 4.4.1 (2024-06-14 ucrt)

Steps to reproduce the problem

Split from #15016

  1. In a package, have the following code under R
f_user_facing <- function(x) {
  f_internal1(x)
}

f_internal1 <- function(y) {
  y
  for (i in seq_along(y)) {
    if (y == 1) {
      w <- f_internal2(y) ** 2
    } else {
      w <- y + 2
    }

  }

  w + 1
}
f_internal2 <- function(y) {
  w <- y ** 2

  w + 1
}
  1. Set a breakpoint at the line of the `if() statement.

image

  1. Call devtools::load_all() or source().

  2. Call f_user_facing(2) in the console.

If this can't be reproduced easily,

Describe the problem in detail

It seems that the frame is off by one in this situation, and focuses on the parent function.

As you can see, the green arrow is not focused in the correct place.

image

Additional things I noted while trying to access objects

Warning in pos > offsets[linepref] :
   size of a longer object is not a multiple of the size of a shorter object.
  la taille d'un objet plus long n'est pas multiple de la taille d'un objet plus court
Calls: .rs.simulateSourceRefs -> which

Which suggests the sourceRefs aren't properly counted?

Also, I just upgraded to R 4.4.1 and don't remember this being in issue in R 4.3.2, but I may be mistaken.

Describe the behavior you expected

Note the correct functionality is working, it is just the editor focus that is not in the correct place. (i.e.
I would expect the editor to be focused)
A workaround for now is to insert a browser() statement.

Note that this is a very minimal reprex, but the issue makes debugging near impossible in larger context (when the 2 functions are in 2 different files for example)

  • 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.

Activity

  1. added
    new
    on Aug 22, 2024
  2. changed the title [-]Debugging is off by one frame is srcRef i[/-] [+]Debugging seems off by one frame and focuses on the parent frame[/+] on Aug 22, 2024
  3. kevinushey commented on Aug 22, 2024

    @kevinushey
    Collaborator

    I think I can confirm what you saw -- the debugger works as expected with R 4.3.2, but this issue arises in R 4.4.1. Something must've changed in the R debugger internals that's tripping us up...

  4. added and removed
    new
    on Aug 22, 2024
  5. olivroy commented on Aug 23, 2024

    @olivroy
    ContributorAuthor

    I just saw the warning too on R 4.2.3 fwiw

    Warning in pos > offsets[linepref] :
       size of a longer object is not a multiple of the size of a shorter object.
    Calls: .rs.simulateSourceRefs -> which

    The problem is likely that something doesn't get passed on correctly here

    pos <- gregexpr(calltext, singleline, fixed = TRUE)[[1]]
    if (length(pos) > 1)
    {
    # 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])
    if (length(best) == 0)
    {
    # No match past the preferred line, just pick the match closest
    best <- which.min(abs(linepref - pos))
    }
    else
    {
    best <- best[1]
    }
    endpos <- pos[best] + attr(pos, "match.length")[best]
    pos <- pos[best]
    }
    else
    {
    endpos <- pos + attr(pos, "match.length")
    }
    # Return an empty source ref if we couldn't find a match
    if (length(pos) == 0 || pos < 0)
    return(c(0L, 0L, 0L, 0L, 0L, 0L))
    # Compute the starting and ending lines
    firstline <- which(offsets >= pos, arr.ind = TRUE)[1]
    lastline <- which(offsets >= endpos, arr.ind = TRUE)[1]
    if (is.na(lastline))
    lastline <- length(offsets)

    Last modified in #14360

  6. kevinushey commented on Aug 23, 2024

    @kevinushey
    Collaborator

    Some more investigation; it seems like the issue only occurs when attempting to debug within a braced expression; e.g. with

    f <- function() {
       1 + 1
       {
          2 + 2
       }
    }
    

    Setting a breakpoint on 1 + 1 seems to work; 2+ 2 does not. I also see the warning noted by @olivroy in the second case.

  7. self-assigned this
    on Aug 23, 2024
  8. added this to the Kousa Dogwood milestone on Aug 23, 2024
  9. kevinushey commented on Aug 26, 2024

    @kevinushey
    Collaborator

    This should be fixed in the latest daily builds, available at https://dailies.rstudio.com/rstudio/kousa-dogwood/. Testing would be appreciated!

  10. olivroy commented on Sep 3, 2024

    @olivroy
    ContributorAuthor

    This seems resolved! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions