Skip to content

fix(compiler): validate disabled-state rx.State usage structurally across compiled domains - #7965

Open
TejasLamba2006 wants to merge 1 commit into
reflex-dev:mainfrom
TejasLamba2006:fix/compiler-structural-state-context-guard
Open

TejasLamba2006 wants to merge 1 commit into
reflex-dev:mainfrom
TejasLamba2006:fix/compiler-structural-state-context-guard

Conversation

@TejasLamba2006

@TejasLamba2006 TejasLamba2006 commented Nov 22, 2026 •

Copy link
Copy Markdown

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?

Summary

Fixes #7579 by replacing the generated-JS substring guard (code_uses_state_contexts on page_ctx.output_code) in compile_app with structural validation over originating VarData metadata across all final compilation domains:

  • Auto-memoized and explicit memo state reads: Auto-memoization moves stateful subtrees out of page modules into memo modules, and Component._get_vars_hooks strips originating VarData to position-only metadata when emitting hooks. _compiled_app_uses_state inspects originating VarData.field_dependencies (walking Vars, deps, components, app_wraps, hooks, prop-embedded components, and children) after app-wrap resolution/memoization, hydrate_fallback registration, and MEMOS scoping.
  • No false positives on literal text or client-only hooks: Pages rendering literal "useContext(StateContexts" or "useStateContext(" strings, rx.client_state, or custom client-only React hooks compile cleanly under rx.App(enable_state=False).
  • Actionable diagnostic: ReflexRuntimeError now explains that rx.State cannot be accessed when enable_state=False is set on rx.App (since the reproduction already defines a State subclass). reflex.state.code_uses_state_contexts is preserved for backwards compatibility with existing callers.

Testing

  • Added unit tests in tests/units/compiler/test_compiler.py covering auto-memoized and non-memoized state reads, explicit @rx.memo body and prop reads, prop-embedded stateful components, stateful extra_app_wraps, stateful hydrate_fallback, literal hook-spelling text, stateless/client-only hooks, and the enable_state=True control.

Fixes #7579

View guided diff

…ross compiled domains

Replace the JS substring check in compile_app with structural VarData
state-dependency inspection across compiled pages, app wraps, explicit
and automatic memos, prop-embedded components, hydrate fallbacks, and
document head entries, and explain enable_state=False in the raised
ReflexRuntimeError.

Fixes reflex-dev#7579
@TejasLamba2006
TejasLamba2006 requested a review from a team as a code owner November 22, 2026 13:55

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects frontend compilation when an app disables state.

The implementation has no established functional blocker, but the required test assertions and news fragment must be fixed before merging.

Findings

  1. P2 Tests miss broken error messages ▶
  2. P2 Release note is missing ▶

Summary

Replaces the generated-JavaScript substring check with a walk through originating VarData.

  • Disabled-state apps now check state use across compiled components.

Reviews (1) · Last reviewed commit: "fix(compiler): validate disabled-state r..." · Reviewed by Greptile

app = rx.App(enable_state=False)
app.add_page(lambda: rx.text(DynState.label), route="/")

with pytest.raises(ReflexRuntimeError, match=r"enable_state=False"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Tests miss broken error messages

The new error tests match only enable_state=False, so they still pass if the explanation or suggested fix is wrong. The repository guide explicitly requires complete error-message comparisons. Capture the exception and compare its full message before merging. The same pattern appears at lines 2474 and 2517, and elsewhere in the new tests.

Context Used: CLAUDE.md (source)

Comment on lines +1685 to +1695
if app._state is None and _compiled_app_uses_state(
pages=(
page_ctx.root_component for page_ctx in compile_ctx.compiled_pages.values()
),
app_root=app_root,
memos=all_memos,
head_components=app.head_components,
extra_vars=(
app.html_lang,
*(app.html_custom_attrs.values() if app.html_custom_attrs else ()),
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Release note is missing

This changes which apps fail compilation and replaces the error shown to users, but the PR adds no news/ fragment. The repository guide requires one for user-facing changes. Before merging, add a root news/+<slug>.bugfix.md entry describing the disabled-state check and the corrected handling of literal text.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enable_state=False guard (code_uses_state_contexts) misses memoized state reads

1 participant