Skip to content

Commit 60ed221

Browse files
authored
Merge pull request #2766 from ViewComponent/joelhawksley-temporary-module-names
Skip temporarily named modules when computing cache digests
2 parents 74c80ad + 5cfb262 commit 60ed221

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎docs/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Avoid `NameError` during eager loading when components include modules with temporary names.
14+
15+
*Joel Hawksley*
16+
1317
* Avoid repeated scanning of unterminated ERB tags during cache dependency discovery.
1418

1519
*Joel Hawksley*

‎lib/view_component/cache_digest/resolver.rb‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ def module_files(component)
145145
component.ancestors.take_while { |ancestor| ancestor != ViewComponent::Base }.filter_map { |ancestor|
146146
next if ancestor.is_a?(Class) || ancestor.name.nil?
147147

148-
path, _line = Object.const_source_location(ancestor.name)
148+
begin
149+
path, _line = Object.const_source_location(ancestor.name)
150+
rescue NameError
151+
next
152+
end
149153
path unless path.nil? || path.start_with?(VIEW_COMPONENT_LIB)
150154
}.uniq
151155
end

‎test/sandbox/test/cacheable_test.rb‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,21 @@ def test_cache_digest_changes_when_a_component_an_included_module_renders_change
447447
) { CacheableHelperComponent.cache_digest }
448448
end
449449

450+
def test_modules_with_temporary_names_are_not_digested
451+
skip "Module#set_temporary_name requires Ruby 3.3+" unless Module.method_defined?(:set_temporary_name)
452+
453+
component = CacheableHelperComponent.dup
454+
resolver = ViewComponent::CacheDigest::Resolver.instance
455+
source = resolver.source_for(component)
456+
mod = Module.new
457+
component.include(mod)
458+
459+
["ViewComponent::Properties(Extension)", "MissingNamespace::Properties(Extension)"].each do |name|
460+
mod.set_temporary_name(name)
461+
assert_equal source, resolver.source_for(component)
462+
end
463+
end
464+
450465
# Every component includes modules from ViewComponent itself, which would
451466
# otherwise tie every digest to the installed version of the gem.
452467
def test_modules_view_component_includes_are_not_digested

0 commit comments

Comments
 (0)