Skip to content

Commit de6ca1b

Browse files
authored
refactor: use gazelle for bzl_library management (bazel-contrib#3852)
To make it easier to manage the many bzl_library targets we have, switch to using gazelle to do so. Unfortunately, gazelle has strong opinions about target names: it forces `{foo}` names and doesn't allow `{foo}_bzl` names. Trying to make it do so requires quite a bit of gazelle directives. Instead, rename all the internal targets, but create aliases for public targets. Along the way... * Delete unused py_args.bzl * Add agent rule to avoid inappropriate copyright * Add a skill for creating rules
1 parent 9ed7641 commit de6ca1b

54 files changed

Lines changed: 1741 additions & 1384 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
trigger: always_on
3+
---
4+
5+
# When to Use Copyright
6+
7+
Unless directed by the user otherwise, do not add Bazel copyright to new or
8+
existing files.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
name: rule-creator
3+
description: Create and format agent rules with proper front matter in the workspace
4+
---
5+
6+
Use this skill when you need to create a new rule for the agent in the
7+
workspace.
8+
9+
### Rule File Location
10+
11+
All workspace-specific rules must be created as individual Markdown files under
12+
the `.agents/rules/` directory:
13+
```
14+
.agents/rules/<rule-name>.md
15+
```
16+
17+
### Rule Format
18+
19+
Every rule file must start with a YAML front matter block defining the trigger
20+
condition, followed by the rule content in Markdown.
21+
22+
```yaml
23+
---
24+
trigger: <trigger-condition>
25+
---
26+
27+
# <Rule Title>
28+
29+
<Rule description and directives...>
30+
```
31+
32+
#### Trigger Conditions
33+
* `always_on`: The rule is always active and must be followed for all tasks.
34+
* Custom triggers: You can specify other trigger conditions if the rule only
35+
applies in certain contexts.
36+
37+
### Formatting Guidelines
38+
* **Line Wrapping:** Always wrap all text in the rule file (including the
39+
title and description) to **80 columns** to ensure readability and
40+
compatibility.
41+
* **Clarity:** Write clear, actionable directives.
42+
43+
### Example
44+
45+
To create a rule that prevents adding copyrights:
46+
47+
```markdown
48+
---
49+
trigger: always_on
50+
---
51+
52+
# No Copyrights Rule
53+
54+
Unless directed by the user otherwise, do not add Bazel copyright to new or
55+
existing files.
56+
```

.pre-commit-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ repos:
6565
entry: ./tools/private/sync_downloader_configs.py
6666
files: downloader_config\.cfg$
6767
pass_filenames: false
68+
- id: gazelle
69+
name: Run Gazelle
70+
language: system
71+
entry: bazel run //tools/private/gazelle
72+
files: (\.bzl|\.bazel|BUILD|WORKSPACE(\.bzlmod)?)$
73+
pass_filenames: false

BUILD.bazel

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@
1414

1515
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1616

17+
# Resolve rules_cc Starlark libraries to their correct targets
18+
# gazelle:resolve starlark @rules_cc//cc/common:cc_info.bzl @rules_cc//cc/common:common
19+
# gazelle:resolve starlark @rules_cc//cc/common:cc_common.bzl @rules_cc//cc/common:common
20+
21+
# Resolve protobuf Starlark libraries
22+
# gazelle:resolve starlark @com_google_protobuf//bazel:py_proto_library.bzl @com_google_protobuf//bazel:py_proto_library_bzl
23+
24+
# Resolve bazel_tools repo rules to our internal wrapper to avoid transitive dependencies
25+
# gazelle:resolve starlark @bazel_tools//tools/build_defs/repo:http.bzl //python/private:bazel_tools
26+
# gazelle:resolve starlark @bazel_tools//tools/build_defs/repo:utils.bzl //python/private:bazel_tools
27+
28+
# Prevent Gazelle from incorrectly stripping the .bzl suffix from the toml.bzl repo name
29+
# gazelle:resolve starlark @toml.bzl//:toml.bzl @toml.bzl//:toml
30+
31+
# Override Gazelle's incorrect default resolution for platforms host constraints Starlark library
32+
# gazelle:resolve starlark @platforms//host:constraints.bzl @platforms//host:constraints_lib
33+
34+
# Override Gazelle's incorrect default resolution for rules_cc Starlark libraries
35+
# gazelle:resolve starlark @rules_cc//cc:cc_import.bzl @rules_cc//cc:core_rules
36+
# gazelle:resolve starlark @rules_cc//cc:cc_library.bzl @rules_cc//cc:core_rules
37+
38+
# Exclude directories that are separate packages/workspaces or only for testing/examples
39+
# gazelle:exclude tests
40+
# gazelle:exclude examples
41+
42+
# Exclude internal development tools and dependencies that users don't need
43+
# gazelle:exclude internal_dev_setup.bzl
44+
# gazelle:exclude internal_dev_deps.bzl
45+
# gazelle:exclude python/private/internal_dev_deps.bzl
46+
# gazelle:exclude workspace_bazel9.bzl
47+
48+
# Exclude legacy paths that are only kept for backwards compatibility
49+
# gazelle:exclude python/private/common
50+
1751
package(default_visibility = ["//visibility:public"])
1852

1953
licenses(["notice"])
@@ -52,12 +86,6 @@ filegroup(
5286
],
5387
)
5488

