Remove autoload for constant if the autoload fails#4715
Merged
jeremyevans merged 4 commits intoOct 8, 2021
Merged
Conversation
Previously, if an autoload failed (the file was loaded, but the constant was not defined by the autoloaded file). Ruby will try to autoload again if you delete the autoloaded file from $LOADED_FEATURES. With this change, the autoload is removed as soon as it fails. To handle cases where multiple threads are autoloading, when deleting an autoload, handle the case where another thread already deleted it. Fixes [Bug ruby#15790]
Contributor
|
👍 |
Member
|
So this removes the autoload but not the constant? That seems rather unexpected, I think removing the constant would be cleaner. |
Member
|
NOTE: this PR was updated to actually remove the constant (and by that its associated autoload), that's I think much better, thanks 👍 |
fxn
added a commit
to fxn/zeitwerk
that referenced
this pull request
Oct 14, 2021
Before Ruby 3.1, if the file require'd by Module#autoload does not define the expected constant, parent.constants still contains said constant, and you have to manually remove_const to fully unload everything. This changed with ruby/ruby#4715. Starting with Ruby 3.1, parent.constants no longer includes the constant, and remove_const raises NameError. This patch allows Zeitwerk to work correctly in both cases without conditionals.
jamieblynn
added a commit
to jamieblynn/zeitwerk
that referenced
this pull request
Jan 29, 2022
Before Ruby 3.1, if the file require'd by Module#autoload does not define the expected constant, parent.constants still contains said constant, and you have to manually remove_const to fully unload everything. This changed with ruby/ruby#4715. Starting with Ruby 3.1, parent.constants no longer includes the constant, and remove_const raises NameError. This patch allows Zeitwerk to work correctly in both cases without conditionals.
connorchris831
added a commit
to connorchris831/zeitwerk
that referenced
this pull request
Jan 5, 2023
Before Ruby 3.1, if the file require'd by Module#autoload does not define the expected constant, parent.constants still contains said constant, and you have to manually remove_const to fully unload everything. This changed with ruby/ruby#4715. Starting with Ruby 3.1, parent.constants no longer includes the constant, and remove_const raises NameError. This patch allows Zeitwerk to work correctly in both cases without conditionals.
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.
Previously, if an autoload failed (the file was loaded, but the
constant was not defined by the autoloaded file). Ruby will try
to autoload again if you delete the autoloaded file from
$LOADED_FEATURES. With this change, the autoload is removed as
soon as it fails.
To handle cases where multiple threads are autoloading, when
deleting an autoload, handle the case where another thread
already deleted it.
Fixes [Bug #15790]