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