Skip to content

A DLL whose extension methods couldn't be read can be required again - #54

Closed
Largo wants to merge 9 commits into
Largo:masterfrom
sholden:fix/extension-scan-retry
Closed

Largo wants to merge 9 commits into
Largo:masterfrom
sholden:fix/extension-scan-retry

Conversation

@Largo

@Largo Largo commented Oct 29, 2026

Copy link
Copy Markdown
Owner

Opened upstream from @sholden's branch sholden:fix/extension-scan-retry, unchanged; this is their fork PR sholden#11. It is stacked on #53, which is stacked on #50, so until those merge this diff also shows their commits. The commit this PR adds:

  • 94388b3 A DLL whose extension methods couldn't be read can be required again

The description below is sholden's. Bare PR numbers that were fork-local now point at sholden/ironruby, or at the upstream PR where one exists.


A DLL whose extension methods couldn't be read stayed that way for the rest of the process. #53 lists this under "Found, not fixed".

using_clr_extensions Demo
require '/x/Demo2.dll'   # LoadError: Demo, which Demo2 is built against, can't be found
require '/y/Demo.dll'
require '/x/Demo2.dll'   # true, but Demo2's extension methods are never read
w.shout                  # NoMethodError, though Demo::LaterExtensions.Shout(w) works

A worse variant shows up when the DLL is required before any extension methods are asked for. That require only queues the DLL and succeeds. The first using_clr_extensions or using reads the queue and raises from that line, whatever namespace it names:

require '/x/Demo2.dll'   # fine
using System::Linq       # Errno::ENOENT: Could not load file or assembly 'Demo, ...'

Every assembly queued after Demo2 also lost its extension methods for good. master has both problems. #53 makes them rarer, because a DLL now fails this way only when nothing satisfies its reference. It still happens when a dependency is really missing.

Cause

Reading an assembly's extension methods loads the types in their signatures, so it fails when one of them lives in an assembly that can't be found. RubyContext.AddExtensionAssembly records an assembly as handled before reading it, and kept the record when reading failed.

On a retried require, the DLR's ScriptDomainManager.LoadAssembly doesn't report an assembly it has already seen. Even if it did, RubyContext would have skipped the assembly as handled. For the queue, RubyContext empties the queue before reading it, and one exception ended the loop.

What changed

  • An assembly whose extension methods can't be read is taken back out of the handled set, so the next attempt reads it again.
  • When require loads a DLL the runtime has already seen, it asks the context for any reading still owed. The retry above reads Demo2's extension methods and returns true.
  • Reading the queue, or the assemblies of a namespace being activated, warns about an assembly it can't read and carries on with the rest. The using line no longer raises. The warning names the DLL and what's missing:
    t.rb:3: warning: extension methods of Demo2 not read: Could not load file or assembly 'Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
    
    That assembly is read again at the next using of its namespace or the next require of it.
  • A require whose extension methods can't be read still raises LoadError, as before. Only the using path changed from raising to warning. Raising there broke a using of an unrelated namespace, and the queue was already lost by then.
  • The constant version is now bumped even when reading fails, because the assembly's namespaces are loaded either way.

The DLL itself can't be unloaded, so after a failed require its namespaces stay visible. That was already the case.

Tests

  • Require_UnreadableExtensions1 makes a require fail, loads the missing dependency, and retries. The retry returns true and the extension method works.
  • Require_UnreadableExtensions2 queues an unreadable DLL ahead of a good one, then activates their namespace. It expects one warning, the good DLL's method working, and a second warning, not an exception, when the namespace is activated again.
  • Both fail without the change. The first gets NoMethodError for shout, and the second raises FileNotFoundException from the activation.
  • The tests use DLLs required by path find the DLLs they reference, loaded or alongside #53's fixture writer, which can now name the extension method.

Results:

CI passed all seven checks. IronRuby.Tests reported 0 failure lines on ubuntu and windows with both net8.0 and net10.0, and spec/language passed with 2935 examples and 0 failures on both.

Stack

  1. CLR extension methods reach a method group used before using_clr_extensions #43, merged, fixes stale CLR overloads after using_clr_extensions
  2. Refinements: only refined calls pay, and the JIT and OSR keep working #49 makes refinements cost nothing where they can't change a call
  3. using System::Linq: CLR extension methods scoped like C#'s using directive #50 scopes CLR extension methods with using
  4. DLLs required by path find the DLLs they reference, loaded or alongside #53 resolves the DLLs a DLL required by path references
  5. This PR, stacked on DLLs required by path find the DLLs they reference, loaded or alongside #53. It needs DLLs required by path find the DLLs they reference, loaded or alongside #53's commit, for the line in Loader.LoadFromPath it changes and for the test fixtures. With DLLs required by path find the DLLs they reference, loaded or alongside #53's commit it also applies to master.

