Skip to content

Commit fd5f531

Browse files
authored
feat(gazelle)!: Move the plugin to a separate workspace (#972)
feat!(gazelle): Move the plugin to a separate workspace Summary: * Move go.mod to gazelle. * Move gazelle definition. * Fix file distribution for the gazelle module. * Update the example test. * Include rules_python_gazelle_plugin during integration tests * Update ignored packages * Update CI configuration
1 parent 43e18f0 commit fd5f531

File tree

31 files changed

+208
-103
lines changed

31 files changed

+208
-103
lines changed

.bazelci/presubmit.yml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,11 @@ buildifier:
1010
# As a regression test for #225, check that wheel targets still build when
1111
# their package path is qualified with the repo name.
1212
- "@rules_python//examples/wheel/..."
13-
- "-//gazelle/..."
1413
build_flags:
1514
- "--keep_going"
1615
test_targets:
1716
- "--"
1817
- "..."
19-
# The gazelle tests are not compatible with Windows, so we only test them
20-
# on Linux. The build file generation, which uses this Gazelle extension,
21-
# runs on all platforms, and is asserted by the build_file_generation
22-
# integration tests below.
23-
- "-//gazelle/..."
2418
test_flags:
2519
- "--test_tag_filters=-integration-test"
2620
.reusable_build_test_all: &reusable_build_test_all
@@ -30,8 +24,9 @@ tasks:
3024
gazelle_extension:
3125
name: Test the Gazelle extension
3226
platform: ubuntu2004
33-
build_targets: ["//gazelle/..."]
34-
test_targets: ["//gazelle/..."]
27+
build_targets: ["//..."]
28+
test_targets: ["//..."]
29+
working_directory: gazelle
3530
ubuntu:
3631
<<: *reusable_config
3732
name: Default test on Ubuntu

.github/workflows/workspace_snippet.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ register_toolchains(
4040
)
4141
\`\`\`
4242
43-
## Using WORKSPACE:
43+
## Using WORKSPACE
4444
4545
Paste this snippet into your \`WORKSPACE\` file:
4646
@@ -58,4 +58,18 @@ load("@rules_python//python:repositories.bzl", "py_repositories")
5858
5959
py_repositories()
6060
\`\`\`
61+
62+
### Gazelle plugin
63+
64+
Paste this snippet into your \`WORKSPACE\` file:
65+
66+
\`\`\`starlark
67+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
68+
http_archive(
69+
name = "rules_python_gazelle_plugin",
70+
sha256 = "${SHA}",
71+
strip_prefix = "${PREFIX}/gazelle",
72+
url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/${TAG}.tar.gz",
73+
)
74+
\`\`\`
6175
EOF

.gitignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,5 @@ user.bazelrc
4343
*.swp
4444
*.swo
4545

46-
# Go/Gazelle files
47-
# These otherwise match patterns above
48-
!go.mod
49-
!BUILD.out
50-
5146
# Python cache
5247
**/__pycache__/

BUILD.bazel

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@bazel_gazelle//:def.bzl", "gazelle")
1615
load(":version.bzl", "BAZEL_VERSION")
1716

1817
package(default_visibility = ["//visibility:public"])
@@ -32,10 +31,10 @@ filegroup(
3231
"WORKSPACE",
3332
"internal_deps.bzl",
3433
"internal_setup.bzl",
35-
"//gazelle:distribution",
3634
"//python:distribution",
3735
"//python/pip_install:distribution",
3836
"//tools:distribution",
37+
"@rules_python_gazelle_plugin//:distribution",
3938
],
4039
visibility = [
4140
"//examples:__pkg__",
@@ -61,23 +60,6 @@ filegroup(
6160
visibility = ["//visibility:public"],
6261
)
6362

64-
# Gazelle configuration options.
65-
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
66-
# gazelle:prefix github.com/bazelbuild/rules_python
67-
# gazelle:exclude bazel-out
68-
# gazelle:exclude examples/**
69-
gazelle(name = "gazelle")
70-
71-
gazelle(
72-
name = "gazelle_update_repos",
73-
args = [
74-
"-from_file=go.mod",
75-
"-to_macro=gazelle/deps.bzl%gazelle_deps",
76-
"-prune",
77-
],
78-
command = "update-repos",
79-
)
80-
8163
genrule(
8264
name = "assert_bazelversion",
8365
srcs = [".bazelversion"],

WORKSPACE

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ python_register_multi_toolchains(
3434
python_versions = MINOR_MAPPING.values(),
3535
)
3636

37-
load("//gazelle:deps.bzl", "gazelle_deps")
38-
39-
# gazelle:repository_macro gazelle/deps.bzl%gazelle_deps
40-
gazelle_deps()
41-
4237
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4338

4439
# Used for Bazel CI
@@ -58,3 +53,17 @@ rbe_preconfig(
5853
name = "buildkite_config",
5954
toolchain = "ubuntu1804-bazel-java11",
6055
)
56+
57+
local_repository(
58+
name = "rules_python_gazelle_plugin",
59+
path = "gazelle",
60+
)
61+
62+
# The rules_python gazelle extension has some third-party go dependencies
63+
# which we need to fetch in order to compile it.
64+
load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
65+
66+
# See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
67+
# This rule loads and compiles various go dependencies that running gazelle
68+
# for python requirements.
69+
_py_gazelle_deps()

examples/build_file_generation/BUILD.bazel

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
# ruleset. When the symbol is loaded you can use the rule.
55
load("@bazel_gazelle//:def.bzl", "gazelle")
66
load("@pip//:requirements.bzl", "all_whl_requirements")
7-
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
8-
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
9-
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
107
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
118
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
9+
load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
10+
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
11+
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
1212

1313
compile_pip_requirements(
1414
name = "requirements",
@@ -53,7 +53,7 @@ gazelle_python_manifest(
5353
gazelle(
5454
name = "gazelle",
5555
data = GAZELLE_PYTHON_RUNTIME_DEPS,
56-
gazelle = "@rules_python//gazelle:gazelle_python_binary",
56+
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
5757
)
5858

5959
# This rule is auto-generated and managed by Gazelle,

examples/build_file_generation/WORKSPACE

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ local_repository(
8282
# url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.16.1.tar.gz",
8383
# )
8484

85+
# We import the repository-local rules_python_gazelle_plugin version in order to
86+
# be able to test development changes to the plugin.
87+
local_repository(
88+
name = "rules_python_gazelle_plugin",
89+
path = "../../gazelle",
90+
)
91+
92+
# When loading the gazelle plugin outside this repo, use the http_archive rule as follows:
93+
#
94+
#http_archive(
95+
# name = "rules_python_gazelle_plugin",
96+
# sha256 = "497ca47374f48c8b067d786b512ac10a276211810f4a580178ee9b9ad139323a",
97+
# strip_prefix = "rules_python-0.16.1/gazelle",
98+
# url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.16.1.tar.gz",
99+
#)
100+
85101
# Next we load the toolchain from rules_python.
86102
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
87103

@@ -124,7 +140,7 @@ install_deps()
124140

125141
# The rules_python gazelle extension has some third-party go dependencies
126142
# which we need to fetch in order to compile it.
127-
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
143+
load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
128144

129145
# See: https://github.com/bazelbuild/rules_python/blob/main/gazelle/README.md
130146
# This rule loads and compiles various go dependencies that running gazelle

examples/build_file_generation/gazelle_python.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ manifest:
114114
zipp.py310compat: zipp
115115
pip_repository:
116116
name: pip
117-
integrity: 4153df7683d64d7d6ad56c14ea1c7f7bec84a2ddf9ef8f075d1bb9313b8d11aa
117+
integrity: 2c84a3cabeaff134a1d045e5a173a3178086f236ab20f895ffbd7f3b7a6e5bb0

gazelle/.bazelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
test --test_output=errors
2+
3+
# Do NOT implicitly create empty __init__.py files in the runfiles tree.
4+
# By default, these are created in every directory containing Python source code
5+
# or shared libraries, and every parent directory of those directories,
6+
# excluding the repo root directory. With this flag set, we are responsible for
7+
# creating (possibly empty) __init__.py files and adding them to the srcs of
8+
# Python targets as required.
9+
build --incompatible_default_to_explicit_init_py
10+
11+
# Windows makes use of runfiles for some rules
12+
build --enable_runfiles
13+
startup --windows_enable_symlinks

gazelle/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Bazel directories
2+
/bazel-*
3+
/bazel-bin
4+
/bazel-genfiles
5+
/bazel-out
6+
/bazel-testlogs
7+
user.bazelrc
8+
9+
# Go/Gazelle files
10+
# These otherwise match patterns above
11+
!go.mod
12+
!BUILD.out

0 commit comments

Comments
 (0)