Skip to content

Commit f22f39c

Browse files
authored
fix(precompiling): only add pyc to default outputs if precompiling explicitly enabled for target (#2307)
This fixes a bug where precompiled files were *always* being added to the default outputs of a target. The intent is they are only added to a target's default outputs if the target explicitly opted into precompiling. This went unnoticed because the exec tools toolchain is still disabled by default, so the test that verifies a basic py_binary's default outputs didn't generate implicit pyc files. It was introduced when fixing the pyc collection bug. To fix, only add the pyc files deemed "required", not all the pyc files generated. Also added a test to capture this case to the precompile tests.
1 parent dd5db65 commit f22f39c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

python/private/py_executable.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment =
175175
default_outputs = builders.DepsetBuilder()
176176
default_outputs.add(executable)
177177
default_outputs.add(precompile_result.keep_srcs)
178-
default_outputs.add(precompile_result.pyc_files)
178+
default_outputs.add(required_pyc_files)
179179

180180
imports = collect_imports(ctx, semantics)
181181

tests/base_rules/precompile/precompile_tests.bzl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,38 @@ def _test_pyc_collection_include_dep_omit_source_impl(env, target):
514514

515515
_tests.append(_test_pyc_collection_include_dep_omit_source)
516516

517+
def _test_precompile_attr_inherit_pyc_collection_disabled_precompile_flag_enabled(name):
518+
if not rp_config.enable_pystar:
519+
rt_util.skip_test(name = name)
520+
return
521+
rt_util.helper_target(
522+
py_binary,
523+
name = name + "_subject",
524+
srcs = ["bin.py"],
525+
main = "bin.py",
526+
precompile = "inherit",
527+
pyc_collection = "disabled",
528+
)
529+
analysis_test(
530+
name = name,
531+
impl = _test_precompile_attr_inherit_pyc_collection_disabled_precompile_flag_enabled_impl,
532+
target = name + "_subject",
533+
config_settings = _COMMON_CONFIG_SETTINGS | {
534+
PRECOMPILE: "enabled",
535+
},
536+
)
537+
538+
def _test_precompile_attr_inherit_pyc_collection_disabled_precompile_flag_enabled_impl(env, target):
539+
target = env.expect.that_target(target)
540+
target.runfiles().not_contains_predicate(
541+
matching.str_matches("/bin.*pyc"),
542+
)
543+
target.default_outputs().not_contains_predicate(
544+
matching.file_path_matches("/bin.*pyc"),
545+
)
546+
547+
_tests.append(_test_precompile_attr_inherit_pyc_collection_disabled_precompile_flag_enabled)
548+
517549
def runfiles_contains_at_least_predicates(runfiles, predicates):
518550
for predicate in predicates:
519551
runfiles.contains_predicate(predicate)

0 commit comments

Comments
 (0)