Skip to content

address review findings on debug frame resolution - #18863

Merged
kevinushey merged 2 commits into
mainfrom
bugfix/18754-nested-debug-source
Sep 17, 2026
Merged

kevinushey merged 2 commits into
mainfrom
bugfix/18754-nested-debug-source

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Follow-up to #18846, which fixed #18754 but left a few loose ends found in review.

That PR changed what context_depth means in the environment-state payload: it
now names the frame the browser is halted in rather than being forced to 1 while
stepping. Two client-side predicates still read it as "execution is halted here",
and two source-reference paths needed narrowing.

Changes

  • Report the browse frame's depth separately. context_depth also follows the
    frame the user selects from the call stack, so it cannot answer "is this the
    executing frame". The payload now carries browse_frame_depth alongside it, and
    EnvironmentObjects.enableClickableObjects() and the CodeBrowserNavigationEvent
    executing flag compare against that instead of hardcoding 1. Without this, clicking
    a data frame in the Environment pane stopped calling View() and the code browser
    lost its executing-line arrow whenever the browsed frame was not innermost -- any
    tryCatch, withCallingHandlers or suppressWarnings wrapper, or a Shiny
    reactive. This also fixes the same question being answered wrongly after selecting
    an outer frame from the call stack, which predates fix source navigation when debugging nested package functions #18846.

  • Keep the call-site fallback for a browsed frame that has source of its own.
    The new branch nulled the srcref unconditionally, including when the function
    carried source references -- reachable when R_GetCurrentSrcref() returns the
    <in-bc-interp> symbol for byte-compiled code. That sent the client to a deparsed
    copy of the function with an unnavigable filename instead of to the file. Debugging internal R functions in RStudio does not go to source #18754
    is about functions with no source of their own, so the null-out is now gated on
    that.

  • Resolve the browsed frame to the outermost context sharing the browser
    environment
    , matching what .rs.getFunctionContext reports to the client. More
    than one context can share an environment -- eval(expr, envir = <a frame env>),
    or top-level code under source() -- and the previous per-frame identity test
    marked every one of them current, so the frame the client reads could fall into
    the wrong branch and simulate a wrong line.

  • Repeat the last browser position when a step cannot be resolved. Suppressing
    the event entirely left the client's requery timer armed; it stalled 500 ms and
    then issued a requery_context RPC that resolved to nothing again.

  • Drop src_context from .rs.callFrames, which no longer has a reader on the
    C++ side.

  • Assert the trace installed in the deep-stack e2e test. It asserted
    FRAME_BUILDS: 0, which is also what a trace() that never installed leaves
    behind, so the test would have kept passing if the regression returned.

Testing

Added a testthat case covering the restored fallback; it fails on two assertions
with the hasSourceRefs gate reverted.

  • rstudio-tests --scope r --filter 'debugger|environment|linter' -- 205 pass
  • npm run test:desktop-dev -- tests/panes/debugger -- 33 passed, 5 skipped
  • npm run test:desktop-dev -- tests/panes/environment -- 9 passed, 3 skipped

No NEWS.md entry: this corrects work from #18846, which has not shipped in a
release yet.

- report the browse frame's depth separately from context_depth, which now
  follows the frame the user selects from the call stack; the client read
  context_depth == 1 as "execution is halted here" for clickable objects and
  the code browser's executing-line arrow
- keep the call-site fallback for the browsed frame when the function has
  source of its own, so an empty runtime srcref no longer sends it to a
  deparsed copy instead of the file
- resolve the browsed frame to the outermost context sharing the browser
  environment, matching .rs.getFunctionContext, so eval(envir = <a frame>)
  and top-level code under source() name the same frame the client browses
- repeat the last browser position when a step cannot be resolved, so the
  client stops waiting on its requery timer
- drop src_context from .rs.callFrames, which no longer has a reader
- assert the trace installed in the deep-stack e2e test, which asserted a
  counter that a failed trace would also leave at zero
@kevinushey kevinushey added this to the 2026.10 (Blue Mistflower) milestone Sep 17, 2026
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 17:22 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 17:22 — with GitHub Actions Active

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issues. 2 follow-up (+0 pre-existing, not commented)

🔴 must be resolved before merge. 🟠 is real but not merge-blocking — either address it here or open an issue to do so later. 🟡 is optional.

  • 🟠 src/cpp/session/modules/SessionEnvironment.R:1460 — halted frame with source reports its caller's call site as its own position
  • 🟠 src/cpp/session/modules/environment/SessionEnvironment.cpp:1137 — repeat-last-position fallback is empty for the first step after a frame change

