1# How to contribute 2 3We'd love to accept your patches and contributions to this project. There are 4just a few small guidelines you need to follow. 5 6## Getting started 7 8Before we can work on the code, we need to get a copy of it and setup some 9local environment and tools. 10 11First, fork the code to your user and clone your fork. This gives you a private 12playground where you can do any edits you'd like. For this guide, we'll use 13the [GitHub `gh` tool](https://github.com/cli/cli) 14([Linux install](https://github.com/cli/cli/blob/trunk/docs/install_linux.md)). 15(More advanced users may prefer the GitHub UI and raw `git` commands). 16 17```shell 18gh repo fork bazelbuild/rules_python --clone --remote 19``` 20 21Next, make sure you have a new enough version of Python installed that supports the 22various code formatters and other devtools. For a quick start, 23[install pyenv](https://github.com/pyenv/pyenv-installer) and 24at least Python 3.9.15: 25 26```shell 27curl https://pyenv.run | bash 28pyenv install 3.9.15 29pyenv shell 3.9.15 30``` 31 32## Development workflow 33 34It's suggested that you create what is called a "feature/topic branch" in your 35fork when you begin working on code you want to eventually send or code review. 36 37``` 38git checkout main # Start our branch from the latest code 39git checkout -b my-feature # Create and switch to our feature branch 40git push origin my-feature # Cause the branch to be created in your fork. 41``` 42 43From here, you then edit code and commit to your local branch. If you want to 44save your work to github, you use `git push` to do so: 45 46``` 47git push origin my-feature 48``` 49 50Once the code is in your github repo, you can then turn it into a Pull Request 51to the actual rules_python project and begin the code review process. 52 53 54## Running tests 55 56Running tests is particularly easy thanks to Bazel, simply run: 57 58``` 59bazel test //... 60``` 61 62And it will run all the tests it can find. The first time you do this, it will 63probably take long time because various dependencies will need to be downloaded 64and setup. Subsequent runs will be faster, but there are many tests, and some of 65them are slow. If you're working on a particular area of code, you can run just 66the tests in those directories instead, which can speed up your edit-run cycle. 67 68## Formatting 69 70Starlark files should be formatted by 71[buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md). 72Otherwise the Buildkite CI will fail with formatting/linting violations. 73We suggest using a pre-commit hook to automate this. 74First [install pre-commit](https://pre-commit.com/#installation), 75then run 76 77```shell 78pre-commit install 79``` 80 81### Running buildifer manually 82 83You can also run buildifier manually. To do this, 84[install buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md), 85and run the following command: 86 87```shell 88$ buildifier --lint=fix --warnings=native-py -warnings=all WORKSPACE 89``` 90 91Replace the argument "WORKSPACE" with the file that you are linting. 92 93## Contributor License Agreement 94 95Contributions to this project must be accompanied by a Contributor License 96Agreement. You (or your employer) retain the copyright to your contribution, 97this simply gives us permission to use and redistribute your contributions as 98part of the project. Head over to <https://cla.developers.google.com/> to see 99your current agreements on file or to sign a new one. 100 101You generally only need to submit a CLA once, so if you've already submitted one 102(even if it was for a different project), you probably don't need to do it 103again. 104 105## Code reviews 106 107All submissions, including submissions by project members, require review. We 108use GitHub pull requests for this purpose. Consult [GitHub Help] for more 109information on using pull requests. 110 111[GitHub Help]: https://help.github.com/articles/about-pull-requests/ 112 113### Commit messages 114 115Commit messages (upon merging) and PR messages should follow the [Conventional 116Commits](https://www.conventionalcommits.org/) style: 117 118``` 119type(scope)!: <summary> 120 121<body> 122 123BREAKING CHANGE: <summary> 124``` 125 126Where `(scope)` is optional, and `!` is only required if there is a breaking change. 127If a breaking change is introduced, then `BREAKING CHANGE:` is required; see 128the [Breaking Changes](#breaking-changes) section for how to introduce breaking 129changes. 130 131Common `type`s: 132 133* `build:` means it affects the building or development workflow. 134* `docs:` means only documentation is being added, updated, or fixed. 135* `feat:` means a user-visible feature is being added. 136* `fix:` means a user-visible behavior is being fixed. 137* `refactor:` means some sort of code cleanup that doesn't change user-visible behavior. 138* `revert:` means a prior change is being reverted in some way. 139* `test:` means only tests are being added. 140 141For the full details of types, see 142[Conventional Commits](https://www.conventionalcommits.org/). 143 144## Generated files 145 146Some checked-in files are generated and need to be updated when a new PR is 147merged: 148 149* **requirements lock files**: These are usually generated by a 150 `compile_pip_requirements` update target, which is usually in the same directory. 151 e.g. `bazel run //docs:requirements.update` 152 153## Core rules 154 155The bulk of this repo is owned and maintained by the Bazel Python community. 156However, since the core Python rules (`py_binary` and friends) are still 157bundled with Bazel itself, the Bazel team retains ownership of their stubs in 158this repository. This will be the case at least until the Python rules are 159fully migrated to Starlark code. 160 161Practically, this means that a Bazel team member should approve any PR 162concerning the core Python logic. This includes everything under the `python/` 163directory except for `pip.bzl` and `requirements.txt`. 164 165Issues should be triaged as follows: 166 167- Anything concerning the way Bazel implements the core Python rules should be 168 filed under [bazelbuild/bazel](https://github.com/bazelbuild/bazel), using 169 the label `team-Rules-python`. 170 171- If the issue specifically concerns the rules_python stubs, it should be filed 172 here in this repository and use the label `core-rules`. 173 174- Anything else, such as feature requests not related to existing core rules 175 functionality, should also be filed in this repository but without the 176 `core-rules` label. 177 178(breaking-changes)= 179## Breaking Changes 180 181Breaking changes are generally permitted, but we follow a 3-step process for 182introducing them. The intent behind this process is to balance the difficulty of 183version upgrades for users, maintaining multiple code paths, and being able to 184introduce modern functionality. 185 186The general process is: 187 1881. In version `N`, introduce the new behavior, but it must be disabled by 189 default. Users can opt into the new functionality when they upgrade to 190 version `N`, which lets them try it and verify functionality. 1912. In version `N+1`, the new behavior can be enabled by default. Users can 192 opt out if necessary, but doing so causes a warning to be issued. 1933. In version `N+2`, the new behavior is always enabled and cannot be opted out 194 of. The API for the control mechanism can be removed in this release. 195 196Note that the `+1` and `+2` releases are just examples; the steps are not 197required to happen in immedially subsequent releases. 198 199 200### How to control breaking changes 201 202The details of the control mechanism will depend on the situation. Below is 203a summary of some different options. 204 205* Environment variables are best for repository rule behavior. Environment 206 variables can be propagated to rules and macros using the generated 207 `@rules_python_internal//:config.bzl` file. 208* Attributes are applicable to macros and regular rules, especially when the 209 behavior is likely to vary on a per-target basis. 210* [User defined build settings](https://bazel.build/extending/config#user-defined-build-settings) 211 (aka custom build flags) are applicable for rules when the behavior change 212 generally wouldn't vary on a per-target basis. They also have the benefit that 213 an entire code base can have them easily enabled by a bazel command line flag. 214* Allowlists allow a project to centrally control if something is 215 enabled/disabled. Under the hood, they are basically a specialized custom 216 build flag. 217 218Note that attributes and flags can seamlessly interoperate by having the default 219controlled by a flag, and an attribute can override the flag setting. This 220allows a project to enable the new behavior by default while they work to fix 221problematic cases to prepare for the next upgrade. 222 223### What is considered a breaking change? 224 225Precisely defining what constitutes a breaking change is hard because it's 226easy for _someone, somewhere_ to depend on _some_ observable behavior, despite 227our best efforts to thoroughly document what is or isn't supported and hiding 228any internal details. 229 230In general, something is considered a breaking change when it changes the 231direct behavior of a supported public API. Simply being able to observe a 232behavior change doesn't necessarily mean it's a breaking change. 233 234Long standing undocumented behavior is a large grey area and really depends on 235how load-bearing it has become and what sort of reasonable expectation of 236behavior there is. 237 238Here's some examples of what would or wouldn't be considered a breaking change. 239 240Breaking changes: 241 * Renaming an function argument for public functions. 242 * Enforcing stricter validation than was previously required when there's a 243 sensible reason users would run afoul of it. 244 * Changing the name of a public rule. 245 246Not breaking changes: 247 * Upgrading dependencies 248 * Changing internal details, such as renaming an internal file. 249 * Changing a rule to a macro. 250 251## FAQ 252 253### Installation errors when during `git commit` 254 255If you did `pre-commit install`, various tools are run when you do `git commit`. 256This might show as an error such as: 257 258``` 259[INFO] Installing environment for https://github.com/psf/black. 260[INFO] Once installed this environment will be reused. 261[INFO] This may take a few minutes... 262An unexpected error has occurred: CalledProcessError: command: ... 263``` 264 265To fix, you'll need to figure out what command is failing and why. Because these 266are tools that run locally, its likely you'll need to fix something with your 267environment or the installation of the tools. For Python tools (e.g. black or 268isort), you can try using a different Python version in your shell by using 269tools such as [pyenv](https://github.com/pyenv/pyenv). 270