Skip to content

Commit a5a7ffb

Browse files
f0rmigaalexeagleJonathon Belotti
authored
Gazelle extension for Python (#514)
Gazelle plugin * Add new example to --deleted_packages * Update examples/build_file_generation/BUILD Co-authored-by: Jonathon Belotti <[email protected]> * fix: gazelle:exclude on coarse-grained Signed-off-by: Thulio Ferraz Assis <[email protected]> * fix: comment on Kinds() Co-authored-by: Jonathon Belotti <[email protected]> * owner: f0rmiga Signed-off-by: Thulio Ferraz Assis <[email protected]> * fix: build and setuptools pinned versions With the recent change in pypa/setuptools#2769, some wheels started to fail build immediately with an unpinned setuptools in isolation mode. Signed-off-by: Thulio Ferraz Assis <[email protected]> * refactor: use local_repository in examples Signed-off-by: Thulio Ferraz Assis <[email protected]> * bump: examples Bazel version Signed-off-by: Thulio Ferraz Assis <[email protected]> * fix: add missing .gitignore to example Signed-off-by: Thulio Ferraz Assis <[email protected]> * refactor: remove python_coarse_grained_generation Also add the python_generation_mode directive. Signed-off-by: Thulio Ferraz Assis <[email protected]> * fix: gazelle spam from org_golang_x_tools Signed-off-by: Thulio Ferraz Assis <[email protected]> * revert: example .bazelversion Signed-off-by: Thulio Ferraz Assis <[email protected]> * fix: simplify std_modules.py Signed-off-by: Thulio Ferraz Assis <[email protected]> * feat: test py_library without __init__.py Signed-off-by: Thulio Ferraz Assis <[email protected]> * feat: manifest generation tag manual Signed-off-by: Thulio Ferraz Assis <[email protected]> * fix: check std modules last Performing the check last is more correct and yields better performance, noticeable on large repositories. Signed-off-by: Thulio Ferraz Assis <[email protected]> Co-authored-by: Alex Eagle <[email protected]> Co-authored-by: Jonathon Belotti <[email protected]>
1 parent 431caac commit a5a7ffb

File tree

389 files changed

+5702
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

389 files changed

+5702
-2
lines changed

.bazelrc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
build --deleted_packages=examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import,examples/relative_requirements
7-
query --deleted_packages=examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import,examples/relative_requirements
6+
build --deleted_packages=examples/build_file_generation,examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import,examples/relative_requirements
7+
query --deleted_packages=examples/build_file_generation,examples/legacy_pip_import/boto,examples/legacy_pip_import/extras,examples/legacy_pip_import/helloworld,examples/pip_install,examples/pip_parse,examples/py_import,examples/relative_requirements
88

99
test --test_output=errors
10+
11+
# Do NOT implicitly create empty __init__.py files in the runfiles tree.
12+
# By default, these are created in every directory containing Python source code
13+
# or shared libraries, and every parent directory of those directories,
14+
# excluding the repo root directory. With this flag set, we are responsible for
15+
# creating (possibly empty) __init__.py files and adding them to the srcs of
16+
# Python targets as required.
17+
build --incompatible_default_to_explicit_init_py

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
/python/whl.bzl @thundergolfer @andyscott
1111
/python/requirements.txt @thundergolfer @andyscott
1212

13+
# Directory containing the Gazelle extension and Go code.
14+
/gazelle/ @f0rmiga
15+
1316
# The proposals dir corresponds to the Bazel proposals process, documented
1417
# here: https://bazel.build/designs/index.html
1518
/proposals/ @brandjon @lberki

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@
4141
# vim swap files
4242
*.swp
4343
*.swo
44+
45+
# Go/Gazelle files
46+
# These otherwise match patterns above
47+
!go.mod
48+
!BUILD.out

BUILD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
load("@bazel_gazelle//:def.bzl", "gazelle")
15+
1416
package(default_visibility = ["//visibility:public"])
1517

1618
licenses(["notice"]) # Apache 2.0
@@ -51,3 +53,19 @@ filegroup(
5153
],
5254
visibility = ["//visibility:public"],
5355
)
56+
57+
# Gazelle configuration options.
58+
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
59+
# gazelle:prefix github.com/bazelbuild/rules_python
60+
# gazelle:exclude bazel-out
61+
gazelle(name = "gazelle")
62+
63+
gazelle(
64+
name = "update_go_deps",
65+
args = [
66+
"-from_file=go.mod",
67+
"-to_macro=gazelle/deps.bzl%gazelle_deps",
68+
"-prune",
69+
],
70+
command = "update-repos",
71+
)

WORKSPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ rules_python_internal_deps()
2424
load("//:internal_setup.bzl", "rules_python_internal_setup")
2525