🤖 Generated with Claude Code

sholden and others added 9 commits October 29, 2026 08:57
…ective

using_clr_extensions activates a namespace's extension methods for every call
in the runtime: a gem calling it changes what `list.select' means in every
other file. C# scopes them to the files that say `using System.Linq;', and
Ruby has the same notion for refinements, so `using' now accepts a CLR
namespace and brings its extension methods into the rest of the file, class
body or eval string, exactly where a refinement would be active:

  using System::Linq
  list.where { it > 1 }.order_by { -it }.to_a

`using System::Linq::Enumerable' brings in one static class's extension
methods, as C#'s `using static' does.

Extension methods come last, as in C#, rather than at the extended type's
place in the ancestors: a call that finds a Ruby method calls it
(list.select is Enumerable#select), one that finds a CLR method group gets them
as more overloads (list.count(pred) next to List#Count; an exact tie still goes
to the instance method), and one that finds nothing gets them alone
(list.where). The CLR-cased name (list.Select) still reaches them.

Built on the refinement machinery: a CLR namespace in a scope's `using' list is
part of its RefinementActivation, so methods defined there record it, call
sites guard on it, and send/public_send/method/respond_to? and irb see it. The
namespace's extension methods are filed with the types they extend (including
those of assemblies loaded later) under their CLR and Ruby names, and those
names join the per-(module, name) index stage one introduced - only calls of
those names on those types take the lexical guard. The group a call sees is
cached per class version, name and activation.

A program that `using's System::Linq pays nothing where it makes no LINQ call;
a hot LINQ call costs ~12% more than through using_clr_extensions, for the
guard. using_clr_extensions keeps working as before and now warns, once per
process, that it is deprecated.

ClrScopedExtensions1-4 cover the lexical scope, extension methods coming last
(Ruby method, merged CLR group, extension alone; Widget/Square/Box fixtures),
`using static', and a call bound before the first `using' of the namespace.
The tests of using_clr_extensions expect its one warning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… only lexically

The scoped form put extension methods last, behind every Ruby method, so
`using System::Linq' turned list.select from LINQ's Select (what
using_clr_extensions gives) into Enumerable#select, and code moving from one
form to the other had to rename its calls to their CLR names. The scoped form is
meant to be using_clr_extensions confined to a file, class body or eval string,
so it now resolves the same way.

An extension method sits where a refinement of the type it extends would, and a
class's place also has those of the interfaces it implements, as
using_clr_extensions puts both in the class's own CLR method group:

  using System::Linq
  list.select { it * 2 }.to_a   # LINQ's Select, ahead of Enumerable#select
  dictionary.select { ... }     # LINQ's too, ahead of the IDictionary one
  [3, 1].select { it > 1 }      # Array#select: Array's ancestors have no IEnumerable[T]
  [3, 1].Select { ... }         # NoMethodError, as before

A CLR method group anywhere still gets them as more overloads, the instance member
winning an exact tie (list.count(pred), list.reverse). A Ruby method defined
ahead of them wins, and a site bound to them re-binds when one is defined or
removed. A singleton class sees them, as respond_to? and method did under
using_clr_extensions.

Across 13 receivers (List, Integer[], HashSet, Dictionary, an OfType iterator,
a List with a singleton class, Array, Hash, Range, String, Object, ...) and 30
calls, the two forms now print the same, except that a Ruby class including
IEnumerable[Object] gets LINQ through `using' and doesn't through
using_clr_extensions. 101samples.rb prints the same under both with only its
using line changed.

ClrScopedExtensions2 expects LINQ's select; ClrScopedExtensions5 covers Array,
Dictionary, a singleton, Enumerable#where behind LINQ's on a List and ahead of
nothing on an Array, and a Ruby select defined and removed on List[Integer].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sions has them

