Skip to content

Commit 194711a

Browse files
authored
fix: collect imports from components in props in _get_all_imports() (#6317)
Fixes #6312 _get_all_imports() only walked self.children to collect transitive imports. Components embedded in props (e.g. a fallback: Component prop) were skipped, causing missing JS imports and ReferenceError at runtime. The sibling methods _get_all_custom_code() and _get_all_dynamic_imports() already call self._get_components_in_props() for this case. This brings _get_all_imports() in line with them. Co-authored-by: lawrence3699 <lawrence3699@users.noreply.github.com>
1 parent c979b46 commit 194711a

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

packages/reflex-base/src/reflex_base/components/component.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,12 @@ def _get_all_imports(self, collapse: bool = False) -> ParsedImportDict:
17501750
The import dict with the required imports.
17511751
"""
17521752
imports_ = imports.merge_parsed_imports(
1753-
self._get_imports(), *[child._get_all_imports() for child in self.children]
1753+
self._get_imports(),
1754+
*[child._get_all_imports() for child in self.children],
1755+
*[
1756+
component._get_all_imports()
1757+
for component in self._get_components_in_props()
1758+
],
17541759
)
17551760
return imports.collapse_imports(imports_) if collapse else imports_
17561761

tests/units/components/test_component.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,32 @@ def test_get_imports(component1, component2):
522522
}
523523

524524

525+
def test_get_all_imports_includes_components_in_props():
526+
"""Test that _get_all_imports collects imports from components in props."""
527+
528+
class InnerComponent(Component):
529+
"""A component that requires a specific import."""
530+
531+
def _get_imports(self) -> ParsedImportDict:
532+
return {"some-library": [ImportVar(tag="SomeTag")]}
533+
534+
class OuterComponent(Component):
535+
"""A component with a component-typed prop."""
536+
537+
fallback: Component | None = None
538+
539+
def _get_imports(self) -> ParsedImportDict:
540+
return {"outer-library": [ImportVar(tag="OuterTag")]}
541+
542+
inner = InnerComponent.create()
543+
outer = OuterComponent.create(fallback=inner)
544+
all_imports = outer._get_all_imports()
545+
assert "some-library" in all_imports, (
546+
"_get_all_imports() should collect imports from components in props"
547+
)
548+
assert "outer-library" in all_imports
549+
550+
525551
def test_get_custom_code(component1: Component, component2: Component):
526552
"""Test getting the custom code of a component.
527553

0 commit comments

Comments
 (0)