2626
rules_python_internal_setup()
27+
28+
load("//gazelle:deps.bzl", "gazelle_deps")
29+
30+
# gazelle:repository_macro gazelle/deps.bzl%gazelle_deps
31+
gazelle_deps()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.0.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel-*
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
load("@bazel_gazelle//:def.bzl", "gazelle")
2+
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
3+
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
4+
load("@rules_python//python:defs.bzl", "py_library")
5+
6+
# Gazelle python extension needs a manifest file mapping from
7+
# an import to the installed package that provides it.
8+
# This macro produces two targets:
9+
# - //:gazelle_python_manifest.update can be used with `bazel run`
10+
# to recalculate the manifest
11+
# - //:gazelle_python_manifest.test is a test target ensuring that
12+
# the manifest doesn't need to be updated
13+
gazelle_python_manifest(
14+
name = "gazelle_python_manifest",
15+
modules_mapping = "@modules_map//:modules_mapping.json",
16+
pip_deps_repository_name = "pip",
17+
requirements = "//:requirements_lock.txt",
18+
)
19+
20+
# Our gazelle target points to the python gazelle binary.
21+
# This is the simple case where we only need one language supported.
22+
# If you also had proto, go, or other gazelle-supported languages,
23+
# you would also need a gazelle_binary rule.
24+
# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
25+
gazelle(
26+
name = "gazelle",
27+
data = GAZELLE_PYTHON_RUNTIME_DEPS,
28+
gazelle = "@rules_python//gazelle:gazelle_python_binary",
29+
)
30+
31+
# This rule is auto-generated and managed by Gazelle,
32+
# because it found the __init__.py file in this folder.
33+
py_library(
34+
name = "build_file_generation",
35+
srcs = ["__init__.py"],
36+
visibility = ["//:__subpackages__"],
37+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Build file generation with Gazelle
2+
3+
This example shows a project that has Gazelle setup with the rules_python
4+
extension, so that targets like `py_library` and `py_binary` can be
5+
automatically created just by running
6+
7+
```sh
8+
$ bazel run //:gazelle
9+
```
10+
11+
As a demo, try creating a `__main__.py` file in this directory, then
12+
re-run that gazelle command. You'll see that a `py_binary` target
13+
is created in the `BUILD` file.
14+
15+
Or, try importing the `requests` library in `__init__.py`.
16+
You'll see that `deps = ["@pip//pypi__requests"]` is automatically
17+
added to the `py_library` target in the `BUILD` file.
18+
19+
For more information on the behavior of the rules_python gazelle extension,
20+
see the README.md file in the /gazelle folder.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
workspace(name = "build_file_generation_example")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
######################################################################
6+
# We need rules_go and bazel_gazelle, to build the gazelle plugin from source.
7+
# Setup instructions for this section are at
8+
# https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
9+
10+
# Note, you could omit the rules_go dependency, if you have some way to statically
11+
# compile the gazelle binary for your workspace and distribute it to users on all
12+
# needed platforms.
13+
http_archive(
14+
name = "io_bazel_rules_go",
15+
sha256 = "69de5c704a05ff37862f7e0f5534d4f479418afc21806c887db544a316f3cb6b",
16+
urls = [
17+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.27.0/rules_go-v0.27.0.tar.gz",
18+
"https://github.com/bazelbuild/rules_go/releases/download/v0.27.0/rules_go-v0.27.0.tar.gz",
19+
],
20+
)
21+
22+
# NB: bazel-gazelle version must be after 18 August 2021
23+
# to include https://github.com/bazelbuild/bazel-gazelle/commit/2834ea4
24+
http_archive(
25+
name = "bazel_gazelle",
26+
sha256 = "0bb8056ab9ed4cbcab5b74348d8530c0e0b939987b0cfe36c1ab53d35a99e4de",
27+
strip_prefix = "bazel-gazelle-2834ea44b3ec6371c924baaf28704730ec9d4559",
28+
urls = [
29+
# No release since March, and we need subsequent fixes
30+
"https://github.com/bazelbuild/bazel-gazelle/archive/2834ea44b3ec6371c924baaf28704730ec9d4559.zip",
31+
],
32+
)
33+
34+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
35+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
36+
37+
go_rules_dependencies()
38+
39+
go_register_toolchains(version = "1.16.5")
40+
41+
gazelle_dependencies()
42+
43+
######################################################################
44+
# Remaining setup is for rules_python
45+
46+
local_repository(
47+
name = "rules_python",
48+
path = "../..",
49+
)
50+
51+
load("@rules_python//python:pip.bzl", "pip_install")
52+
53+
pip_install(
54+
# Uses the default repository name "pip"
55+
requirements = "//:requirements_lock.txt",
56+
)
57+
58+
# The rules_python gazelle extension has some third-party go dependencies
59+
# which we need to fetch in order to compile it.
60+
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
61+
62+
_py_gazelle_deps()
63+
64+
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
65+
66+
# This repository rule fetches the metadata for python packages we
67+
# depend on. That data is required for the gazelle_python_manifest
68+
# rule to update our manifest file.
69+
# To see what this rule does, try `bazel run @modules_map//:print`
70+
modules_mapping(
71+
name = "modules_map",
72+
requirements = "//:requirements_lock.txt",
73+
)

0 commit comments

Comments
 (0)