Skip to content

fix: do not include transitive sources from pyi_deps in runtime runfiles - #4178

Open
faximan wants to merge 9 commits into
bazel-contrib:mainfrom
faximan:faximan/fix-pyi-deps-runfiles
Open

faximan wants to merge 9 commits into
bazel-contrib:mainfrom
faximan:faximan/fix-pyi-deps-runfiles

Conversation

@faximan

@faximan faximan commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

pyi_deps are documented as build-time only dependencies for type checking and static analysis, but currently leaks into the runfiles.

Before: create_py_info merged pyi_deps targets using py_info.merge(), which caused all transitive sources from type stubs and their dependencies to be placed in PyInfo.transitive_sources and included in downstream binary/test runfiles.

After: create_py_info only propagates imports (for module import resolution during type checking), transitive_pyi_files, and transitive_original_sources from pyi_deps. Adds unit tests verifying transitive_sources and binary runfiles exclusion.

Tested: in our repo using rules_python and pyi_deps for type checking, this reduced the runfile tree size with 20M for a sample py_test.

…runfiles

`pyi_deps` are documented as build-time only dependencies for type
checking and static analysis. Previously, `create_py_info` merged
`pyi_deps` targets using `py_info.merge()`, which caused all transitive
sources from type stubs and their dependencies to be placed in
`PyInfo.transitive_sources` and included in downstream binary/test runfiles.

This change updates `create_py_info` to only propagate `imports`,
`transitive_pyi_files`, and `transitive_original_sources` from `pyi_deps`,
preventing them from leaking into runtime runfiles. Adds unit tests
verifying `transitive_sources` and binary runfiles exclusion.
@faximan faximan changed the title fix: do not include pyi_deps in runtime runfiles fix: do not include transitive sources from pyi_deps in runtime runfiles Sep 24, 2026
Unreleased changes must be recorded as news fragment files under news/ rather than editing CHANGELOG.md directly. Revert the direct edit to CHANGELOG.md and add news/4178.fixed.md.
Encapsulate build-time PyInfo merging into PyInfoBuilder.merge_build_time() so create_py_info does not inspect individual PyInfo fields for pyi_deps. Only pyi_files are merged; runtime fields (imports, transitive_sources) and transitive_original_sources are excluded.
A type-checking-only dependency in pyi_deps can include plain .py files or imports that are needed by static type checkers, but should not be included in runtime outputs. Add PyInfo.type_checking_info and PyInfoBuilder.merge_type_checking() to store and propagate type-checking-only PyInfo separately from runtime PyInfo fields.
@faximan

faximan commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

@rickeylev thanks so much for fixing up the PR. Let me know if I can help with any of the code changes or if we can move forward with the review.

@aignas

aignas commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

I personally think having a config flag gate the behaviour would be better. For debug/analysis rules we may want to propagate the pyi files, but fer fully stripped production artifacts maybe we don't need them. Consider introducing a config flag that would acces "auto" "yes" or "no" values.

@faximan

faximan commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

I personally think having a config flag gate the behaviour would be better. For debug/analysis rules we may want to propagate the pyi files, but fer fully stripped production artifacts maybe we don't need them. Consider introducing a config flag that would acces "auto" "yes" or "no" values.

This is not something I have a strong opinion on, so I added the flag, with the default 'auto' meaning prune iff -c opt. I added corresponding tests and documentation.

PTAL!

Comment on lines +251 to +253
* `auto`: (default) Automatically decide the effective value based on the
compilation mode. In `opt` and standard builds, `pyi_deps` are pruned from
runtime runfiles.

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.

This is really nice, thank you.

FYI, @rickeylev, we could do the same in the #4192.

Comment thread python/private/flags.bzl Outdated
@faximan

faximan commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

PTAL - also fixed a failing test with 69a7e04.

@faximan
faximan requested a review from aignas October 2, 2026 09:31

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.

3 participants