1# Contributing to Tonic 2 3:balloon: Thanks for your help improving the project! We are so happy to have 4you! 5 6There are opportunities to contribute to `tonic` at any level. It doesn't 7matter if you are just getting started with Rust or are the most weathered 8expert, we can use your help. 9 10**No contribution is too small and all contributions are valued.** 11 12This guide will help you get started. **Do not let this guide intimidate you**. 13It should be considered a map to help you navigate the process. 14 15You may also get help with contributing in the `dev` channel, please join 16us! 17 18Tonic is a part of the [Tokio][tokio] and [Hyperium][hyperium] project, and follows the project's 19guidelines for contributing. This document is based on the 20[`CONTRIBUTING.md` file][tokio-contrib] in the `tokio-rs/tokio` repository. 21 22[dev]: https://gitter.im/tokio-rs/dev 23[tokio]: https://tokio.rs 24[hyperium]: https://github.com/hyperium 25[tokio-contrib]: https://github.com/tokio-rs/tokio/blob/master/CONTRIBUTING.md 26 27## Conduct 28 29The `tonic` project adheres to the [Rust Code of Conduct][coc]. This describes 30the _minimum_ behavior expected from all contributors. 31 32[coc]: https://github.com/rust-lang/rust/blob/master/CODE_OF_CONDUCT.md 33 34## Contributing in Issues 35 36For any issue, there are fundamentally three ways an individual can contribute: 37 381. By opening the issue for discussion: For instance, if you believe that you 39 have uncovered a bug in a `tonic` crate, creating a new issue in the 40 hyperium/tonic [issue tracker][issues] is the way to report it. 41 422. By helping to triage the issue: This can be done by providing 43 supporting details (a test case that demonstrates a bug), providing 44 suggestions on how to address the issue, or ensuring that the issue is tagged 45 correctly. 46 473. By helping to resolve the issue: Typically this is done either in the form of 48 demonstrating that the issue reported is not a problem after all, or more 49 often, by opening a Pull Request that changes some bit of something in 50 Tokio in a concrete and reviewable manner. 51 52**Anybody can participate in any stage of contribution**. We urge you to 53participate in the discussion around bugs and participate in reviewing PRs. 54 55[issues]: https://github.com/hyperium/tonic/issues 56 57### Asking for General Help 58 59If you have reviewed existing documentation and still have questions or are 60having problems, you can open an issue asking for help. 61 62In exchange for receiving help, we ask that you contribute back a documentation 63PR that helps others avoid the problems that you encountered. 64 65### Submitting a Bug Report 66 67When opening a new issue in the `tonic` issue tracker, users will 68be presented with a [basic template][template] that should be filled in. If you 69believe that you have uncovered a bug, please fill out this form, following the 70template to the best of your ability. Do not worry if you cannot answer every 71detail, just fill in what you can. 72 73The two most important pieces of information we need in order to properly 74evaluate the report is a description of the behavior you are seeing and a simple 75test case we can use to recreate the problem on our own. If we cannot recreate 76the issue, it becomes impossible for us to fix. 77 78In order to rule out the possibility of bugs introduced by userland code, test 79cases should be limited, as much as possible, to using only Tokio APIs. 80 81See [How to create a Minimal, Complete, and Verifiable example][mcve]. 82 83[mcve]: https://stackoverflow.com/help/mcve 84[template]: .github/ISSUE_TEMPLATE/bug_report.md 85 86### Triaging a Bug Report 87 88Once an issue has been opened, it is not uncommon for there to be discussion 89around it. Some contributors may have differing opinions about the issue, 90including whether the behavior being seen is a bug or a feature. This discussion 91is part of the process and should be kept focused, helpful, and professional. 92 93Short, clipped responses—that provide neither additional context nor supporting 94detail—are not helpful or professional. To many, such responses are simply 95annoying and unfriendly. 96 97Contributors are encouraged to help one another make forward progress as much as 98possible, empowering one another to solve issues collaboratively. If you choose 99to comment on an issue that you feel either is not a problem that needs to be 100fixed, or if you encounter information in an issue that you feel is incorrect, 101explain why you feel that way with additional supporting context, and be willing 102to be convinced that you may be wrong. By doing so, we can often reach the 103correct outcome much faster. 104 105### Resolving a Bug Report 106 107In the majority of cases, issues are resolved by opening a Pull Request. The 108process for opening and reviewing a Pull Request is similar to that of opening 109and triaging issues, but carries with it a necessary review and approval 110workflow that ensures that the proposed changes meet the minimal quality and 111functional guidelines of the Tokio project. 112 113## Pull Requests 114 115Pull Requests are the way concrete changes are made to the code, documentation, 116and dependencies in the `tonic` repository. 117 118Even tiny pull requests (e.g., one character pull request fixing a typo in API 119documentation) are greatly appreciated. Before making a large change, it is 120usually a good idea to first open an issue describing the change to solicit 121feedback and guidance. This will increase the likelihood of the PR getting 122merged. 123 124### Tests 125 126If the change being proposed alters code (as opposed to only documentation for 127example), it is either adding new functionality to a crate or it is fixing 128existing, broken functionality. In both of these cases, the pull request should 129include one or more tests to ensure that the crate does not regress in the future. 130There are two ways to write tests: integration tests and documentation tests 131(Tokio avoids unit tests as much as possible). 132 133#### Integration tests 134 135Integration tests go in the same crate as the code they are testing. Each sub 136crate should have a `dev-dependency` on `tonic` itself. This makes all 137`tonic` utilities available to use in tests, no matter the crate being 138tested. 139 140The best strategy for writing a new integration test is to look at existing 141integration tests in the crate and follow the style. 142 143#### Documentation tests 144 145Ideally, every API has at least one [documentation test] that demonstrates how to 146use the API. Documentation tests are run with `cargo test --doc`. This ensures 147that the example is correct and provides additional test coverage. 148 149The trick to documentation tests is striking a balance between being succinct 150for a reader to understand and actually testing the API. 151 152The type level example for `tokio_timer::Timeout` provides a good example of a 153documentation test: 154 155```rust 156/// // import the `timeout` function, usually this is done 157/// // with `use tokio::prelude::*` 158/// use tokio::prelude::FutureExt; 159/// use futures::Stream; 160/// use futures::sync::mpsc; 161/// use std::time::Duration; 162/// 163/// # fn main() { 164/// let (tx, rx) = mpsc::unbounded(); 165/// # tx.unbounded_send(()).unwrap(); 166/// # drop(tx); 167/// 168/// let process = rx.for_each(|item| { 169/// // do something with `item` 170/// # drop(item); 171/// # Ok(()) 172/// }); 173/// 174/// # tokio::runtime::current_thread::block_on_all( 175/// // Wrap the future with a `Timeout` set to expire in 10 milliseconds. 176/// process.timeout(Duration::from_millis(10)) 177/// # ).unwrap(); 178/// # } 179``` 180 181Given that this is a *type* level documentation test and the primary way users 182of `tokio` will create an instance of `Timeout` is by using 183`FutureExt::timeout`, this is how the documentation test is structured. 184 185Lines that start with `/// #` are removed when the documentation is generated. 186They are only there to get the test to run. The `block_on_all` function is the 187easiest way to execute a future from a test. 188 189If this were a documentation test for the `Timeout::new` function, then the 190example would explicitly use `Timeout::new`. For example: 191 192```rust 193/// use tokio::timer::Timeout; 194/// use futures::Future; 195/// use futures::sync::oneshot; 196/// use std::time::Duration; 197/// 198/// # fn main() { 199/// let (tx, rx) = oneshot::channel(); 200/// # tx.send(()).unwrap(); 201/// 202/// # tokio::runtime::current_thread::block_on_all( 203/// // Wrap the future with a `Timeout` set to expire in 10 milliseconds. 204/// Timeout::new(rx, Duration::from_millis(10)) 205/// # ).unwrap(); 206/// # } 207``` 208 209#### Generated code 210 211When making changes to `tonic-build` that affects the generated code you will 212need to ensure that each of the sub crates gets updated as well. Each of the sub 213crates like, for example `tonic-health`, generate their gRPC code via `codegen` 214crate. This requires `Protocol Buffers Compiler` of which version is same as the 215one used in the GitHub Action (see the 216[environment variable `PROTOC_VERSION`](./.github/workflows/CI.yml)). 217 218``` 219cargo run --package codegen 220``` 221 222### Commits 223 224It is a recommended best practice to keep your changes as logically grouped as 225possible within individual commits. There is no limit to the number of commits 226any single Pull Request may have, and many contributors find it easier to review 227changes that are split across multiple commits. 228 229That said, if you have a number of commits that are "checkpoints" and don't 230represent a single logical change, please squash those together. 231 232Note that multiple commits often get squashed when they are landed (see the 233notes about [commit squashing]). 234 235#### Commit message guidelines 236 237A good commit message should describe what changed and why. 238 2391. The first line should: 240 241 * contain a short description of the change (preferably 50 characters or less, 242 and no more than 72 characters) 243 * be entirely in lowercase with the exception of proper nouns, acronyms, and 244 the words that refer to code, like function/variable names 245 * be prefixed with the name of the crate being changed (without the 246 `tonic` prefix) and start with an imperative verb. 247 248 Examples: 249 250 * build: add regex for parsing field filters 251 * tonic: add `Clone` impl for `Service` and `MakeService` 252 2532. Keep the second line blank. 2543. Wrap all other lines at 72 columns (except for long URLs). 2554. If your patch fixes an open issue, you can add a reference to it at the end 256 of the log. Use the `Fixes: #` prefix and the issue number. For other 257 references use `Refs: #`. `Refs` may include multiple issues, separated by a 258 comma. 259 260 Examples: 261 262 - `Fixes: #1337` 263 - `Refs: #1234` 264 265Sample complete commit message: 266 267```txt 268subcrate: explain the commit in one line 269 270Body of commit message is a few lines of text, explaining things 271in more detail, possibly giving some background about the issue 272being fixed, etc. 273 274The body of the commit message can be several paragraphs, and 275please do proper word-wrap and keep columns shorter than about 27672 characters or so. That way, `git log` will show things 277nicely even when it is indented. 278 279Fixes: #1337 280Refs: #453, #154 281``` 282 283### Opening the Pull Request 284 285From within GitHub, opening a new Pull Request will present you with a 286[template] that should be filled out. Please try to do your best at filling out 287the details, but feel free to skip parts if you're not sure what to put. 288 289[template]: .github/PULL_REQUEST_TEMPLATE.md 290 291### Discuss and update 292 293You will probably get feedback or requests for changes to your Pull Request. 294This is a big part of the submission process so don't be discouraged! Some 295contributors may sign off on the Pull Request right away, others may have 296more detailed comments or feedback. This is a necessary part of the process 297in order to evaluate whether the changes are correct and necessary. 298 299**Any community member can review a PR and you might get conflicting feedback**. 300Keep an eye out for comments from code owners to provide guidance on conflicting 301feedback. 302 303**Once the PR is open, do not rebase the commits**. See [Commit Squashing] for 304more details. 305 306### Commit Squashing 307 308In most cases, **do not squash commits that you add to your Pull Request during 309the review process**. When the commits in your Pull Request land, they may be 310squashed into one commit per logical change. Metadata will be added to the 311commit message (including links to the Pull Request, links to relevant issues, 312and the names of the reviewers). The commit history of your Pull Request, 313however, will stay intact on the Pull Request page. 314 315## Reviewing Pull Requests 316 317**Any Tokio and Hyperium community member is welcome to review any pull request**. 318 319All Tokio contributors who choose to review and provide feedback on Pull 320Requests have a responsibility to both the project and the individual making the 321contribution. Reviews and feedback must be helpful, insightful, and geared 322towards improving the contribution as opposed to simply blocking it. If there 323are reasons why you feel the PR should not land, explain what those are. Do not 324expect to be able to block a Pull Request from advancing simply because you say 325"No" without giving an explanation. Be open to having your mind changed. Be open 326to working with the contributor to make the Pull Request better. 327 328Reviews that are dismissive or disrespectful of the contributor or any other 329reviewers are strictly counter to the Code of Conduct. 330 331When reviewing a Pull Request, the primary goals are for the codebase to improve 332and for the person submitting the request to succeed. **Even if a Pull Request 333does not land, the submitters should come away from the experience feeling like 334their effort was not wasted or unappreciated**. Every Pull Request from a new 335contributor is an opportunity to grow the community. 336 337### Review a bit at a time. 338 339Do not overwhelm new contributors. 340 341It is tempting to micro-optimize and make everything about relative performance, 342perfect grammar, or exact style matches. Do not succumb to that temptation. 343 344Focus first on the most significant aspects of the change: 345 3461. Does this change make sense for Tokio? 3472. Does this change make Tokio better, even if only incrementally? 3483. Are there clear bugs or larger scale issues that need attending to? 3494. Is the commit message readable and correct? If it contains a breaking change 350 is it clear enough? 351 352Note that only **incremental** improvement is needed to land a PR. This means 353that the PR does not need to be perfect, only better than the status quo. Follow 354up PRs may be opened to continue iterating. 355 356When changes are necessary, *request* them, do not *demand* them, and **do not 357assume that the submitter already knows how to add a test or run a benchmark**. 358 359Specific performance optimization techniques, coding styles and conventions 360change over time. The first impression you give to a new contributor never does. 361 362Nits (requests for small changes that are not essential) are fine, but try to 363avoid stalling the Pull Request. Most nits can typically be fixed by the Tokio 364Collaborator landing the Pull Request but they can also be an opportunity for 365the contributor to learn a bit more about the project. 366 367It is always good to clearly indicate nits when you comment: e.g. 368`Nit: change foo() to bar(). But this is not blocking.` 369 370If your comments were addressed but were not folded automatically after new 371commits or if they proved to be mistaken, please, [hide them][hiding-a-comment] 372with the appropriate reason to keep the conversation flow concise and relevant. 373 374### Be aware of the person behind the code 375 376Be aware that *how* you communicate requests and reviews in your feedback can 377have a significant impact on the success of the Pull Request. Yes, we may land 378a particular change that makes `tonic` better, but the individual might 379just not want to have anything to do with `tonic` ever again. The goal is 380not just having good code. 381 382### Abandoned or Stalled Pull Requests 383 384If a Pull Request appears to be abandoned or stalled, it is polite to first 385check with the contributor to see if they intend to continue the work before 386checking if they would mind if you took it over (especially if it just has nits 387left). When doing so, it is courteous to give the original contributor credit 388for the work they started (either by preserving their name and email address in 389the commit log, or by using an `Author: ` meta-data tag in the commit. 390 391_Adapted from the [Node.js contributing guide][node]_. 392 393[node]: https://github.com/nodejs/node/blob/master/CONTRIBUTING.md 394[hiding-a-comment]: https://help.github.com/articles/managing-disruptive-comments/#hiding-a-comment 395[documentation test]: https://doc.rust-lang.org/rustdoc/documentation-tests.html 396 397## Releasing 398 399Since the Tonic project consists of a number of crates, many of which depend on 400each other, releasing new versions to crates.io can involve some complexities. 401When releasing a new version of a crate, follow these steps: 402 4031. **Ensure that the release crate has no path dependencies.** When the HEAD 404 version of a Tonic crate requires unreleased changes in another Tonic crate, 405 the crates.io dependency on the second crate will be replaced with a path 406 dependency. Crates with path dependencies cannot be published, so before 407 publishing the dependent crate, any path dependencies must also be published. 408 This should be done through a form of depth-first tree traversal: 409 410 1. Starting with the first path dependency in the crate to be released, 411 inspect the `Cargo.toml` for the dependency. If the dependency has any 412 path dependencies of its own, repeat this step with the first such 413 dependency. 414 2. Begin the release process for the path dependency. 415 3. Once the path dependency has been published to crates.io, update the 416 dependent crate to depend on the crates.io version. 417 4. When all path dependencies have been published, the dependent crate may 418 be published. 419 420 To verify that a crate is ready to publish, run: 421 422 ```bash 423 cd <CRATE NAME> 424 cargo publish --dry-run -p <CRATE NAME> 425 ``` 426 4272. **Update Cargo metadata.** After releasing any path dependencies, update the 428 `version` field in `Cargo.toml` to the new version, and the `documentation` 429 field to the docs.rs URL of the new version. 4303. **Update other documentation links.** Update the `#![doc(html_root_url)]` 431 attribute in the crate's `lib.rs` and the "Documentation" link in the crate's 432 `README.md` to point to the docs.rs URL of the new version. 4334. **Update the changelog for the crate.** Each crate in the Tokio repository 434 has its own `CHANGELOG.md` in that crate's subdirectory. Any changes to that 435 crate since the last release should be added to the changelog. Change 436 descriptions may be taken from the Git history, but should be edited to 437 ensure a consistent format, based on [Keep A Changelog][keep-a-changelog]. 438 Other entries in that crate's changelog may also be used for reference. 4395. **Perform a final audit for breaking changes.** Compare the HEAD version of 440 crate with the Git tag for the most recent release version. If there are any 441 breaking API changes, determine if those changes can be made without breaking 442 existing APIs. If so, resolve those issues. Otherwise, if it is necessary to 443 make a breaking release, update the version numbers to reflect this. 4446. **Open a pull request with your changes.** Once that pull request has been 445 approved by a maintainer and the pull request has been merged, continue to 446 the next step. 4477. **Release the crate.** Run the following command: 448 449 ```bash 450 cd <CRATE NAME> 451 cargo publish --dry-run -p <CRATE NAME> 452 ``` 453 454 Your editor and prompt you to edit a message for the tag. Copy the changelog 455 entry for that release version into your editor and close the window. 456 457[keep-a-changelog]: https://github.com/olivierlacan/keep-a-changelog/blob/master/CHANGELOG.md 458