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### Commits 210 211It is a recommended best practice to keep your changes as logically grouped as 212possible within individual commits. There is no limit to the number of commits 213any single Pull Request may have, and many contributors find it easier to review 214changes that are split across multiple commits. 215 216That said, if you have a number of commits that are "checkpoints" and don't 217represent a single logical change, please squash those together. 218 219Note that multiple commits often get squashed when they are landed (see the 220notes about [commit squashing]). 221 222#### Commit message guidelines 223 224A good commit message should describe what changed and why. 225 2261. The first line should: 227 228 * contain a short description of the change (preferably 50 characters or less, 229 and no more than 72 characters) 230 * be entirely in lowercase with the exception of proper nouns, acronyms, and 231 the words that refer to code, like function/variable names 232 * be prefixed with the name of the crate being changed (without the 233 `tonic` prefix) and start with an imperative verb. 234 235 Examples: 236 237 * build: add regex for parsing field filters 238 * tonic: add `Clone` impl for `Service` and `MakeService` 239 2402. Keep the second line blank. 2413. Wrap all other lines at 72 columns (except for long URLs). 2424. If your patch fixes an open issue, you can add a reference to it at the end 243 of the log. Use the `Fixes: #` prefix and the issue number. For other 244 references use `Refs: #`. `Refs` may include multiple issues, separated by a 245 comma. 246 247 Examples: 248 249 - `Fixes: #1337` 250 - `Refs: #1234` 251 252Sample complete commit message: 253 254```txt 255subcrate: explain the commit in one line 256 257Body of commit message is a few lines of text, explaining things 258in more detail, possibly giving some background about the issue 259being fixed, etc. 260 261The body of the commit message can be several paragraphs, and 262please do proper word-wrap and keep columns shorter than about 26372 characters or so. That way, `git log` will show things 264nicely even when it is indented. 265 266Fixes: #1337 267Refs: #453, #154 268``` 269 270### Opening the Pull Request 271 272From within GitHub, opening a new Pull Request will present you with a 273[template] that should be filled out. Please try to do your best at filling out 274the details, but feel free to skip parts if you're not sure what to put. 275 276[template]: .github/PULL_REQUEST_TEMPLATE.md 277 278### Discuss and update 279 280You will probably get feedback or requests for changes to your Pull Request. 281This is a big part of the submission process so don't be discouraged! Some 282contributors may sign off on the Pull Request right away, others may have 283more detailed comments or feedback. This is a necessary part of the process 284in order to evaluate whether the changes are correct and necessary. 285 286**Any community member can review a PR and you might get conflicting feedback**. 287Keep an eye out for comments from code owners to provide guidance on conflicting 288feedback. 289 290**Once the PR is open, do not rebase the commits**. See [Commit Squashing] for 291more details. 292 293### Commit Squashing 294 295In most cases, **do not squash commits that you add to your Pull Request during 296the review process**. When the commits in your Pull Request land, they may be 297squashed into one commit per logical change. Metadata will be added to the 298commit message (including links to the Pull Request, links to relevant issues, 299and the names of the reviewers). The commit history of your Pull Request, 300however, will stay intact on the Pull Request page. 301 302## Reviewing Pull Requests 303 304**Any Tokio and Hyperium community member is welcome to review any pull request**. 305 306All Tokio contributors who choose to review and provide feedback on Pull 307Requests have a responsibility to both the project and the individual making the 308contribution. Reviews and feedback must be helpful, insightful, and geared 309towards improving the contribution as opposed to simply blocking it. If there 310are reasons why you feel the PR should not land, explain what those are. Do not 311expect to be able to block a Pull Request from advancing simply because you say 312"No" without giving an explanation. Be open to having your mind changed. Be open 313to working with the contributor to make the Pull Request better. 314 315Reviews that are dismissive or disrespectful of the contributor or any other 316reviewers are strictly counter to the Code of Conduct. 317 318When reviewing a Pull Request, the primary goals are for the codebase to improve 319and for the person submitting the request to succeed. **Even if a Pull Request 320does not land, the submitters should come away from the experience feeling like 321their effort was not wasted or unappreciated**. Every Pull Request from a new 322contributor is an opportunity to grow the community. 323 324### Review a bit at a time. 325 326Do not overwhelm new contributors. 327 328It is tempting to micro-optimize and make everything about relative performance, 329perfect grammar, or exact style matches. Do not succumb to that temptation. 330 331Focus first on the most significant aspects of the change: 332 3331. Does this change make sense for Tokio? 3342. Does this change make Tokio better, even if only incrementally? 3353. Are there clear bugs or larger scale issues that need attending to? 3364. Is the commit message readable and correct? If it contains a breaking change 337 is it clear enough? 338 339Note that only **incremental** improvement is needed to land a PR. This means 340that the PR does not need to be perfect, only better than the status quo. Follow 341up PRs may be opened to continue iterating. 342 343When changes are necessary, *request* them, do not *demand* them, and **do not 344assume that the submitter already knows how to add a test or run a benchmark**. 345 346Specific performance optimization techniques, coding styles and conventions 347change over time. The first impression you give to a new contributor never does. 348 349Nits (requests for small changes that are not essential) are fine, but try to 350avoid stalling the Pull Request. Most nits can typically be fixed by the Tokio 351Collaborator landing the Pull Request but they can also be an opportunity for 352the contributor to learn a bit more about the project. 353 354It is always good to clearly indicate nits when you comment: e.g. 355`Nit: change foo() to bar(). But this is not blocking.` 356 357If your comments were addressed but were not folded automatically after new 358commits or if they proved to be mistaken, please, [hide them][hiding-a-comment] 359with the appropriate reason to keep the conversation flow concise and relevant. 360 361### Be aware of the person behind the code 362 363Be aware that *how* you communicate requests and reviews in your feedback can 364have a significant impact on the success of the Pull Request. Yes, we may land 365a particular change that makes `tonic` better, but the individual might 366just not want to have anything to do with `tonic` ever again. The goal is 367not just having good code. 368 369### Abandoned or Stalled Pull Requests 370 371If a Pull Request appears to be abandoned or stalled, it is polite to first 372check with the contributor to see if they intend to continue the work before 373checking if they would mind if you took it over (especially if it just has nits 374left). When doing so, it is courteous to give the original contributor credit 375for the work they started (either by preserving their name and email address in 376the commit log, or by using an `Author: ` meta-data tag in the commit. 377 378_Adapted from the [Node.js contributing guide][node]_. 379 380[node]: https://github.com/nodejs/node/blob/master/CONTRIBUTING.md 381[hiding-a-comment]: https://help.github.com/articles/managing-disruptive-comments/#hiding-a-comment 382[documentation test]: https://doc.rust-lang.org/rustdoc/documentation-tests.html 383 384## Releasing 385 386Since the Tonic project consists of a number of crates, many of which depend on 387each other, releasing new versions to crates.io can involve some complexities. 388When releasing a new version of a crate, follow these steps: 389 3901. **Ensure that the release crate has no path dependencies.** When the HEAD 391 version of a Tonic crate requires unreleased changes in another Tonic crate, 392 the crates.io dependency on the second crate will be replaced with a path 393 dependency. Crates with path dependencies cannot be published, so before 394 publishing the dependent crate, any path dependencies must also be published. 395 This should be done through a form of depth-first tree traversal: 396 397 1. Starting with the first path dependency in the crate to be released, 398 inspect the `Cargo.toml` for the dependency. If the dependency has any 399 path dependencies of its own, repeat this step with the first such 400 dependency. 401 2. Begin the release process for the path dependency. 402 3. Once the path dependency has been published to crates.io, update the 403 dependent crate to depend on the crates.io version. 404 4. When all path dependencies have been published, the dependent crate may 405 be published. 406 407 To verify that a crate is ready to publish, run: 408 409 ```bash 410 cd <CRATE NAME> 411 cargo publish --dry-run -p <CRATE NAME> 412 ``` 413 4142. **Update Cargo metadata.** After releasing any path dependencies, update the 415 `version` field in `Cargo.toml` to the new version, and the `documentation` 416 field to the docs.rs URL of the new version. 4173. **Update other documentation links.** Update the `#![doc(html_root_url)]` 418 attribute in the crate's `lib.rs` and the "Documentation" link in the crate's 419 `README.md` to point to the docs.rs URL of the new version. 4204. **Update the changelog for the crate.** Each crate in the Tokio repository 421 has its own `CHANGELOG.md` in that crate's subdirectory. Any changes to that 422 crate since the last release should be added to the changelog. Change 423 descriptions may be taken from the Git history, but should be edited to 424 ensure a consistent format, based on [Keep A Changelog][keep-a-changelog]. 425 Other entries in that crate's changelog may also be used for reference. 4265. **Perform a final audit for breaking changes.** Compare the HEAD version of 427 crate with the Git tag for the most recent release version. If there are any 428 breaking API changes, determine if those changes can be made without breaking 429 existing APIs. If so, resolve those issues. Otherwise, if it is necessary to 430 make a breaking release, update the version numbers to reflect this. 4316. **Open a pull request with your changes.** Once that pull request has been 432 approved by a maintainer and the pull request has been merged, continue to 433 the next step. 4347. **Release the crate.** Run the following command: 435 436 ```bash 437 cd <CRATE NAME> 438 cargo publish --dry-run -p <CRATE NAME> 439 ``` 440 441 Your editor and prompt you to edit a message for the tag. Copy the changelog 442 entry for that release version into your editor and close the window. 443 444[keep-a-changelog]: https://github.com/olivierlacan/keep-a-changelog/blob/master/CHANGELOG.md 445