fix: Faker::Config.lazy_loading configuration must be respected (WIP)#3256
Open
thdaraujo wants to merge 5 commits into
Open
fix: Faker::Config.lazy_loading configuration must be respected (WIP)#3256thdaraujo wants to merge 5 commits into
Faker::Config.lazy_loading configuration must be respected (WIP)#3256thdaraujo wants to merge 5 commits into
Conversation
thdaraujo
commented
May 1, 2026
Contributor
Author
|
testing script require_relative "../faker/lib/faker"
lazy_loading = if ENV.key?('FAKER_LAZY_LOAD')
puts 'set via env...'
ENV['FAKER_LAZY_LOAD'] == '1'
else
puts 'set via config...'
Faker::Config.lazy_loading = true
end
puts "Number of constants: #{Faker.constants.size}"
puts "Faker::Name #{Faker::Name.name}"
puts "Number of constants: #{Faker.constants.size}"
if lazy_loading
puts "faker is lazy loading..."
if Faker.constants.size > 10
raise "must be lazy loaded!"
else
"lazy loading success!"
end
else
puts "faker is NOT lazy loading..."
if Faker.constants.size < 10
raise "must be eager loaded!"
else
"eager loading success!"
end
endCall with env or config: FAKER_LAZY_LOAD=0 ruby script.rb
FAKER_LAZY_LOAD=1 ruby script.rb
# change `lazy_loading` config on the file itself, then run
ruby script.rb |
f6c2113 to
471afd9
Compare
thdaraujo
commented
May 1, 2026
| private_constant :EAGER_LOAD_MUTEX | ||
|
|
||
| # initial usage determines lazy loading or eager loading | ||
| # TODO: this can be a bit surprising and error-prone |
Contributor
Author
There was a problem hiding this comment.
this sets up loading the first time a generator is called, which could be a bit surprising/unexpected.
maybe it's a good enough solution for letting folks test it?
in the future, we aim to make lazy loading default, so this wouldn't be a problem anymore, in my view.
4003e8f to
8dda593
Compare
e5c4aa3 to
9014cb4
Compare
This fix initializes lazy loading or eager loading after the first time a generator is called, which can be a bit surprising. But this guarantees that the `Faker::Config.lazy_loading` setting is respected.
Fix this failure when lazy loading: https://github.com/faker-ruby/faker/actions/runs/25201119276/job/73892235147?pr=3256
9014cb4 to
63fd229
Compare
thdaraujo
commented
Jun 1, 2026
| end | ||
|
|
||
| mutex.synchronize { loaded_files << f } | ||
| end |
Contributor
Author
There was a problem hiding this comment.
stub Kernel#require to spy on files being required/loaded
Contributor
Author
There was a problem hiding this comment.
maybe I could pass require or Kernel via dependency injection to make it easier to test :/
It would also get rid of the warnings.
Could be a future improvement!
63fd229 to
dd9026d
Compare
dd9026d to
f1db978
Compare
Contributor
|
I tested on a Ruby project, and some generators raise this error: |
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.
(fixes #3248 )
This fix initializes lazy loading or eager loading after the first constant access, which can be a bit surprising.
But this guarantees that the
Faker::Config.lazy_loadingsetting is respected.We introduce a new
Faker::Loaderclass responsible for taking care of the loading process.The loader has a method
Loader#install_onthat adds lazy loading to a generator. It definesconst_missingon namespace classes (Faker::Games, Faker::Music etc.), delegating back to the Loader, which resolves the constant either by requiring the single corresponding file (lazy) or by loading all generator files at once (eager).The loading strategy (lazy vs eager) is snapshotted on the first
const_missingcall. Changing config after first use has no effect.The loader also has a list of inflections to handle cases such as
DnD -> dnd.rb.Config.lazy_loadingis now process-wide rather than thread-local, so it cannot be confired per-thread anymore (which didn't seem very useful to begin with, so we're simplifying this).--
The main source of the problem is due to Faker having configuration set directly, which makes it hard to defer evaluation of the configuration before requiring the Faker library.
Other possible alternative could be:
lazy_loadingconfig is first set to true