Skip to content

Commit c8c0afe

Browse files
committed
Change py_binary to load from rules_python
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
1 parent 4f28899 commit c8c0afe

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

ImplementationReadme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ We make the choice to set the compilation working directory ("directory" key) to
116116

117117
I'm calling out this decision explicitly, because there's a tempting trap that caught Bazel's official editor plugins (Android Studio, Xcode, ...) before we helped fix them.
118118

119-
Do not be tempted to set the compilation "directory" to the bazel execroot (`bazel info execution_root`). The execroot may seem to work but breaks subtly on the next build; the execroot directory is reconfigured for whatever new target is being built, deleting the links to external workspaces and top-level packages not used by that particular build. Using the execroot might be tempting because that *is* where Bazel says it's going to invoke the command it gave you, but don't do it! It'll only have the subset of the code used for the last build, not for all build, breaking the paths used for editing the rest of the codebase.
119+
Do not be tempted to set the compilation "directory" to the bazel execroot (`bazel info execution_root`). The execroot may seem to work but breaks subtly on the next build; the execroot directory is reconfigured for whatever new target is being built, deleting the links to external workspaces and top-level packages not used by that particular build. Using the execroot might be tempting because that *is* where Bazel says it's going to invoke the command it gave you, but don't do it! It'll only have the subset of the code used for the last build, not for all builds, breaking the paths used for editing the rest of the codebase.
120120

121121
Remember that the key goal of `compile_commands.json` is to "de-Bazel" the build commands into something `clangd` can understand. Not pointing into Bazel's temporary build scratch space (execroot) is an important part of decoupling from Bazel's transitory state.
122122

MODULE.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module(name = "hedron_compile_commands")
22

3+
bazel_dep(name = "rules_python", version = "1.0.0")
4+
35
p = use_extension("//:workspace_setup.bzl", "hedron_compile_commands_extension")
46
pt = use_extension("//:workspace_setup_transitive.bzl", "hedron_compile_commands_extension")
57
ptt = use_extension("//:workspace_setup_transitive_transitive.bzl", "hedron_compile_commands_extension")

refresh_compile_commands.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ refresh_compile_commands(
5757
# Implementation
5858

5959
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
60+
load("@rules_python//python:py_binary.bzl", _py_binary = "py_binary")
6061

6162

6263
def refresh_compile_commands(
6364
name,
6465
targets = None,
6566
exclude_headers = None,
6667
exclude_external_sources = False,
68+
py_binary = _py_binary,
6769
**kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes.
6870
# Convert the various, acceptable target shorthands into the dictionary format
6971
# In Python, `type(x) == y` is an antipattern, but [Starlark doesn't support inheritance](https://bazel.build/rules/language), so `isinstance` doesn't exist, and this is the correct way to switch on type.
@@ -92,7 +94,7 @@ def refresh_compile_commands(
9294
_expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, **kwargs)
9395

9496
# Combine them so the wrapper calls the main script
95-
native.py_binary(
97+
py_binary(
9698
name = name,
9799
main = version_checker_script_name,
98100
srcs = [version_checker_script_name, script_name],

0 commit comments

Comments
 (0)