using_clr_extensions adds extension methods to the CLR method groups of classes
backed by a CLR type, its own and those of its interfaces, and nowhere else. The
scoped form also looked at interface modules themselves, so a Ruby class that
includes IEnumerable[Object] got LINQ through `using' and nothing through
using_clr_extensions. Now only a CLR class's place counts, when deciding whether
extensions shadow a Ruby method and when gathering them into a group.

Against master (14d53a5), over 20 receivers - CLR collections, a List with a
singleton class, Ruby subclasses of List with and without their own select,
class objects, Array, Hash, Range, String, Struct, Set, Enumerator - and 30
calls each: `using System::Linq' prints what master's using_clr_extensions
does, and a program that `using's it in one module prints, outside that
module, what master prints with no activation. using_clr_extensions and no
activation print what they print on master.

ClrScopedExtensions5 also checks the Ruby class including IEnumerable[Object].

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Src/Samples/Linq/101samples.rb stopped parsing when Ruby stopped accepting a
trailing comma in a nested block parameter, |(c,), p|, and nothing ran it
after that. Past the parse error it raised a NameError at FirstOrDefault,
which names Fixnum, gone since Ruby 3.2.

- |(c,), p| is |(c, _), p|.
- Fixnum is Integer, which is System::Int32 in IronRuby.
- Min: Projection called sum, copied from Sum: Projection, and printed the
  total length of the words instead of the shortest. It calls min now,
  through method(:min).of(Object, Integer) as Min: Grouped does. A
  Func<Object, int> fits both Min(Func<TSource, int>) and
  Min<TSource, TResult>(Func<TSource, TResult>), and IronRuby can't choose
  between them where C# picks the first.
- Repeat printed its 7s without a newline, so the next heading ran on.
- OfType was commented out as an IronRuby bug. It works now.

The file still calls using_clr_extensions. Its output is the reference for
moving it to `using'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The samples were the last code outside the tests that called
using_clr_extensions, so they printed its deprecation warning. They say
`using System::Linq' now. Inside the scope calls resolve as they did under
using_clr_extensions, so every call keeps its name, and the output is the
same apart from the warning.

to_seq, which turns the samples' arrays into the IEnumerable<T> LINQ
extends, was a method added to Object for the whole program. It is a
refinement of Object now, active below the `using' like the LINQ methods.
Nothing in the file calls it before that point.

A probe at each of the 119 call sites with a LINQ method's name saw all of
them run: 112 reach a System.Linq.Enumerable method, and the other 7 are
Array#join on what to_a returned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Nothing ran Src/Samples/Linq/101samples.rb, so nobody noticed when it stopped
parsing. build-and-test now runs it on each OS and target framework, after the
socket check, and diffs its stdout and stderr against
Src/Samples/Linq/101samples.expected, which holds this branch's output on
Linux and net10.0.

A wrong answer, an exception or a warning fails the step. Tried locally by
putting using_clr_extensions back, which adds its deprecation warning; by
changing count(pred) to where(pred); by raising in the middle; and by
dropping `using LinqSampleHelpers', which makes to_seq undefined. The sample
takes about 1.6 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The samples passed every LINQ delegate as lambda { ... }. A block goes to a
method's trailing delegate parameter, so a call whose last argument was a
lambda now takes a block:
numbers.where { |n| n < 5 }, group_by(->(p) { p.category }) { |c, g| ... },
aggregate(start_balance) { |balance, next_withdrawal| ... }. A lambda that
isn't the last argument, such as a key selector before a result selector,
and the make_pair and identity helpers are -> lambdas now.

Two indexed samples keep a lambda. On .NET 9 and later OfType returns one
of LINQ's private iterator classes, whose own public Where(Func<T, bool>)
and Select(Func<T, R>) IronRuby binds to. A block with two parameters fits
those too, because a proc takes any number of arguments, so
digits.where { |digit, index| ... } got nil for index and
numbers.select { |num, index| ... } was ambiguous. A lambda's arity rules
them out. The indexed select_many, take_while and skip_while have no such
methods to compete with and take blocks. Each of the two has a comment.

The output is unchanged, so 101samples.expected is too. A probe at each of
the 119 call sites with a LINQ method's name still sees all of them reach
the same methods as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
require loads a DLL by its path with Assembly.LoadFile, which on .NET Core
gives each file a load context of its own, where a reference by name finds
the application's assemblies and no other. A reference from one such DLL to
another fell to IronRuby's AssemblyResolve handler, which only searched
$LOAD_PATH. So two DLLs required by their paths, the second built against
the first, broke as soon as anything read the second's signatures:

  require '/x/Demo.dll'
  using_clr_extensions Demo
  require '/x/Demo2.dll'   # LoadError: Could not load file or assembly
                           # 'Demo, Version=1.0.0.0, ...'

Active extension methods are read as each assembly loads, so there the
require raised. Without them, the first call of a Demo2 method taking a Demo
type raised Errno::ENOENT instead. And when $LOAD_PATH held another file of
Demo, the handler loaded it as a second Demo, whose Widget isn't the one
Ruby made: Demo2's methods wouldn't take Ruby's Widgets.

The handler now tries three places in turn:

- The assemblies the runtime has loaded. One answers if it has the simple
  name and culture asked for, at least the version asked for (the rule .NET's
  binder applies to an application's assemblies), and the public key token if
  the reference names one.
- $LOAD_PATH, as before, with its name-only check.
- The directory of the DLL that asked, if the runtime loaded it from a file,
  roughly what .NET Framework's LoadFrom context did. A file there is loaded
  only if its identity will do, and becomes the runtime's as a $LOAD_PATH
  find does.

AssemblyResolve is the process's event, and every runtime in a process is
asked, in the order they hooked it. A runtime now leaves a reference made by
a DLL that another runtime loaded from a file to that runtime. Before, the
first runtime whose $LOAD_PATH held a copy of the dependency answered for
all of them, with its copy. IronRuby.Tests, which runs each test in a new
runtime of one process, is such a process: without this, the new tests
answer each other's references.

Require_PathLoadedDependency1-6 cover the reported case, the directory,
copies on $LOAD_PATH and next to the DLL that must not be loaded again,
versions, $LOAD_PATH before the directory, and a second runtime with copies
of its own. The fixture DLLs are written at test time with
System.Reflection.Metadata; PersistedAssemblyBuilder would need .NET 9.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reading an assembly's extension methods loads the types in their
signatures, and fails if one lives in an assembly that can't be found.
RubyContext recorded the assembly as handled before reading it, and kept the
record when reading failed, so nothing ever tried again:

  using_clr_extensions Demo
  require '/x/Demo2.dll'   # LoadError: Demo can't be found
  require '/y/Demo.dll'
  require '/x/Demo2.dll'   # true, but Demo2's extension methods never read

The failed require left Demo2 out of $LOADED_FEATURES, as it should, but
the retry found the runtime had seen the assembly. The DLR's
ScriptDomainManager reports an assembly once, and RubyContext skipped it.

A DLL required before any extension methods were asked for was only queued,
and its require succeeded. The first using_clr_extensions or `using' read
the queue, raised the reading error from that line, whatever its namespace,
and had already emptied the queue: every assembly after the bad one lost
its extension methods for good.

