Skip to content

Commit 7d82f06

Browse files
authored
feat: add bzlmod support for gazelle plugin (#1077)
* feat: add optional pkg alias generation to pip_repository * feat: support using aliases in gazelle * doc: migrate gazelle example to use user friendly aliases * feat: gazelle supports bzlmod * chore: update gazelle plugin deps * chore: bazel run //:gazelle * fix: remove deps that are imported as bazel deps See: bazel-contrib/bazel-gazelle#1403 * ci: add build_file_generation to show that we support bzlmod
1 parent 51458e8 commit 7d82f06

File tree

16 files changed

+231
-69
lines changed

16 files changed

+231
-69
lines changed

.bazelci/presubmit.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ tasks:
7373
build_targets: ["//..."]
7474
test_targets: ["//..."]
7575
working_directory: gazelle
76+
gazelle_extension_bzlmod:
77+
<<: *common_bzlmod_flags
78+
name: Test the Gazelle extension under bzlmod
79+
platform: ubuntu2004
80+
build_targets: ["//..."]
81+
test_targets: ["//..."]
82+
working_directory: gazelle
83+
7684
ubuntu_min:
7785
<<: *minimum_supported_version
7886
<<: *reusable_config
@@ -138,6 +146,32 @@ tasks:
138146
working_directory: examples/build_file_generation
139147
platform: windows
140148

149+
integration_test_build_file_generation_bzlmod_ubuntu:
150+
<<: *minimum_supported_bzlmod_version
151+
<<: *common_bzlmod_flags
152+
<<: *reusable_build_test_all
153+
name: build_file_generation_bzlmod integration tests on Ubuntu
154+
working_directory: examples/build_file_generation
155+
platform: ubuntu2004
156+
integration_test_build_file_generation_bzlmod_debian:
157+
<<: *common_bzlmod_flags
158+
<<: *reusable_build_test_all
159+
name: build_file_generation_bzlmod integration tests on Debian
160+
working_directory: examples/build_file_generation
161+
platform: debian11
162+
integration_test_build_file_generation_bzlmod_macos:
163+
<<: *common_bzlmod_flags
164+
<<: *reusable_build_test_all
165+
name: build_file_generation_bzlmod integration tests on macOS
166+
working_directory: examples/build_file_generation
167+
platform: macos
168+
integration_test_build_file_generation_bzlmod_windows:
169+
<<: *common_bzlmod_flags
170+
<<: *reusable_build_test_all
171+
name: build_file_generation_bzlmod integration tests on Windows
172+
working_directory: examples/build_file_generation
173+
platform: windows
174+
141175
integration_test_bzlmod_ubuntu_min:
142176
<<: *minimum_supported_bzlmod_version
143177
<<: *reusable_build_test_all

docs/pip_repository.md

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/build_file_generation/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ gazelle_python_manifest(
4343
modules_mapping = ":modules_map",
4444
pip_repository_name = "pip",
4545
requirements = "//:requirements_lock.txt",
46+
# NOTE: we can use this flag in order to make our setup compatible with
47+
# bzlmod.
48+
use_pip_repository_aliases = True,
4649
)
4750

4851
# Our gazelle target points to the python gazelle binary.
@@ -65,7 +68,7 @@ py_library(
6568
visibility = ["//:__subpackages__"],
6669
deps = [
6770
"//random_number_generator",
68-
"@pip_flask//:pkg",
71+
"@pip//flask",
6972
],
7073
)
7174

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module(
2+
name = "example_bzlmod",
3+
version = "0.0.0",
4+
compatibility_level = 1,
5+
)
6+
7+
bazel_dep(name = "rules_python", version = "0.19.0")
8+
bazel_dep(name = "rules_python_gazelle_plugin", version = "0.19.0")
9+
bazel_dep(name = "gazelle", version = "0.29.0", repo_name = "bazel_gazelle")
10+
11+
# local overrides for the packages for CI purposes.
12+
# for usual setups you should remove this block.
13+
local_path_override(
14+
module_name = "rules_python",
15+
path = "../..",
16+
)
17+
18+
local_path_override(
19+
module_name = "rules_python_gazelle_plugin",
20+
path = "../../gazelle",
21+
)
22+
23+
# Register python toolchain
24+
python = use_extension("@rules_python//python:extensions.bzl", "python")
25+
python.toolchain(
26+
name = "python3_9",
27+
python_version = "3.9",
28+
)
29+
use_repo(python, "python3_9_toolchains")
30+
31+
register_toolchains(
32+
"@python3_9_toolchains//:all",
33+
)
34+
35+
pip = use_extension("@rules_python//python:extensions.bzl", "pip")
36+
pip.parse(
37+
name = "pip",
38+
# Generate user friendly alias labels for each dependency that we have.
39+
incompatible_generate_aliases = True,
40+
requirements_lock = "//:requirements_lock.txt",
41+
requirements_windows = "//:requirements_windows.txt",
42+
)
43+
use_repo(pip, "pip")

examples/build_file_generation/WORKSPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ load("@rules_python//python:pip.bzl", "pip_parse")
9090
# You can instead check this `requirements.bzl` file into your repo.
9191
pip_parse(
9292
name = "pip",
93+
# Generate user friendly alias labels for each dependency that we have.
94+
incompatible_generate_aliases = True,
9395
# (Optional) You can provide a python_interpreter (path) or a python_interpreter_target (a Bazel target, that
9496
# acts as an executable). The latter can be anything that could be used as Python interpreter. E.g.:
9597
# 1. Python interpreter that you compile in the build file.

examples/build_file_generation/gazelle_python.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@ manifest:
114114
zipp.py310compat: zipp
115115
pip_repository:
116116
name: pip
117-
integrity: 2c84a3cabeaff134a1d045e5a173a3178086f236ab20f895ffbd7f3b7a6e5bb0
117+
use_pip_repository_aliases: true
118+
integrity: 85f073e37e31339508aaaf5e0d5472adae5148fd5f054e9cc586343c026660e1

gazelle/MODULE.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module(
2+
name = "rules_python_gazelle_plugin",
3+
version = "0.0.0",
4+
compatibility_level = 1,
5+
)
6+
7+
bazel_dep(name = "rules_python", version = "0.18.0")
8+
bazel_dep(name = "rules_go", version = "0.38.1", repo_name = "io_bazel_rules_go")
9+
bazel_dep(name = "gazelle", version = "0.29.0", repo_name = "bazel_gazelle")
10+
11+
go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
12+
go_deps.from_file(go_mod = "//:go.mod")
13+
use_repo(
14+
go_deps,
15+
"com_github_bazelbuild_buildtools",
16+
"com_github_bmatcuk_doublestar",
17+
"com_github_emirpasic_gods",
18+
"com_github_ghodss_yaml",
19+
"in_gopkg_yaml_v2",
20+
)

gazelle/deps.bzl

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,22 @@ def gazelle_deps():
2828
sum = "h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=",
2929
version = "v0.0.0-20190523083050-ea95bdfd59fc",
3030
)
31-
go_repository(
32-
name = "com_github_bazelbuild_bazel_gazelle",
33-
importpath = "github.com/bazelbuild/bazel-gazelle",
34-
sum = "h1:+/ZhUxlDy4XnyMIGeKkbRZoIGssy1eO51GijwIvvuwE=",
35-
version = "v0.27.0",
36-
)
31+
3732
go_repository(
3833
name = "com_github_bazelbuild_buildtools",
3934
build_naming_convention = "go_default_library",
4035
importpath = "github.com/bazelbuild/buildtools",
4136
sum = "h1:jhiMzJ+8unnLRtV8rpbWBFE9pFNzIqgUTyZU5aA++w8=",
4237
version = "v0.0.0-20221004120235-7186f635531b",
4338
)
44-
go_repository(
45-
name = "com_github_bazelbuild_rules_go",
46-
importpath = "github.com/bazelbuild/rules_go",
47-
sum = "h1:ViPR65vOrg74JKntAUFY6qZkheBKGB6to7wFd8gCRU4=",
48-
version = "v0.35.0",
49-
)
39+
5040
go_repository(
5141
name = "com_github_bmatcuk_doublestar",
5242
importpath = "github.com/bmatcuk/doublestar",
5343
sum = "h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0=",
5444
version = "v1.3.4",
5545
)
56-
go_repository(
57-
name = "com_github_bmatcuk_doublestar_v4",
58-
importpath = "github.com/bmatcuk/doublestar/v4",
59-
sum = "h1:Qu+u9wR3Vd89LnlLMHvnZ5coJMWKQamqdz9/p5GNthA=",
60-
version = "v4.2.0",
61-
)
46+
6247
go_repository(
6348
name = "com_github_burntsushi_toml",
6449
importpath = "github.com/BurntSushi/toml",
@@ -113,12 +98,7 @@ def gazelle_deps():
11398
sum = "h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=",
11499
version = "v0.1.0",
115100
)
116-
go_repository(
117-
name = "com_github_fsnotify_fsnotify",
118-
importpath = "github.com/fsnotify/fsnotify",
119-
sum = "h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=",
120-
version = "v1.5.4",
121-
)
101+
122102
go_repository(
123103
name = "com_github_ghodss_yaml",
124104
importpath = "github.com/ghodss/yaml",
@@ -134,33 +114,22 @@ def gazelle_deps():
134114
go_repository(
135115
name = "com_github_golang_mock",
136116
importpath = "github.com/golang/mock",
137-
sum = "h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=",
138-
version = "v1.6.0",
117+
sum = "h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=",
118+
version = "v1.1.1",
139119
)
140120
go_repository(
141121
name = "com_github_golang_protobuf",
142122
importpath = "github.com/golang/protobuf",
143-
sum = "h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=",
144-
version = "v1.5.2",
123+
sum = "h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=",
124+
version = "v1.4.3",
145125
)
146126
go_repository(
147127
name = "com_github_google_go_cmp",
148128
importpath = "github.com/google/go-cmp",
149129
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
150130
version = "v0.5.9",
151131
)
152-
go_repository(
153-
name = "com_github_pelletier_go_toml",
154-
importpath = "github.com/pelletier/go-toml",
155-
sum = "h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=",
156-
version = "v1.9.5",
157-
)
158-
go_repository(
159-
name = "com_github_pmezard_go_difflib",
160-
importpath = "github.com/pmezard/go-difflib",
161-
sum = "h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=",
162-
version = "v1.0.0",
163-
)
132+
164133
go_repository(
165134
name = "com_github_prometheus_client_model",
166135
importpath = "github.com/prometheus/client_model",
@@ -218,8 +187,8 @@ def gazelle_deps():
218187
go_repository(
219188
name = "org_golang_google_protobuf",
220189
importpath = "google.golang.org/protobuf",
221-
sum = "h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=",
222-
version = "v1.28.0",
190+
sum = "h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=",
191+
version = "v1.25.0",
223192
)
224193
go_repository(
225194
name = "org_golang_x_crypto",
@@ -260,8 +229,8 @@ def gazelle_deps():
260229
go_repository(
261230
name = "org_golang_x_sync",
262231
importpath = "golang.org/x/sync",
263-
sum = "h1:0SH2R3f1b1VmIMG7BXbEZCBUu2dKmHschSmjqGUrW8A=",
264-
version = "v0.0.0-20220907140024-f12130a52804",
232+
sum = "h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=",
233+
version = "v0.0.0-20220722155255-886fb9371eb4",
265234
)
266235
go_repository(
267236
name = "org_golang_x_sys",

gazelle/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ module github.com/bazelbuild/rules_python/gazelle
33
go 1.19
44

55
require (
6-
github.com/bazelbuild/bazel-gazelle v0.27.0
76
github.com/bazelbuild/buildtools v0.0.0-20221004120235-7186f635531b
8-
github.com/bazelbuild/rules_go v0.35.0
97
github.com/bmatcuk/doublestar v1.3.4
108
github.com/emirpasic/gods v1.18.1
119
github.com/ghodss/yaml v1.0.0

gazelle/manifest/defs.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ def gazelle_python_manifest(
2424
modules_mapping,
2525
pip_repository_name = "",
2626
pip_deps_repository_name = "",
27-
manifest = ":gazelle_python.yaml"):
27+
manifest = ":gazelle_python.yaml",
28+
use_pip_repository_aliases = False):
2829
"""A macro for defining the updating and testing targets for the Gazelle manifest file.
2930
3031
Args:
3132
name: the name used as a base for the targets.
3233
requirements: the target for the requirements.txt file.
3334
pip_repository_name: the name of the pip_install or pip_repository target.
35+
use_pip_repository_aliases: boolean flag to enable using user-friendly
36+
python package aliases.
3437
pip_deps_repository_name: deprecated - the old pip_install target name.
3538
modules_mapping: the target for the generated modules_mapping.json file.
3639
manifest: the target for the Gazelle manifest file.
@@ -67,6 +70,12 @@ def gazelle_python_manifest(
6770
update_target_label,
6871
]
6972

73+
if use_pip_repository_aliases:
74+
update_args += [
75+
"--use-pip-repository-aliases",
76+
"true",
77+
]
78+
7079
go_binary(
7180
name = update_target,
7281
embed = [Label("//manifest/generate:generate_lib")],

gazelle/manifest/generate/generate.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ func init() {
3838
}
3939

4040
func main() {
41-
var manifestGeneratorHashPath string
42-
var requirementsPath string
43-
var pipRepositoryName string
44-
var modulesMappingPath string
45-
var outputPath string
46-
var updateTarget string
41+
var (
42+
manifestGeneratorHashPath string
43+
requirementsPath string
44+
pipRepositoryName string
45+
usePipRepositoryAliases bool
46+
modulesMappingPath string
47+
outputPath string
48+
updateTarget string
49+
)
4750
flag.StringVar(
4851
&manifestGeneratorHashPath,
4952
"manifest-generator-hash",
@@ -60,6 +63,11 @@ func main() {
6063
"pip-repository-name",
6164
"",
6265
"The name of the pip_install or pip_repository target.")
66+
flag.BoolVar(
67+
&usePipRepositoryAliases,
68+
"use-pip-repository-aliases",
69+
false,
70+
"Whether to use the pip-repository aliases, which are generated when passing 'incompatible_generate_aliases = True'.")
6371
flag.StringVar(
6472
&modulesMappingPath,
6573
"modules-mapping",
@@ -103,7 +111,8 @@ func main() {
103111
manifestFile := manifest.NewFile(&manifest.Manifest{
104112
ModulesMapping: modulesMapping,
105113
PipRepository: &manifest.PipRepository{
106-
Name: pipRepositoryName,
114+
Name: pipRepositoryName,
115+
UsePipRepositoryAliases: usePipRepositoryAliases,
107116
},
108117
})
109118
if err := writeOutput(

gazelle/manifest/manifest.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@ type Manifest struct {
144144
type PipRepository struct {
145145
// The name of the pip_install or pip_repository target.
146146
Name string
147+
// UsePipRepositoryAliases allows to use aliases generated pip_repository
148+
// when passing incompatible_generate_aliases = True.
149+
UsePipRepositoryAliases bool `yaml:"use_pip_repository_aliases,omitempty"`
147150
}

0 commit comments

Comments
 (0)