Skip to content

Hosted backtraces: lines for path-less code, no host CLR frames below <main> (part of #163) - #169

Merged
Largo merged 1 commit into
masterfrom
claude/hosted-backtraces
Nov 9, 2026
Merged

Largo merged 1 commit into
masterfrom
claude/hosted-backtraces

Conversation

@Largo

@Largo Largo commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Part of #163. Items 3 and 4 are fixed. The line half of item 2 is fixed too. Items 1 and the stale-file half of 2 are not backtrace bugs, so this PR leaves them alone (see below).

Snippet from the issue, with the backtrace each path printed before and after:

Path Before After
Engine.Execute(code) (eval):2:in 'Object#many' … (eval):0:in 'Array#each', (eval):0:in '<main>', then :0:in '<Probe>b__98_1', RedirectOutput … Main (eval):3:in 'Object#many' … (eval):12:in 'Array#each', (eval):12:in '<main>' and nothing below it
ScriptSource("hosted.rb").Execute() correct lines, then the host's CLR frames below <main> the same lines as ir hosted.rb and CRuby 4.0.9 (checked by hand)
CompilerTest(code) correct lines, then :0:in 'CompilerTest' ×3 … RunTestCase … Main nothing below <main>

Item 3: line 0 and the def line

A source without a path, such as ScriptEngine.Execute(string) or an interactive line, has no SourceUnit.Document. Without a document, AstGenerator.AddDebugInfo emits no sequence points, so that code had no line information at all:

  • a compiled method fell back to the line in its encoded name, which is the def line;
  • top-level frames, and the library frames reported at their line (Array#each), got line 0.

RubyContext.TransformTree now gives such a source a SymbolDocument named (eval), the name its frames already carried (AstGenerator.SourcePath). A side effect: Method#source_location of a method defined there is now ["(eval)", line] instead of nil. CRuby gives ["(eval at …)", line] for eval'd code.

Item 4: CLR host frames below <main>

RubyStackTraceBuilder remembers where the outermost frame that is not a visible CLR frame ends. RubyTrace drops the CLR frames after it: these are the frames of the host that ran the Ruby code. This applies to exception backtraces, caller and caller_locations. A skipped Ruby frame counts, so caller at the top level of hosted code returns [], as in CRuby, not the host's frames.

  • A CLR method between Ruby frames, which called back into Ruby, is still reported (Backtrace4).
  • -X:ExceptionDetail keeps every frame.
  • ir is unaffected, because its own frames were never reported.

Items 1 and 2: why there is no backtrace fix

Under CompilerTest in a fresh runtime, e is not the ArgumentError. It is NameError: undefined local variable or method 'many' for an instance of Foo, raised in Foo#bar before many runs. So no frame is missing.

CompilerTest runs the code with ScriptCode.Run(new Scope()). That bound hosted scope deliberately defines top-level defs as singleton methods of the scope's main object (RubyTopLevelScope.CreateHostedTopLevelScope, MainSingleton). This lets the host read them from the scope (scope.GetVariable<Func<int>>("z"), see RubyHosting1A) and keeps two scopes' methods apart. Methods on main's singleton cannot be called from an instance of another class.

The stale file in item 2 comes from the same cause. A later CompilerTest run reopens Foo and calls the Object#many that an earlier unbound Engine.Execute or ScriptSource.Execute() defined, because those run with ScriptCode.Run(). So (eval) is the correct file for the method that actually ran. Only its line was wrong (item 3), and that is fixed.

Making these methods behave as CRuby's (private methods of Object) would change the hosting API's scope isolation. That is a design decision, not a backtrace fix, so this PR does not make it.

Tests

New tests in IronRuby.Tests:

  • Backtrace_HostedPathlessLines1: the full Engine.Execute backtrace, the CompilerTest-after-Engine.Execute case from item 2, and source_location and caller(0) in path-less code.
  • Backtrace_HostedNoHostFrames1: the full hosted.rb backtrace, caller/caller(0)/e.backtrace under CompilerTest, and a method the host calls through a scope delegate. That method gets one frame from caller(0) and none from caller(1).

Results:

  • Both new tests fail on master (6 assertions). They pass with /debug, /interpret, /sync1 and /noadaptive.
  • Full IronRuby.Tests, Debug: PASSED on net8.0 and on net10.0.
  • ruby/spec with ir:
    • core/exception: 250 examples, 0 failures
    • core/kernel/caller*: 38 examples, 0 failures
    • core/thread/backtrace*: 101 examples, 0 failures
    • language: 2935 examples, 0 failures and 1 error. The error is the send_spec "into"/"to" wording error that is already on master.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Backtraces for code without a file now show (eval) as the source, while hosted code retains its named-file locations.
    • Backtraces now omit host runtime frames below the Ruby call stack, while preserving relevant frames between Ruby calls.
    • Added coverage for source locations, callers, and hosted backtrace behavior.

…elow <main> (#163)

- A source without a path (ScriptEngine.Execute(string), an interactive line) had no
  SymbolDocument, so the compiler emitted no line information for it: a method's frame
  was reported at its `def' line and top-level frames (and Array#each called from them)
  at line 0. It now gets a document named "(eval)", the name its frames already carried.
- The CLR frames of the host below the outermost Ruby frame (CompilerTest, RunTestCase,
  Main, ...) are no longer reported in backtraces or caller; CRuby has nothing below
  <main>. A CLR frame between Ruby frames is still reported, and -X:ExceptionDetail
  keeps everything.

The "missing Object#many frame" under CompilerTest is not a backtrace bug: a top-level
def run in a host's scope (ScriptCode.Run(Scope)) defines a singleton method of that
scope's main object, so Foo#bar raises NameError before `many' runs; a later run then
finds the Object#many an earlier unbound Engine.Execute defined, which is the stale file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Pathless source units now receive an (eval) source document. Backtrace construction tracks a Ruby trace boundary and truncates host CLR frames when exception detail is disabled. New tests cover pathless source locations, line numbers, and hosted backtraces.

Changes: Hosted backtraces

Layer / File(s) Summary
Pathless source documents and tests
Src/Ruby/Compiler/AstGenerator.cs, Src/Ruby/Runtime/RubyContext.cs, Src/IronRuby.Tests/Runtime/BacktraceTests.cs
Pathless source units use an (eval) symbol document. Tests check pathless line numbers and source locations, and named file lines for compiler-hosted code.
Host CLR frame boundary and tests
Src/Ruby/Runtime/RubyStackTraceBuilder.cs, Src/IronRuby.Tests/Runtime/BacktraceTests.cs, Src/IronRuby.Tests/RubyTests.cs
The stack trace builder tracks visible CLR frames and truncates the trace at the Ruby boundary when exception detail is disabled. Registered tests check that host CLR frames are omitted from hosted backtraces.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

  • Issue 163: The changes address the described pathless line-number and host CLR frame behavior. The issue summary says other items remain unchanged.

Possibly related PRs

  • Largo/ironruby#22: It changes visible CLR frame classification and backtrace construction, which this change also uses.

Suggested reviewers: claude, sholden

Merge Risk: Minimal · up to ce273

No actionable merge-blocking issue was identified in the pathless-source or hosted-backtrace changes. Merge after normal checks.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two main aspects of the changeset: line information for path-less (eval) code in backtraces, and removal of host CLR frames below . The changeset adds the PathlessSourceName …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two main aspects of the changeset: line information for path-less (eval) code in backtraces, and removal of host CLR frames below <main>. The changeset adds the PathlessSourceName constant and a GetSourceDocument fallback in RubyContext, and updates RubyStackTraceBuilder to drop host CLR frames below the Ruby boundary. It also adds the BacktraceTests and registers them in RubyTests. The title is specific and matches the main change. The '(part of #163)' suffix is accurate per the objectives, which describe PR #169 addressing items of issue #163.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9170dd0 and ce2733a.

Files selected for processing: 5

  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/BacktraceTests.cs
  • Src/Ruby/Compiler/AstGenerator.cs
  • Src/Ruby/Runtime/RubyContext.cs
  • Src/Ruby/Runtime/RubyStackTraceBuilder.cs

Included review availability: This review used your included allowance. 0 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

@Largo

Largo commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@Largo
Largo merged commit e815798 into master Nov 9, 2026
14 of 15 checks passed
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.

1 participant