bitter-ability-32190
12/27/2021, 3:56 PMbitter-ability-32190
12/27/2021, 3:56 PMBUILD
file had a few bumps, so I got clever and sliced our repo another way.bitter-ability-32190
12/27/2021, 4:00 PMpants-plugins/bazel-compat-macros.py
# Macros for Bazel defintions
def alias(*args, **kwargs): ...
def genrule(*args, **kwargs): ...
def exports_files(*args, **kwargs): ...
def load(*args, **kwargs): ...
def requirement(arg): ...
# ... you get the idea ;)
def glob(*args, **kwargs): return []
# Ones we do care about
def py_library(**kwargs):
srcs = kwargs.get("srcs", [])
if srcs:
return python_sources(name=kwargs["name"], sources=srcs)
def py_test(**kwargs):
srcs = kwargs.get("srcs", [])
if srcs:
return python_sources(name=kwargs["name"], sources=srcs)
Then to do the other side:
# In pants-compat.bzl
def python_requirements(**kwargs):
pass
And finally, the top-level BUILD
load("//:pants-compat.bzl", "python_requirements")
package(default_visibility = ["//visibility:public"])
# Bazel stuff
python_requirements(
module_mapping = {
"absl-py": ["absl"],
# ...
},
)
bitter-ability-32190
12/27/2021, 4:01 PMbitter-ability-32190
12/27/2021, 4:02 PMcurved-television-6568
12/27/2021, 4:06 PMbitter-ability-32190
12/27/2021, 4:08 PMlint
bitter-ability-32190
12/27/2021, 4:16 PMpants
for more than format/linting/checkingbitter-ability-32190
12/27/2021, 4:18 PMcurved-television-6568
12/27/2021, 4:22 PMmaybe have a āignore-unrecognized-definitionsā optionis it too many definitions to āstubā out with macros? or are there other constructs that doesnāt map to a macro syntactically ?
bitter-ability-32190
12/27/2021, 4:25 PMglob
bitter-ability-32190
12/27/2021, 4:42 PMlint
, it's much more complicated š .
Bazel is cool with double-ownership, but Pants not so muchbitter-ability-32190
12/27/2021, 4:44 PMpy_binary
rules are used in py_test
, but declare srcs=[...]
for their sources.
But there may or may not be a py_library
which also has the declared srcs
, leading to ambiguous ownership.
The solution is to scrub the `py_binary`s that use an already-owned py_library
to use the :foo/bar
syntax instead of declaring ownershipbitter-ability-32190
12/27/2021, 4:54 PMpy_binary
contains an actual src in srcs
, and then py_binary
is empty to Pantsbitter-ability-32190
12/27/2021, 5:02 PMdef py_binary(
srcs = [],
**kwargs
):
for src in srcs:
if src.endswith(".py"):
# This is valid Bazel, but for Pants-compat to work, each src should be owned
# once-and-only-once. And the easiest way to do that is throught `py_library`.
fail(
"`py_binary` cannot claim a Python src. Use `py_library` instead and depend on it.\n\n"
+ "E.g. declare:\n\n"
+ "py_library(\n"
+ ' name = "{}",\n'.format(src[:-3])
+ ' srcs = ["{}"],\n'.format(src)
+ ")\n\n"
+ 'Then set the `py_binary` `srcs` attribute to ":{}"'.format(src[:-3])
)
_py_binary(
srcs=srcs,
**kwargs,
)