Now an assembly whose extension methods can't be read is forgotten, so the
next attempt reads it again. A require of a DLL the runtime has seen asks
the context for any reading still owed, so the retry above reads Demo2's
extension methods and returns true. Reading the queue, or the assemblies of
a namespace being activated, warns about an assembly it can't read and goes
on with the rest:

  warning: extension methods of Demo2 not read: Could not load file or
  assembly 'Demo, Version=1.0.0.0, ...'

That assembly is read again at the next activation of its namespace or
require of it. The constant version is bumped even when reading fails, as
the assembly's namespaces are loaded either way.

Require_UnreadableExtensions1 retries a require that failed, and
Require_UnreadableExtensions2 queues an unreadable DLL ahead of a good one
and activates their namespace twice. Both fail without the change. The
fixture writer can now name the extension method.

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

coderabbitai Bot commented Oct 29, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 334faf1c-6ef5-4d99-b3e8-7e395ba265e9
Commits

Reviewing files that changed from the base of the PR and between 3cc808e and 94388b3.

Files selected for processing: 17
  • .github/workflows/ci.yml
  • README.md
  • Src/IronRuby.Tests/Helpers.cs
  • Src/IronRuby.Tests/RubyTests.cs
  • Src/IronRuby.Tests/Runtime/ClrBinderTests.cs
  • Src/IronRuby.Tests/Runtime/ClrRuntimeInteropTests.cs
  • Src/IronRuby.Tests/Runtime/ClrTests.cs
  • Src/IronRuby.Tests/Runtime/ClrTests_Globals.cs
  • Src/IronRuby.Tests/Runtime/LoaderTests.cs
  • Src/Libraries/Builtins/KernelOps.cs
  • Src/Libraries/Builtins/ModuleOps.cs
  • Src/Ruby/Builtins/RubyModule.cs
  • Src/Ruby/Runtime/Loader.cs
  • Src/Ruby/Runtime/RefinementActivation.cs
  • Src/Ruby/Runtime/RubyContext.cs
  • Src/Samples/Linq/101samples.expected
  • Src/Samples/Linq/101samples.rb
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Largo

Largo commented Oct 29, 2026

Copy link
Copy Markdown
Owner Author

Landed as #64: the same commits rebased onto master (which now has #50 and #56), still authored by @sholden, and merged. Thanks!

@Largo Largo closed this Oct 29, 2026
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.

2 participants