Skip to content

fix(testing): prevent host Python env from leaking into Flutter tests - #6747

Open
PythBuster wants to merge 1 commit into
flet-dev:mainfrom
PythBuster:patch-1
Open

fix(testing): prevent host Python env from leaking into Flutter tests#6747
PythBuster wants to merge 1 commit into
flet-dev:mainfrom
PythBuster:patch-1

Conversation

@PythBuster

@PythBuster PythBuster commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description

FletTestApp started the Flutter integration-test process with the complete environment inherited from the host Python process.

IDEs such as PyCharm add debugger and sitecustomize directories to PYTHONPATH. That value was inherited by flutter test and subsequently passed to the embedded Serious Python runtime.

As a result, the packaged test app could exit before connecting to RemoteTester. Flutter then reported exit code 79 with "No tests were found", while the flet_app fixture failed during setup.

Create an explicit environment for the Flutter subprocess and remove PYTHONPATH and PYTHONHOME before launching it. All Flet, Flutter and SERIOUS_PYTHON_* variables remain unchanged.

This keeps host-only Python configuration out of the packaged runtime while preserving the environment required to build and execute integration tests.

Verified with the PyCharm TeamCity pytest plugin and PyCharm helper paths present in the host PYTHONPATH.

Test code

# Minimal test/reproduction code for reviewers, if applicable.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • I signed the CLA.
  • I have performed a self-review of my own code.
  • My code follows the style guidelines of this project.
  • I have commented my code, particularly in hard-to-understand areas.
  • My changes generate no new warnings.
  • New and existing tests pass locally with my changes.
  • I have made corresponding documentation changes, if applicable.
  • I have added changelog entries for user-facing changes, if applicable.
  • I have updated release guide pages and website/sidebars.yml for breaking changes, removals, and deprecations, if applicable.

Screenshots

Additional details

Summary by Sourcery

Bug Fixes:

  • Prevent host IDE and debugger Python configuration from causing packaged Flutter integration tests to exit prematurely by stripping PYTHONPATH and PYTHONHOME from the Flutter test environment.

FletTestApp started the Flutter integration-test process with the complete environment inherited from the host Python process.

IDEs such as PyCharm add debugger and sitecustomize directories to PYTHONPATH. That value was inherited by `flutter test` and subsequently passed to the embedded Serious Python runtime.

As a result, the packaged test app could exit before connecting to RemoteTester. Flutter then reported exit code 79 with "No tests were found", while the flet_app fixture failed during setup.

Create an explicit environment for the Flutter subprocess and remove PYTHONPATH and PYTHONHOME before launching it. 
All Flet, Flutter and SERIOUS_PYTHON_* variables remain unchanged.

This keeps host-only Python configuration out of the packaged runtime while preserving the environment required to build and execute integration tests.

Verified with the PyCharm TeamCity pytest plugin and PyCharm helper paths present in the host PYTHONPATH.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@sourcery-ai sourcery-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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@FeodorFitsner

Copy link
Copy Markdown
Contributor

Optional follow-up, same rationale as this fix - not blocking.

PYTHONPATH/PYTHONHOME are the two that bit you here, but they aren't the only host-Python knobs the embedded interpreter reads at init. Two more in the same family:

  • PYTHONNOUSERSITE - worth setting to 1 rather than popping. If the host's ~/.local/lib/pythonX.Y/site-packages happens to match the embedded interpreter's version, site.py adds it to sys.path and host packages leak in exactly the way this PR is preventing. Setting it explicitly is what actually blocks that, since the default (unset) means user site is enabled.
  • PYTHONEXECUTABLE - macOS framework builds use it to seed sys.executable; a host value pointing at the IDE's interpreter is wrong for the packaged app.

Both are pre-existing hazards rather than anything this PR introduces, so they're fine as a separate change. If they do get added, it might be worth flipping the shape from a denylist of pops to a small helper that builds the env - something like:

def _flutter_subprocess_env() -> dict[str, str]:
    """
    Environment for the `flutter test` child. Host-Python configuration must
    not reach the app's embedded interpreter: IDEs (PyCharm) inject debugger
    and sitecustomize paths via PYTHONPATH, which the packaged app imports at
    startup and dies on - surfacing as Flutter exit code 79, "No tests were
    found", before RemoteTester ever connects. All FLET_*, FLUTTER_* and
    SERIOUS_PYTHON_* variables are preserved.
    """
    env = os.environ.copy()
    for key in ("PYTHONPATH", "PYTHONHOME", "PYTHONEXECUTABLE"):
        env.pop(key, None)
    env["PYTHONNOUSERSITE"] = "1"
    return env

That also makes the one testable part of this - which keys are dropped, which survive - unit-testable without launching Flutter, and it parallels _flutter_path_env in flet_cli/commands/test.py.

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.

3 participants