Skip to content

Fix sys.path ordering: single-file module from earlier root beats package from later root#3962

Open
kain88-de wants to merge 1 commit into
facebook:mainfrom
kain88-de:fix-search-path-ordering
Open

Fix sys.path ordering: single-file module from earlier root beats package from later root#3962
kain88-de wants to merge 1 commit into
facebook:mainfrom
kain88-de:fix-search-path-ordering

Conversation

@kain88-de

Copy link
Copy Markdown

Summary

Python's sys.path semantics are first-match-wins regardless of whether the match is a .py file or a package directory. When widget.py exists in root0 and widget/init.py exists in root1, root0's module should win.

The bug was in FindResult::best_result, which unconditionally placed RegularPackage and LegacyNamespacePackage at the top priority tier. This meant a package from any fallback root would override a single-file module found earlier in the search path.

The fix adds an explicit match arm before the RegularPackage tier: when a is a SingleFilePyModule or CompiledModule and b is a package type, a wins. This preserves the existing behavior where .pyi stubs from later roots still beat .py implementations (handled by the subsequent SingleFilePyiModule arm).

Fixes #3407

Test Plan

Two new tests covering the bug directly:

  • module::finder::tests::test_single_file_in_earlier_root_beats_package_in_later_root — unit
    test on find_module
  • test::imports::test_search_path_module_beats_later_package — integration test verifying no
    type error when importing from the correctly resolved module

…kage from later root

Python's sys.path semantics are first-match-wins regardless of whether the
match is a .py file or a package directory. When widget.py exists in root0
and widget/__init__.py exists in root1, root0's module should win.

The bug was in FindResult::best_result, which unconditionally placed
RegularPackage and LegacyNamespacePackage at the top priority tier. This
meant a package from any fallback root would override a single-file module
found earlier in the search path.

The fix adds an explicit match arm before the RegularPackage tier: when a
is a SingleFilePyModule or CompiledModule and b is a package type, a wins.
This preserves the existing behavior where .pyi stubs from later roots still
beat .py implementations (handled by the subsequent SingleFilePyiModule arm).
@meta-cla

meta-cla Bot commented Jun 28, 2026

Copy link
Copy Markdown

Hi @kain88-de!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jun 28, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the cla signed label Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

search_path: package in later entry shadows module in earlier entry

1 participant