# source of its own. Handing a source-less function its caller's
# location is exactly #18754; there we leave the srcref unset and
# simulate a position from the deparsed body below.
callSrcref <- attr(call, "srcref", exact = TRUE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 When the halted frame has source, this fallback reports where the caller made the call, not a line in the halted function, so the highlight can land in another file.
Use the function's own srcref here (e.g. attr(origFun, "srcref")) rather than attr(call, "srcref"), and have the new test check line_number too.

Why

attr(call, "srcref") belongs to the call expression, which sits in the caller. If located_fn lives in file A and outer_fn in file B, the frame reports file B at outer_fn's call line. The new test in test-debugger.R puts both functions in one file and only checks file_name, so it passes either way.

Not merge-blocking: it only happens when the runtime srcref is <in-bc-interp> and no inner call is mapped, and the result is no worse than before #18846.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in 20d80c1. Reproduced it with the two functions in separate files: the browsed frame came back naming the caller's file at the caller's line.

Applied to the browsed frame only -- that line is shared with the lapply/do.call path, where the call site is the deliberate answer and is covered by test-debugger.R plus a debugger.test.ts case, so switching it wholesale would have regressed traceback navigation. Outer frames keep attr(call, "srcref"); the browsed frame now uses attr(origFun, "srcref").

Test rewritten as you suggested (two files, asserts line_number/end_line_number); with the fix reverted it fails on all three assertions, where the single-file version failed on none.

Also corrected the comment's rationale: the <in-bc-interp> claim was wrong. R_GetCurrentSrcref resolves that symbol through fixBCSrcref/R_getCurrentSrcref before we see it, so the branch is reached when the runtime srcref is absent, not when it is the symbol.

SEXP srcref = inferDebugSrcrefs(depth, pLineDebugState);
if (srcref != R_NilValue)
enqueBrowserLineChangedEvent(srcref);
else if (!s_lastBrowserLineJson.isEmpty())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 If the first step after entering or changing frames can't be resolved, nothing is sent, so the client still waits 500 ms and re-queries.
Save the position the context-change event reports into s_lastBrowserLineJson rather than clearing it at line 1116.

Why

Line 1116 clears s_lastBrowserLineJson on every frame change, and only a resolved step in the same frame fills it again. So this repeat path does nothing until one step in the new frame has succeeded.

Not merge-blocking: the cost is one 500 ms delay and one extra requery_context call; the position shown stays correct.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and fixed in 20d80c1, slightly differently than suggested. Instead of assigning at the clear site, commonEnvironmentStateData() now records the browse frame's range whenever it builds the state, so s_lastBrowserLineJson means "the position the client currently holds" on every path that reports one -- the context-change event, set_context_depth and requery_context -- rather than only resolved same-frame steps. The clear in onConsolePrompt is gone.

Checked the repeat is inert on the client: function_line_number is always 1, so functionRelativePosition(-currentFunctionLineNumber_ + 1) is identity and the repeated range compares equal to currentBrowsePosition_; the handler skips the view update and just cancels the timer. Added a bounds check on frames.getValueAt(depth - 1), since this read isn't under the contextCallfun guard the existing one has and callFramesFromR returns empty frames on error.

- the srcref fallback for the frame the browser is halted in used the call
  that created it, which is a position in the caller and can name another
  file entirely; use the function's own definition instead. outer frames keep
  the call-site fallback, which is what makes them navigable from the
  traceback
- strengthen the testthat case to put the two functions in separate files and
  assert the line, which the single-file version could not distinguish
- record the position each context-change event leaves the client at, instead
  of clearing it; the repeat-last-position path was dead until one step in the
  new frame had resolved, so the first unresolved step after a frame change
  still cost a 500 ms stall and a requery_context round trip
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 18:52 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 18:52 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 18:54 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 18:54 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 18:57 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 18:57 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 19:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 19:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 19:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 19:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 19:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 19:03 — with GitHub Actions Active
@github-actions

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
3054 0 196 4
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 10 0
✅ desktop-linux 771 0 38 0
✅ desktop-macos 767 0 42 1
✅ desktop-windows 771 0 42 2
✅ server-linux 715 0 44 1
✅ setup 28 0 20 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit 20d80c175f7f4d6c4e710f9504c871957bd8607f - Run #1068

@kevinushey
kevinushey merged commit e6d24a0 into main Sep 17, 2026
38 checks passed
@kevinushey
kevinushey deleted the bugfix/18754-nested-debug-source branch September 17, 2026 20:41

This branch was successfully deployed

1 active deployment
e2e-playwright — 20d80c17 Deployed Sep 17, 2026 by kevinushey via windows / e2e-desktop-windows (1) #1068
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Debugging internal R functions in RStudio does not go to source

2 participants