Repository navigation
A DLL whose extension methods couldn't be read can be required again (rebased) - #64
Merged
Merged
Conversation
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]>
…le next to it is no answer $LOAD_PATH was the one place ResolveAssembly still matched by simple name only, and it loaded the file before checking even that. It came before the DLL's own directory, so an older Dep.dll on $LOAD_PATH answered a reference to a newer Dep sitting next to the DLL, which then failed later with a MissingMethodException. It now checks the file's identity with Satisfies before loading it, as the other two places do. Next to the DLL, every failure turned into a LoadError thrown out of the AssemblyResolve handler, which failed the type load of the DLL that asked: - A file there that isn't an assembly (a native DLL of the same name) or can't be read now leaves the reference unresolved, as if nothing were there. - A dependency that loads but whose extension methods can't be read (they name a type of a missing assembly; read as it loads once any `using' of a CLR namespace has run) is reported with a warning and still answers the reference. 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]>
|
Warning Review limit reachedYou'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. View limit details
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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#54 rebased onto master, which now has #50 and #56. The original PR came from @sholden's branch, which still sits on the pre-rebase #50 and conflicts with master. Commits, authorship kept:
Stacked on #63 (the #53 replacement), so until that merges this diff shows its commits too. No changes in the rebase.
Checked together with #55, all of them merged onto master 62655f8 (Debug, net8.0, Linux):
regen-initializers.shleavesInitializers.Generated.csunchanged; 0 build warnings; IronRuby.Tests 0 failures;spec/language2934 examples, 0 failures;spec/coreexception, mutex, conditionvariable, refinement,Module#refine/#using,main.usingandKernel#requireall 0 failures; the 101 LINQ samples print exactly101samples.expected;Util/error-message-matrix.rb,refinement-binding-matrix.rb,jit-matrix.rbandcondvar-matrix.rbmatch CRuby 4.0.6 line for line, andrefinement-matrix.rbdiffers only in object addresses.The description below is from #54 (sholden's).
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".
A worse variant shows up when the DLL is required before any extension methods are asked for. That
requireonly queues the DLL and succeeds. The firstusing_clr_extensionsorusingreads the queue and raises from that line, whatever namespace it names: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.AddExtensionAssemblyrecords an assembly as handled before reading it, and kept the record when reading failed.On a retried
require, the DLR'sScriptDomainManager.LoadAssemblydoesn't report an assembly it has already seen. Even if it did,RubyContextwould have skipped the assembly as handled. For the queue,RubyContextempties the queue before reading it, and one exception ended the loop.What changed
requireloads 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 returnstrue.usingline no longer raises. The warning names the DLL and what's missing:usingof its namespace or the nextrequireof it.requirewhose extension methods can't be read still raisesLoadError, as before. Only theusingpath changed from raising to warning. Raising there broke ausingof an unrelated namespace, and the queue was already lost by then.The DLL itself can't be unloaded, so after a failed
requireits namespaces stay visible. That was already the case.Tests
Require_UnreadableExtensions1makes arequirefail, loads the missing dependency, and retries. The retry returnstrueand the extension method works.Require_UnreadableExtensions2queues 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.shout, and the second raisesFileNotFoundExceptionfrom the activation.Results:
spec/languagepasses with 2935 examples and 0 failures. TheKernel#require,require_relative,loadandautoloadspecs give 213 examples, 0 failures and the same 2 pre-existing errors as DLLs required by path find the DLLs they reference, loaded or alongside #53 lists.requirenow matches a run where it never failed, andusing System::Linqafter an unreadable Demo2 warns instead of raising, through bothusingandusing_clr_extensions. req3.rb, req4.rb and later.rb print what they did under DLLs required by path find the DLLs they reference, loaded or alongside #53.ActivateExtensionsrather thanLoadExtensions, and git placed the hunk there. I built that in a scratch worktree against a DLR with all ofBuild/dlr-patches, 0102 included, and IronRuby.Tests passes with 0 failures, the two new tests included. I removed the worktree afterwards.CI passed all seven checks. IronRuby.Tests reported 0 failure lines on ubuntu and windows with both net8.0 and net10.0, and
spec/languagepassed with 2935 examples and 0 failures on both.Stack
using_clr_extensionsusing System::Linq: CLR extension methods scoped like C#'s using directive #50 scopes CLR extension methods withusingLoader.LoadFromPathit 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