Skip to content

feat: capture pre-init Python environment in init telemetry#6534

Open
FarhanAliRaza wants to merge 3 commits into
reflex-dev:mainfrom
FarhanAliRaza:init-even-venv
Open

feat: capture pre-init Python environment in init telemetry#6534
FarhanAliRaza wants to merge 3 commits into
reflex-dev:mainfrom
FarhanAliRaza:init-even-venv

Conversation

@FarhanAliRaza
Copy link
Copy Markdown
Contributor

Snapshot virtualenv state and presence of pyproject.toml/requirements.txt before template scaffolding runs so the "init" event reflects the user's actual starting environment, not files the template created.

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

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

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?
  • Have you successfully ran tests with your changes locally?

fixes ENG-9476

Snapshot virtualenv state and presence of pyproject.toml/requirements.txt
before template scaffolding runs so the "init" event reflects the user's
actual starting environment, not files the template created.
@FarhanAliRaza FarhanAliRaza requested a review from a team as a code owner May 19, 2026 19:44
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 19, 2026

Greptile Summary

This PR adds a get_init_environment() helper that snapshots whether the user is inside a virtualenv and whether pyproject.toml/requirements.txt exist, capturing this state before template scaffolding modifies the directory. The snapshot is then forwarded as event properties to the existing send("init", …) call.

  • Adds is_in_virtualenv() (PEP 405 prefix comparison + VIRTUAL_ENV fallback) and get_init_environment() to telemetry.py, called in initialize_app before any scaffolding touches the filesystem.
  • Extends the test suite with fixtures to control sys.prefix/sys.base_prefix/VIRTUAL_ENV and a temporary working directory, covering virtualenv detection and both dependency-file presence paths.

Confidence Score: 5/5

Safe to merge — the change is additive telemetry only and does not affect application init logic.

The snapshot is captured before scaffolding, passed through the existing properties merge path in _prepare_event, and is never persisted or used in any control flow.

reflex/utils/telemetry.py — the conda detection gap in is_in_virtualenv is worth a second look before the data is relied upon for analysis.

Important Files Changed

Filename Overview
reflex/utils/telemetry.py Adds is_in_virtualenv() and get_init_environment(); conda environments (CONDA_PREFIX) are not detected by is_in_virtualenv(), so conda users will always report in_virtualenv: false.
reflex/utils/templates.py Correctly captures init_environment before scaffolding and passes it to telemetry.send; the placement is correct and the send call matches the existing signature.
tests/units/test_telemetry.py Well-structured fixtures and tests cover PEP-405 venv detection, VIRTUAL_ENV fallback, system Python, pyproject.toml presence, requirements.txt presence, and empty directory.

Sequence Diagram

sequenceDiagram
    participant User
    participant initialize_app
    participant get_init_environment
    participant Filesystem
    participant telemetry_send

    User->>initialize_app: reflex init
    initialize_app->>initialize_app: check Config.FILE exists (reinit guard)
    initialize_app->>get_init_environment: capture pre-scaffolding state
    get_init_environment->>get_init_environment: is_in_virtualenv() [sys.prefix, VIRTUAL_ENV]
    get_init_environment->>Filesystem: Path("pyproject.toml").exists()
    get_init_environment->>Filesystem: Path("requirements.txt").exists()
    get_init_environment-->>initialize_app: "{in_virtualenv, has_pyproject_toml, has_requirements_txt}"
    initialize_app->>initialize_app: scaffold template
    initialize_app->>telemetry_send: "send("init", template=…, properties=init_environment)"
    telemetry_send-->>User: async fire-and-forget to PostHog
Loading

Reviews (2): Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." | Re-trigger Greptile

Comment thread tests/units/test_telemetry.py
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 19, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing FarhanAliRaza:init-even-venv (adecb73) with main (447db31)

Open in CodSpeed

@masenf
Copy link
Copy Markdown
Collaborator

masenf commented May 19, 2026

@greptile

Comment thread reflex/utils/telemetry.py
"""
return {
"in_virtualenv": is_in_virtualenv(),
"has_pyproject_toml": Path(constants.PyprojectToml.FILE).exists(),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

add has_uv_lock and has_reflex_lock (directory) here

Comment thread reflex/utils/telemetry.py
A dict with ``in_virtualenv``, ``has_pyproject_toml`` and
``has_requirements_txt`` boolean flags.
"""
return {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

short circuit when telemetry is not enabled

Comment thread reflex/utils/templates.py

# Captured before scaffolding so the snapshot reflects the user's CWD,
# not files the template will create.
init_environment = telemetry.get_init_environment()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

move this call above the constants.Config.FILE.exists() check and also include this information in the reinit event

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.

2 participants