8000 feat(gazelle)!: Move the plugin to a separate workspace (#972) · bazel-contrib/rules_python@fd5f531 · GitHub
[go: up one dir, main page]

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

gazelle/BUILD.bazel

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
alias(
2-
name = "gazelle",
3-
actual = "//gazelle/python",
4-
visibility = ["//visibility:public"],
5-
)
1+
load("@bazel_gazelle//:def.bzl", "gazelle")
2+
3+
# Gazelle configuration options.
4+
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
5+
# gazelle:prefix github.com/bazelbuild/rules_python/gazelle
6+
# gazelle:exclude bazel-out
7+
gazelle(name = "gazelle")
68

7-
alias(
8-
name = "gazelle_python_binary",
9-
actual = "//gazelle/python:gazelle_binary",
10-
visibility = ["//visibility:public"],
9+
gazelle(
10+
name = "gazelle_update_repos",
11+
args = [
12+
"-from_file=go.mod",
13+
"-to_macro=deps.bzl%gazelle_deps",
14+
"-prune",
15+
],
16+
command = "update-repos",
1117
)
1218

1319
filegroup(
1420
name = "distribution",
15-
srcs = glob(["**"]) + [
16-
"//gazelle/manifest:distribution",
17-
"//gazelle/modules_mapping:distribution",
18-
"//gazelle/python:distribution",
19-
"//gazelle/pythonconfig:distribution",
21+
srcs = [
22+
":BUILD.bazel",
23+
":README.md",
24+
":WORKSPACE",
25+
":def.bzl",
26+
":deps.bzl",
27+
":go.mod",
28+
":go.sum",
29+
"//manifest:distribution",
30+
"//modules_mapping:distribution",
31+
"//python:distribution",
32+
"//pythonconfig:distribution",
2033
],
21-
visibility = ["//:__pkg__"],
34+
visibility = ["@rules_python//:__pkg__"],
2235
)

gazelle/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ depends on.
1717
Add this to your `WORKSPACE`:
1818

1919
```starlark
20+
http_archive(
21+
name = "rules_python_gazelle_plugin",
22+
sha256 = "",
23+
strip_prefix = "rules_python-0.17.0/gazelle",
24+
url = "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.17.0.tar.gz",
25+
)
26+
2027
# To compile the rules_python gazelle extension from source,
2128
# we must fetch some third-party go dependencies that it uses.
22-
load("@rules_python//gazelle:deps.bzl", _py_gazelle_deps = "gazelle_deps")
29+
30+
load("@rules_python_gazelle_plugin//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
2331

2432
_py_gazelle_deps()
2533
```
@@ -40,8 +48,8 @@ To keep the metadata updated, put this in your `BUILD.bazel` file next to `gazel
4048

4149
```starlark
4250
load("@pip//:requirements.bzl", "all_whl_requirements")
43-
load("@rules_python//gazelle/manifest:defs.bzl", "gazelle_python_manifest")
44-
load("@rules_python//gazelle/modules_mapping:def.bzl", "modules_mapping")
51+
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
52+
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
4553

4654
# This rule fetches the metadata for python packages we depend on. That data is
4755
# required for the gazelle_python_manifest rule to update our manifest file.
@@ -75,7 +83,7 @@ with the rules_python extension included. This typically goes in your root
7583

7684
```
7785
load("@bazel_gazelle//:def.bzl", "gazelle")
78-
load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
86+
load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
7987
8088
# Our gazelle target points to the python gazelle binary.
8189
# This is the simple case where we only need one language supported.
@@ -85,7 +93,7 @@ load("@rules_python//gazelle:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
8593
gazelle(
8694
name = "gazelle",
8795
data = GAZELLE_PYTHON_RUNTIME_DEPS,
88-
gazelle = "@rules_python//gazelle:gazelle_python_binary",
96+
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
8997
)
9098
```
9199

gazelle/WORKSPACE

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
workspace(name = "rules_python_gazelle_plugin")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "io_bazel_rules_go",
7+
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
8+
urls = [
9+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
10+
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
11+
],
12+
)
13+
14+
http_archive(
15+
name = "bazel_gazelle",
16+
sha256 = "448e37e0dbf61d6fa8f00aaa12d191745e14f07c31cabfa731f0c8e8a4f41b97",
17+
urls = [
18+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
19+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
20+
],
21+
)
22+
23+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
24+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
25+
26+
go_rules_dependencies()
27+
28+
go_register_toolchains(version = "1.19.4")
29+
30+
gazelle_dependencies()
31+
32+
local_repository(
33+
name = "rules_python",
34+
path = "..",
35+
)
36+
37+
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
38+
39+
python_register_toolchains(
40+
name = "python39",
41+
python_version = "3.9",
42+
)
43+
44+
load("//:deps.bzl", _py_gazelle_deps = "gazelle_deps")
45+
46+
# gazelle:repository_macro deps.bzl%gazelle_deps
47+
_py_gazelle_deps()

gazelle/def.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"""
33

44
GAZELLE_PYTHON_RUNTIME_DEPS = [
5-
"@rules_python//gazelle/python:parse",
6-
"@rules_python//gazelle/python:std_modules",
5+
"@rules_python_gazelle_plugin//python:parse",
6+
"@rules_python_gazelle_plugin//python:std_modules",
77
]

go.mod renamed to gazelle/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/bazelbuild/rules_python
1+
module github.com/bazelbuild/rules_python/gazelle
22

33
go 1.19
44

go.sum renamed to gazelle/go.sum

File renamed without changes.

0 commit comments

Comments
 (0)
0