Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 1 | # Updating Rust crates used by Chromium |
| 2 | |
| 3 | This document describes how Chromium updates crates.io Rust crates that Chromium |
| 4 | depends on. |
| 5 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 6 | We have a weekly rotation (go/chromium-crates-update-rotation) of engineers |
| 7 | responsible for creating and landing CLs that update Rust crates. |
| 8 | |
| 9 | ## Initial setup |
| 10 | |
| 11 | The "Rust: periodic update of 3rd-party crates" rotation requires access to an |
| 12 | up-to-date Chromium repo. One way to start a shift is to run `git fetch`, |
| 13 | `git checkout origin/main`, and `gclient sync` (but other workflows should also |
| 14 | work - e.g. ones based on `git-new-workdir`). |
| 15 | |
| 16 | ## Automated step: `create_update_cl.py` |
| 17 | |
| 18 | The first actual step of the rotation is manually running the |
| 19 | `tools/crates/create_update_cl.py` script. |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 20 | |
Lukasz Anforowicz | 52258d1 | 2024-03-05 20:23:36 | [diff] [blame] | 21 | `//tools/crates/create_update_cl.py` runs `gnrt update`, `gnrt vendor`, and |
| 22 | `gnrt gen`, and then uploads zero, one, or more resulting CLs to Gerrit. |
| 23 | `create_update_cl.py` has to be invoked from within a Chromium repo. |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 24 | The script depends on `depot_tools` and `git` being present in the `PATH`. |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 25 | Depending on how many crates are updated, the script may need 10-15 minutes to |
| 26 | run. |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 27 | |
Lukasz Anforowicz | 52258d1 | 2024-03-05 20:23:36 | [diff] [blame] | 28 | Before the auto-generated CLs can be landed, some additional manual steps need |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 29 | to be done first - see the sections below. |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 30 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 31 | ## Manual step: `run_cargo_vet.py` |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 32 | |
| 33 | The changes in the auto-generated CL need to go through a security audit, which |
| 34 | will ensure that `cargo vet` criteria (e.g. `ub-risk-0`, `safe-to-deploy`, |
Lukasz Anforowicz | 52258d1 | 2024-03-05 20:23:36 | [diff] [blame] | 35 | etc.). still hold for the new versions. The CL description specifies which |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 36 | criteria apply to the updated crates. |
| 37 | See the `//docs/rust-unsafe.md` doc for details on how to audit and certify |
| 38 | the new crate versions (this may require looping in `unsafe` Rust experts |
| 39 | and/or cryptography experts). |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 40 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 41 | For each update CL, there is a separate git branch created. An audit will most |
| 42 | likely need to be recorded in |
| 43 | `third_party/rust/chromium_crates_io/supply-chain/audits.toml` and committed to |
| 44 | the git branch for each update CL. There are some known corner cases where |
| 45 | `audits.toml` changes are not needed: |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 46 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 47 | * Updates of crates listed in `remove_crates` in |
| 48 | `third_party/rust/chromium_crates_io/gnrt_config.toml` (e.g. the `cc` crate |
| 49 | which is a dependency of `cxx` but is not actually used/needed in Chromium). |
| 50 | This case should be recognized by the `create_update_cl.py` script and noted |
| 51 | in the CL description. |
| 52 | * Updates of grand-parented-in crates that are covered by exemptions in |
| 53 | `third_party/rust/chromium_crates_io/supply-chain/config.toml` instead of |
| 54 | being covered by real audits from `audits.toml`. For such crates, skim the |
| 55 | delta and use your best judgement on whether to bump the crate version that |
| 56 | the exemption applies to. Note that `supply-chain/config.toml` is generated |
| 57 | by `gnrt vendor` and should not be edited directly - please instead edit |
| 58 | `third_party/rust/chromium_crates_io/vet_config.toml.hbs` and then run |
| 59 | `tools/crates/run_gnrt.py vendor` to regenerate `supply-chain/config.toml`. |
| 60 | * Update to a crate version that is already covered by `audits.toml` of other |
| 61 | projects that Chromium's `run_cargo_vet.py` imports. In such case (but only |
| 62 | once https://crrev.com/c/5368743 lands) you may |
| 63 | need to commit changes that `cargo vet` generates in |
| 64 | `third_party/rust/chromium_crates_io/supply-chain/imports.lock`. |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 65 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 66 | This step may require one or more of the commands below, for each git branch |
| 67 | associated with an update CL (starting with the earliest branches - ones |
| 68 | closest to `origin/main`): |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 69 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 70 | 1. `git checkout rust-crates-update--...` |
| 71 | - If this is the second or subsequent branch, then also run `git rebase` to |
| 72 | rebase it on top of the manual `audits.toml` changes in the upstream |
| 73 | branches |
| 74 | 1. Check which crate (or crates) and which audit criteria need to be reviewed in |
| 75 | this branch / CL: `tools/crates/run_cargo_vet.py check` |
| 76 | - Note that `run_cargo_vet.py check` may list a _subset_ of the criteria |
| 77 | that the automated script has listed in the CL description (e.g. if some |
| 78 | of the criteria are already covered by `audits.toml` imported from other |
| 79 | projects). |
| 80 | - Install `cargo vet` if it's not yet installed: |
| 81 | * `tools/crates/run_cargo.py install cargo-vet --locked --version=0.9.1` |
| 82 | * TODO: Pre-package `cargo-vet` into `rust-toolchain`: |
| 83 | https://crrev.com/c/5366668 |
| 84 | 1. Follow the cargo vet instructions to inspect diffs and certify the results |
| 85 | 1. `git add third_party/rust/chromium_crates_io/supply-chain`. |
| 86 | `git commit -m 'cargo vet'` |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 87 | 1. `git cl upload -m 'cargo vet'` |
| 88 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 89 | ## Potential additional steps |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 90 | |
| 91 | * If updating `cxx`, you may need to also update its version in: |
| 92 | - `build/rust/BUILD.gn` |
| 93 | - `third_party/rust/cxx/v1/cxx.h` |
| 94 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 95 | * The `create_update_cl.py` script may stop early if it detects that `gnrt |
| 96 | vendor` or `gnrt gen` have reported any warnings or errors (e.g. a "License |
| 97 | file not found for crate foo" warning). In this case, manual intervention is |
| 98 | needed to finish the update CL. It's probably best to finish and land the CLs |
| 99 | created so far before trying to restart the script in order to create the |
| 100 | remaining CLs. |
Lukasz Anforowicz | 52258d1 | 2024-03-05 20:23:36 | [diff] [blame] | 101 | |
Lukasz Anforowicz | 121edc9 | 2024-03-20 18:24:33 | [diff] [blame] | 102 | ## Landing the CL |
Lukasz Anforowicz | ceddb03 | 2024-02-21 16:39:23 | [diff] [blame] | 103 | |
| 104 | Other than the above, the CL can go through the normal, plain-vanilla, manual |
| 105 | review and landing process. |
| 106 | |
| 107 | 1. `git cl upload` |
| 108 | 1. Get a review from one of `//third_party/rust/OWNERS` |
| 109 | 1. Land the CL using CQ+2 |
Lukasz Anforowicz | 6778368 | 2024-03-22 16:24:49 | [diff] [blame^] | 110 | |
| 111 | ## Checking for new major versions |
| 112 | |
| 113 | Note that `create_update_cl.py` will only handle minor version changes (e.g. |
| 114 | 123.1 => 123.2, or 0.123.1 => 0.123.2). Major version changes (e.g. 1.0 => 2.0, |
| 115 | which may include breaking API changes and other breaking changes) need to be |
| 116 | handled separately. |
| 117 | |
| 118 | As part of the rotation, one should attempt to check for new major versions of |
| 119 | direct Chromium dependencies (i.e. dependencies directly listed in |
| 120 | `third_party/rust/chromium_crates_io/Cargo.toml`). To discover direct _and_ |
| 121 | transitive dependencies with a new major version, you can use the command below |
| 122 | (running it in the final update CL branch - after all the minor version |
| 123 | updates): |
| 124 | |
| 125 | ``` |
| 126 | $ tools/crates/run_cargo.py -Zunstable-options -C third_party/rust/chromium_crates_io -Zbindeps update --dry-run --verbose |
| 127 | ... |
| 128 | Unchanged serde_json_lenient v0.1.8 (latest: v0.2.0) |
| 129 | Unchanged syn v1.0.109 (latest: v2.0.53) |
| 130 | ... |
| 131 | ``` |
| 132 | |
| 133 | ### Workflow A: Single update CL |
| 134 | |
| 135 | If the updating to a new major version doesn't require lots of Chromium changes, |
| 136 | then it may be possible to land the update in a single CL. This is typically |
| 137 | possible when the APIs affected by the major version's breaking change either |
| 138 | weren't used by Chromium, or were used only in a handful of places. |
| 139 | |
| 140 | **Warning**: Sometimes a new major version may be API compatible, but may |
| 141 | introduce breaking changes in the _behavior_ of the existing APIs. |
| 142 | |
| 143 | To update: |
| 144 | |
| 145 | 1. Edit `third_party/rust/chromium_crates_io/Cargo.toml` to change the version |
| 146 | being depended upon. |
| 147 | 1. Follow other steps described in |
| 148 | [`docs/rust.md`](../../docs/rust.md#importing-a-crate-from-crates_io) for |
| 149 | importing a new crate (e.g. `gnrt vendor`, `gnrt gen`, etc.) |
| 150 | - TODO(https://crbug.com/330729319): Try to automate some parts of this step |
| 151 | 1. Remove the old version's leftover files in `//third_party/rust/<crate>/<old |
| 152 | epoch>` |
| 153 | |
| 154 | ### Workflow B: Incremental transition |
| 155 | |
| 156 | When lots of first-party code depends on the old major version, then the |
| 157 | transition to the new major version may need to be done incrementally. In this |
| 158 | case the transition can be split into the following steps: |
| 159 | |
| 160 | 1. Open a new bug to track the transition |
| 161 | - TODO: Figure out how to tag/format the bug to make it easy to discover |
| 162 | in future rotations |
| 163 | 1. Land the new major version, so that the old and the new versions coexist. |
| 164 | To do this follow the process for importing a new crate as described in |
| 165 | [`docs/rust.md`](../../docs/rust.md#importing-a-crate-from-crates_io) |
| 166 | (i.e. edit `Cargo.toml` to add the new version, run `gnrt vendor`, and so |
| 167 | forth). |
| 168 | 1. Incrementally transition first-party code to the new major version |
| 169 | 1. Remove the old major version. To do this follow a similar process as above |
| 170 | (i.e. edit `Cargo.toml` to remove the old version, run `gnrt vendor`, and so |
| 171 | forth). Any leftover files in `//third_party/rust/<crate>/<old epoch>` |
| 172 | should also be removed. |
| 173 | |
| 174 | Note that the following `Cargo.toml` syntax allows two versions of a crate to |
| 175 | coexist: |
| 176 | |
| 177 | ``` |
| 178 | [dependencies.serde_json_lenient_old_epoch] |
| 179 | package = "serde_json_lenient" |
| 180 | version = "0.1" |
| 181 | |
| 182 | [dependencies.serde_json_lenient] |
| 183 | version = "0.2" |
| 184 | ``` |