55-
bzl_library(
56-
name = "version_bzl",
57-
srcs = ["version.bzl"],
58-
visibility = ["//:__subpackages__"],
59-
)
60-
6189
# Reexport of all bzl files used to allow downstream rules to generate docs
6290
# without shipping with a dependency on Skylib
6391
filegroup(
@@ -73,3 +101,10 @@ filegroup(
73101
],
74102
visibility = ["//visibility:public"],
75103
)
104+
105+
# keep
106+
bzl_library(
107+
name = "version",
108+
srcs = ["version.bzl"],
109+
visibility = ["//:__subpackages__"],
110+
)

MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ bazel_dep(name = "another_module", version = "0", dev_dependency = True)
9696
# Extra gazelle plugin deps so that WORKSPACE.bzlmod can continue including it for e2e tests.
9797
# We use `WORKSPACE.bzlmod` because it is impossible to have dev-only local overrides.
9898
bazel_dep(name = "rules_go", version = "0.60.0", dev_dependency = True, repo_name = "io_bazel_rules_go")
99+
bazel_dep(name = "gazelle", version = "0.40.0", dev_dependency = True, repo_name = "bazel_gazelle")
100+
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.8.2", dev_dependency = True)
99101

100102
internal_dev_deps = use_extension(
101103
"//python/private:internal_dev_deps.bzl",

WORKSPACE

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ workspace(name = "rules_python")
1717
# Everything below this line is used only for developing rules_python. Users
1818
# should not copy it to their WORKSPACE.
1919

20-
# Necessary so that Bazel 9 recognizes this as rules_python and doesn't try
21-
# to load the version Bazel itself uses by default.
22-
# buildifier: disable=duplicated-name
23-
local_repository(
24-
name = "rules_python",
25-
path = ".",
26-
)
20+
# Workaround for Bazel 9 duplicate name issue in Gazelle.
21+
load("//:workspace_bazel9.bzl", "bazel_9_workaround")
22+
23+
bazel_9_workaround()
2724

2825
load("//:internal_dev_deps.bzl", "rules_python_internal_deps")
2926

docs/BUILD.bazel

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -106,59 +106,59 @@ build_test(
106106
sphinx_stardocs(
107107
name = "bzl_api_docs",
108108
srcs = [
109-
"//python:defs_bzl",
110-
"//python:features_bzl",
111-
"//python:packaging_bzl",
112-
"//python:pip_bzl",
113-
"//python:proto_bzl",
114-
"//python:py_binary_bzl",
115-
"//python:py_cc_link_params_info_bzl",
116-
"//python:py_exec_tools_info_bzl",
117-
"//python:py_exec_tools_toolchain_bzl",
118-
"//python:py_executable_info_bzl",
119-
"//python:py_info_bzl",
120-
"//python:py_library_bzl",
121-
"//python:py_runtime_bzl",
122-
"//python:py_runtime_info_bzl",
123-
"//python:py_test_bzl",
124-
"//python:repositories_bzl",
125-
"//python/api:api_bzl",
126-
"//python/api:attr_builders_bzl",
127-
"//python/api:executables_bzl",
128-
"//python/api:libraries_bzl",
129-
"//python/api:rule_builders_bzl",
130-
"//python/cc:py_cc_toolchain_bzl",
131-
"//python/cc:py_cc_toolchain_info_bzl",
132-
"//python/entry_points:py_console_script_binary_bzl",
133-
"//python/extensions:config_bzl",
134-
"//python/extensions:python_bzl",
135-
"//python/local_toolchains:repos_bzl",
136-
"//python/private:attr_builders_bzl",
137-
"//python/private:builders_bzl",
138-
"//python/private:builders_util_bzl",
139-
"//python/private:py_binary_rule_bzl",
140-
"//python/private:py_cc_toolchain_rule_bzl",
141-
"//python/private:py_info_bzl",
142-
"//python/private:py_library_rule_bzl",
143-
"//python/private:py_runtime_rule_bzl",
144-
"//python/private:py_test_rule_bzl",
145-
"//python/private:rule_builders_bzl",
146-
"//python/private/api:py_common_api_bzl",
147-
"//python/private/pypi:config_settings_bzl",
148-
"//python/private/pypi:env_marker_info_bzl",
149-
"//python/private/pypi:pkg_aliases_bzl",
150-
"//python/private/pypi:whl_config_setting_bzl",
151-
"//python/private/pypi:whl_library_bzl",
152-
"//python/private/zipapp:py_zipapp_rule_bzl",
153-
"//python/uv:lock_bzl",
154-
"//python/uv:uv_bzl",
155-
"//python/uv:uv_toolchain_bzl",
156-
"//python/uv:uv_toolchain_info_bzl",
157-
"//python/zipapp:py_zipapp_binary_bzl",
158-
"//python/zipapp:py_zipapp_test_bzl",
109+
"//python:defs",
110+
"//python:features",
111+
"//python:packaging",
112+
"//python:pip",
113+
"//python:proto",
114+
"//python:py_binary",
115+
"//python:py_cc_link_params_info",
116+
"//python:py_exec_tools_info",
117+
"//python:py_exec_tools_toolchain",
118+
"//python:py_executable_info",
119+
"//python:py_info",
120+
"//python:py_library",
121+
"//python:py_runtime",
122+
"//python:py_runtime_info",
123+
"//python:py_test",
124+
"//python:repositories",
125+
"//python/api",
126+
"//python/api:attr_builders",
127+
"//python/api:executables",
128+
"//python/api:libraries",
129+
"//python/api:rule_builders",
130+
"//python/cc:py_cc_toolchain",
131+
"//python/cc:py_cc_toolchain_info",
132+
"//python/entry_points:py_console_script_binary",
133+
"//python/extensions:config",
134+
"//python/extensions:python",
135+
"//python/local_toolchains:repos",
136+
"//python/private:attr_builders",
137+
"//python/private:builders",
138+
"//python/private:builders_util",
139+
"//python/private:py_binary_rule",
140+
"//python/private:py_cc_toolchain_rule",
141+
"//python/private:py_info",
142+
"//python/private:py_library_rule",
143+
"//python/private:py_runtime_rule",
144+
"//python/private:py_test_rule",
145+
"//python/private:rule_builders",
146+
"//python/private/api:py_common_api",
147+
"//python/private/pypi:config_settings",
148+
"//python/private/pypi:env_marker_info",
149+
"//python/private/pypi:pkg_aliases",
150+
"//python/private/pypi:whl_config_setting",
151+
"//python/private/pypi:whl_library",
152+
"//python/private/zipapp:py_zipapp_rule",
153+
"//python/uv",
154+
"//python/uv:lock",
155+
"//python/uv:uv_toolchain",
156+
"//python/uv:uv_toolchain_info",
157+
"//python/zipapp:py_zipapp_binary",
158+
"//python/zipapp:py_zipapp_test",
159159
] + ([
160160
# This depends on @pythons_hub, which is only created under bzlmod,
161-
"//python/extensions:pip_bzl",
161+
"//python/extensions:pip",
162162
] if BZLMOD_ENABLED else []),
163163
prefix = "api/rules_python/",
164164
tags = ["docs"],
@@ -167,7 +167,7 @@ sphinx_stardocs(
167167

168168
sphinx_stardoc(
169169
name = "py_runtime_pair",
170-
src = "//python/private:py_runtime_pair_rule_bzl",
170+
src = "//python/private:py_runtime_pair_rule",
171171
prefix = "api/rules_python/",
172172
tags = ["docs"],
173173
target_compatible_with = _TARGET_COMPATIBLE_WITH,

gazelle/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
load("@bazel_gazelle//:def.bzl", "gazelle")
2+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
23

34
# Gazelle configuration options.
45
# See https://github.com/bazelbuild/bazel-gazelle#running-gazelle-with-bazel
56
# gazelle:prefix github.com/bazel-contrib/rules_python/gazelle
67
# gazelle:exclude bazel-out
8+
# gazelle:exclude deps.bzl
9+
# gazelle:exclude internal_dev_deps.bzl
710
gazelle(
811
name = "gazelle",
912
)
@@ -36,3 +39,9 @@ filegroup(
3639
],
3740
visibility = ["@rules_python//:__pkg__"],
3841
)
42+
43+
bzl_library(
44+
name = "def",
45+
srcs = ["def.bzl"],
46+
visibility = ["//visibility:public"],
47+
)

gazelle/manifest/BUILD.bazel

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
12
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
23

34
exports_files([
@@ -11,8 +12,8 @@ go_library(
1112
importpath = "github.com/bazel-contrib/rules_python/gazelle/manifest",
1213
visibility = ["//visibility:public"],
1314
deps = [
14-
"@com_github_emirpasic_gods//sets/treeset",
15-
"@in_gopkg_yaml_v2//:yaml_v2",
15+
"@com_github_emirpasic_gods//sets/treeset:go_default_library",
16+
"@in_gopkg_yaml_v2//:go_default_library",
1617
],
1718
)
1819

@@ -32,3 +33,14 @@ filegroup(
3233
],
3334
visibility = ["//:__pkg__"],
3435
)
36+
37+
bzl_library(
38+
name = "defs",
39+
srcs = ["defs.bzl"],
40+
visibility = ["//visibility:public"],
41+
deps = [
42+
"@bazel_skylib//rules:diff_test",
43+
"@io_bazel_rules_go//go:def",
44+
"@rules_python//python:defs_bzl",
45+
],
46+
)

gazelle/modules_mapping/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
12
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
23
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
34

@@ -56,3 +57,9 @@ filegroup(
5657
srcs = glob(["**"]),
5758
visibility = ["//:__pkg__"],
5859
)
60+
61+
bzl_library(
62+
name = "def",
63+
srcs = ["def.bzl"],
64+
visibility = ["//visibility:public"],
65+
)

0 commit comments

Comments
 (0)