Skip to content

fix source navigation when debugging nested package functions - #18846

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

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

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Intent

Closes #18754. When lm() is called inside a function, debugonce(lm) or stepping into it with s can leave the editor showing the caller's source. The debugger now opens the generated lm source and highlights the correct statements as execution advances.

Approach

Prevent functions without source references from inheriting the caller's location, either through R_GetCurrentSrcref() or the source reference on their own call. Preserve real references for primitive evaluation and top-level source contexts. Use the same resolved or simulated reference for context changes and subsequent stepping.

Automated Tests

Added five Playwright E2E tests covering direct debugging, wrappers loaded from a file or defined in the console, stepping into lm, and source navigation inside evalq in a local environment. The tests check the displayed source and the highlighted statements.

Validation on macOS Desktop with R 4.6.1:

  • Before the fix, all three wrapper scenarios failed against RStudio 2026.09.0+175; direct debugging and the evalq control passed.
  • With the fix, the complete debugger E2E suite passed: 25 passed, 2 skipped. Both skips are existing package-build-cycle placeholders.
  • C++ session and GWT builds, TypeScript typechecking, and git diff --check passed.
  • The full local unit-test suite was not run; Windows, Linux, and Server validation remain for CI/QA.

QA Notes

Define fit_lm <- function(data) { lm(mpg ~ wt, data = data) }, then run debugonce(lm); fit_lm(mtcars). Confirm the editor shows lm and that successive n commands highlight ret.x <- x, ret.y <- y, and cl <- match.call().

Repeat by running debugonce(fit_lm); fit_lm(mtcars), advancing to the lm call with n, and stepping into it with s. Check both console-defined and file-sourced wrappers. Ordinary source-file breakpoints and debugging inside evalq should retain their source locations.

Documentation

Added a NEWS entry linked to #18754.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards (no new UI controls or layout changes)
  • A reviewer is assigned to this PR
  • This PR passes all local unit tests (focused E2E validation completed as described above)

@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:10 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:10 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:11 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:11 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:15 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:15 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:19 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:19 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:19 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:19 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:19 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 18:19 — with GitHub Actions Active
@gtritchie gtritchie added this to the 2026.10 (Blue Mistflower) milestone Sep 16, 2026
@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

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

Passed Failed Skipped Flaky
2917 0 188 2
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 9 0
✅ desktop-linux 769 0 38 0
✅ desktop-macos 773 0 33 2
✅ desktop-windows 633 0 46 0
✅ server-linux 714 0 44 0
✅ setup 26 0 18 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 9c43ac9fa63401cb99cd1d9c33be3cfebf91107e - Run #1059

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

1 blocking, 1 follow-up

🔴 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/environment/SessionEnvironment.cpp:987 — stepping inside tryCatch/test_that blocks highlights the enclosing call line, not the current statement
  • 🟠 src/cpp/session/modules/environment/SessionEnvironment.cpp:987 — every debug step now runs the full .rs.callFrames stack walk

return srcref;

// Fall back to building call frames and simulating source refs
CallFrameResult cfResult = callFramesFromR(depth, pLineDebugState.get(), &protect);

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.

🔴 Stepping through a sourced function's tryCatch({...}), withCallingHandlers, suppressWarnings, or test_that block now highlights the enclosing call's line instead of each statement.
Use currentSrcref whenever the frame at the browser's depth has source references, not only when that frame is the innermost context.

Why

Repro: source f <- function() tryCatch({ browser(); x <- 1; y <- 2 }, error = identity) and run f(), then n.

getFunctionContext sets depth to f's frame, because the browser environment is f's environment, but tryCatch, tryCatchList, tryCatchOne, and doTryCatch are inner contexts. In .rs.callFrames, contextDepth == 1L is false for f, so currentSrcref is skipped and envSrcrefMap[[format(f's env)]] returns the srcref of the tryCatch(...) call itself. Before this PR, inferDebugSrcrefs returned R_GetCurrentSrcref directly and tracked x <- 1, y <- 2. Traced through the code; not reproduced in a running session.

return srcref;

// Fall back to building call frames and simulating source refs
CallFrameResult cfResult = callFramesFromR(depth, pLineDebugState.get(), &protect);

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.

🟠 On deep stacks (Shiny, testthat), each n now does a full stack walk, adding latency to every prompt.
Compute the source reference only for the target depth, or keep a C++ fast path when the current srcref is usable.

Why

Previously the same-frame step path returned R_GetCurrentSrcref without calling into R. Now it builds descriptors for every frame, including .rs.readSrcrefLines and deparse-based .rs.simulateSourceRefs for each frame without srcrefs, then uses only src_context$srcref. Highlighted lines are unaffected; this is latency only.

- resolve same-frame debug steps through a single-frame R helper rather than
  rebuilding every frame descriptor on each Browse> prompt
- report an unresolved position as unknown, so the client leaves the debug
  highlight in place instead of moving it to line 0
- keep the call-site fallback for outer frames, so frames whose inner call is
  built internally (lapply, do.call) stay navigable in the traceback
- stop expect.poll from aborting when the source editor is not readable yet
Selecting a frame whose inner call is built internally (lapply, do.call)
must resolve to the call site in the user's file. Without the fallback the
frame is not visible in the Traceback pane at all, so the highlight cannot
move off the innermost frame.
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:35 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:35 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:37 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:39 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:39 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:51 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:51 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:51 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:51 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:51 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 21:51 — with GitHub Actions Active
Wrappers that force the debugged code as a promise (tryCatch,
withCallingHandlers, suppressWarnings) leave other frames innermost, so the
browser's frame is not frame 1. Two places assumed it was:

- .rs.callFrames applied the runtime srcref to the innermost context. Key
  that off the browser's environment instead, and exclude the same frame
  from the call-site fallback, since that is where #18754 would recur.
- commonEnvironmentStateData forced context_depth to 1 while stepping. The
  client browses call_frames[context_depth - 1], so that pointed it at
  doTryCatch (no source, highlight lost) or at the wrapper's own call site
  (highlight on the wrapper line). It only ever worked because
  R_GetCurrentSrcref's outward search leaked the position onto frame 1.

Adds wrapper_source.test.ts covering the three wrappers, outer-frame
selection, and that same-frame steps do not rebuild frame descriptors.
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 22:55 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 22:55 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:00 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:00 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:03 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:08 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:08 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:08 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:08 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:08 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 16, 2026 23:08 — with GitHub Actions Active
@kevinushey
kevinushey merged commit 22abd15 into main Sep 17, 2026
1 of 7 checks passed
@kevinushey
kevinushey deleted the bugfix/18754-nested-debug-source branch September 17, 2026 02:52
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:06 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:06 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:09 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:09 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:12 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:12 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:14 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:14 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:14 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:14 — with GitHub Actions Active
@kevinushey
kevinushey deployed to e2e-playwright September 17, 2026 03:14 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
e2e-playwright — 9c43ac9f Deployed Sep 17, 2026 by kevinushey via windows / e2e-desktop-windows (2) #1059
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