Skip to content

Commit 189e30d

Browse files
rickeylevaignas
andauthored
docs: document some of our project styles/conventions (#2816)
Spurred by the discussion to converge on using `.` to separate generated targets, I wrote down some of the conventions we've adopted. --------- Co-authored-by: Ignas Anikevicius <[email protected]>
1 parent a79bbfa commit 189e30d

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

.editorconfig

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Unix-style newlines with a newline ending every file
2+
[*]
3+
end_of_line = lf
4+
insert_final_newline = true
5+
6+
# Set default charset
7+
[*]
8+
charset = utf-8
9+
10+
# Line width
11+
[*]
12+
max_line_length = 100
13+
14+
# 4 space indentation
15+
[*.{py,bzl}]
16+
indent_style = space
17+
indent_size = 4

CONTRIBUTING.md

+49
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,55 @@ The `legacy_foo` arg was removed
173173
:::
174174
```
175175

176+
## Style and idioms
177+
178+
For the most part, we just accept whatever the code formatters do, so there
179+
isn't much style to enforce.
180+
181+
Some miscellanous style, idioms, and conventions we have are:
182+
183+
### Markdown/Sphinx Style
184+
185+
* Use colons for prose sections of text, e.g. `:::{note}`, not backticks.
186+
* Use backticks for code blocks.
187+
* Max line length: 100.
188+
189+
### BUILD/bzl Style
190+
191+
* When a macro generates public targets, use a dot (`.`) to separate the
192+
user-provided name from the generted name. e.g. `foo(name="x")` generates
193+
`x.test`. The `.` is our convention to communicate that it's a generated
194+
target, and thus one should look for `name="x"` when searching for the
195+
definition.
196+
* The different build phases shouldn't load code that defines objects that
197+
aren't valid for their phase. e.g.
198+
* The bzlmod phase shouldn't load code defining regular rules or providers.
199+
* The repository phase shouldn't load code defining module extensions, regular
200+
rules, or providers.
201+
* The loading phase shouldn't load code defining module extensions or
202+
repository rules.
203+
* Loading utility libraries or generic code is OK, but should strive to load
204+
code that is usable for its phase. e.g. loading-phase code shouldn't
205+
load utility code that is predominately only usable to the bzlmod phase.
206+
* Providers should be in their own files. This allows implementing a custom rule
207+
that implements the provider without loading a specific implementation.
208+
* One rule per file is preferred, but not required. The goal is that defining an
209+
e.g. library shouldn't incur loading all the code for binaries, tests,
210+
packaging, etc; things that may be niche or uncommonly used.
211+
* Separate files should be used to expose public APIs. This ensures our public
212+
API is well defined and prevents accidentally exposing a package-private
213+
symbol as a public symbol.
214+
215+
:::{note}
216+
The public API file's docstring becomes part of the user-facing docs. That
217+
file's docstring must be used for module-level API documentation.
218+
:::
219+
* Repository rules should have name ending in `_repo`. This helps distinguish
220+
them from regular rules.
221+
* Each bzlmod extension, the "X" of `use_repo("//foo:foo.bzl", "X")` should be
222+
in its own file. The path given in the `use_repo()` expression is the identity
223+
Bazel uses and cannot be changed.
224+
176225
## Generated files
177226

178227
Some checked-in files are generated and need to be updated when a new PR is

0 commit comments

Comments
 (0)