Repository navigation
fix source navigation when debugging nested package functions - #18846
Conversation
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
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 insidetryCatch/test_thatblocks 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.callFramesstack walk
| return srcref; | ||
|
|
||
| // Fall back to building call frames and simulating source refs | ||
| CallFrameResult cfResult = callFramesFromR(depth, pLineDebugState.get(), &protect); |
There was a problem hiding this comment.
🔴 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); |
There was a problem hiding this comment.
🟠 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.
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.
Intent
Closes #18754. When
lm()is called inside a function,debugonce(lm)or stepping into it withscan leave the editor showing the caller's source. The debugger now opens the generatedlmsource 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 insideevalqin a local environment. The tests check the displayed source and the highlighted statements.Validation on macOS Desktop with R 4.6.1:
evalqcontrol passed.git diff --checkpassed.QA Notes
Define
fit_lm <- function(data) { lm(mpg ~ wt, data = data) }, then rundebugonce(lm); fit_lm(mtcars). Confirm the editor showslmand that successivencommands highlightret.x <- x,ret.y <- y, andcl <- match.call().Repeat by running
debugonce(fit_lm); fit_lm(mtcars), advancing to thelmcall withn, and stepping into it withs. Check both console-defined and file-sourced wrappers. Ordinary source-file breakpoints and debugging insideevalqshould retain their source locations.Documentation
Added a NEWS entry linked to #18754.
Checklist
NEWS.md