Skip to content

Change py_binary to load from rules_python - #235

Open
stagnation wants to merge 1 commit into
hedronvision:mainfrom
stagnation:feature/change-py-binary-to-load-from-rules_python
Open

Change py_binary to load from rules_python#235
stagnation wants to merge 1 commit into
hedronvision:mainfrom
stagnation:feature/change-py-binary-to-load-from-rules_python

Conversation

@stagnation

Copy link
Copy Markdown

To follow the (future) migration of the python rules out of the set of native rules: bazelbuild/bazel#17773 . This requires us to load the 'rules_python' module and while at it we parameterize the 'py_binary' so it is possible for consumers to switch to their own macros, following the model of pip_compile: https://github.com/bazelbuild/rules_python/blob/9b2b70adba5431162401a97b2bbab1dc938e7245/python/private/pypi/pip_compile.bzl#L32

This solves loading errors:

ERROR: Traceback (most recent call last):
    File "/home/nwirekli/.cache/bazel/_bazel_nwirekli/1eeb85843c9866d288d7d5644c3b4615/
    external/hedron_compile_commands+/refresh_compile_commands.bzl", line 67, column 27, in <toplevel>
                py_binary = native.py_binary,
Error: no native function or rule 'py_binary'
Available attributes: ...
ERROR: error loading package under directory '': error loading package '@@hedron_compile_commands+//':
initialization of module 'refresh_compile_commands.bzl' failed

To follow the (future) migration of the python rules out of the set of
native rules: bazelbuild/bazel#17773 .
This requires us to load the 'rules_python' module and while at it we
parameterize the 'py_binary' so it is possible for consumers to switch
to their own macros, following the model of pip_compile:
https://github.com/bazelbuild/rules_python/blob/9b2b70adba5431162401a97b2bbab1dc938e7245/python/private/pypi/pip_compile.bzl#L32

This solves loading errors:

    ERROR: Traceback (most recent call last):
        File "/home/nwirekli/.cache/bazel/_bazel_nwirekli/1eeb85843c9866d288d7d5644c3b4615/
        external/hedron_compile_commands+/refresh_compile_commands.bzl", line 67, column 27, in <toplevel>
                    py_binary = native.py_binary,
    Error: no native function or rule 'py_binary'
    Available attributes: ...
    ERROR: error loading package under directory '': error loading package '@@hedron_compile_commands+//':
    initialization of module 'refresh_compile_commands.bzl' failed
@stagnation
stagnation force-pushed the feature/change-py-binary-to-load-from-rules_python branch from 059ae12 to c8c0afe Compare December 18, 2024 12:28
@stagnation

Copy link
Copy Markdown
Author

I think this is only needed because we use 8.0.0rc2. Where the default value for a new flag was too strict. A colleague mentioned similar errors and that with the real 8.0.0 this is not a problem. So, after digging ourselves out of the module-version hole that prevents a clean upgrade to 8 this is helpful. And I hope to save some time in a later migration away from native.py_binary.

@keith

keith commented Sep 12, 2025

Copy link
Copy Markdown

this will be required for bazel 9.x bazelbuild/bazel@0522f8e, so probably want to merge this soon since that's ~2 months away

@andrea-nisti

andrea-nisti commented Jan 27, 2026

Copy link
Copy Markdown

any updates on this? currently we overcome this with a patch

@stagnation

Copy link
Copy Markdown
Author

I am not actively working on it, but it stands to reason that this, or something rebased should be merged, yes :)

@helly25

helly25 commented May 17, 2026

Copy link
Copy Markdown

Hi — since upstream has been quiet for some time, the fork at https://github.com/helly25/bazel-compile-commands-extractor has picked up the py_binaryrules_python switch via the broader Bazel 9 fix in #278 (which also loads cc_binary from rules_cc and updates MODULE.bazel / README accordingly).

Backported in helly25#1. Thanks for the original patch.

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.

4 participants