blob: ce7c5d8d1e6b1ba8cac52ec1275404ff15f43c8f [file] [log] [blame] [view]
Lukasz Anforowiczceddb032024-02-21 16:39:231# Updating Rust crates used by Chromium
2
3This document describes how Chromium updates crates.io Rust crates that Chromium
4depends on.
5
Lukasz Anforowicz121edc92024-03-20 18:24:336We have a weekly rotation (go/chromium-crates-update-rotation) of engineers
7responsible for creating and landing CLs that update Rust crates.
8
9## Initial setup
10
11The "Rust: periodic update of 3rd-party crates" rotation requires access to an
12up-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
14work - e.g. ones based on `git-new-workdir`).
15
16## Automated step: `create_update_cl.py`
17
18The first actual step of the rotation is manually running the
19`tools/crates/create_update_cl.py` script.
Lukasz Anforowiczceddb032024-02-21 16:39:2320
Lukasz Anforowicz52258d12024-03-05 20:23:3621`//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 Anforowiczceddb032024-02-21 16:39:2324The script depends on `depot_tools` and `git` being present in the `PATH`.
Lukasz Anforowicz121edc92024-03-20 18:24:3325Depending on how many crates are updated, the script may need 10-15 minutes to
26run.
Lukasz Anforowiczceddb032024-02-21 16:39:2327
Lukasz Anforowicz52258d12024-03-05 20:23:3628Before the auto-generated CLs can be landed, some additional manual steps need
Lukasz Anforowicz121edc92024-03-20 18:24:3329to be done first - see the sections below.
Lukasz Anforowiczceddb032024-02-21 16:39:2330
Lukasz Anforowicz121edc92024-03-20 18:24:3331## Manual step: `run_cargo_vet.py`
Lukasz Anforowiczceddb032024-02-21 16:39:2332
33The changes in the auto-generated CL need to go through a security audit, which
34will ensure that `cargo vet` criteria (e.g. `ub-risk-0`, `safe-to-deploy`,
Lukasz Anforowicz52258d12024-03-05 20:23:3635etc.). still hold for the new versions. The CL description specifies which
Lukasz Anforowicz121edc92024-03-20 18:24:3336criteria apply to the updated crates.
37See the `//docs/rust-unsafe.md` doc for details on how to audit and certify
38the new crate versions (this may require looping in `unsafe` Rust experts
39and/or cryptography experts).
Lukasz Anforowiczceddb032024-02-21 16:39:2340
Lukasz Anforowicz121edc92024-03-20 18:24:3341For each update CL, there is a separate git branch created. An audit will most
42likely need to be recorded in
43`third_party/rust/chromium_crates_io/supply-chain/audits.toml` and committed to
44the git branch for each update CL. There are some known corner cases where
45`audits.toml` changes are not needed:
Lukasz Anforowiczceddb032024-02-21 16:39:2346
Lukasz Anforowicz121edc92024-03-20 18:24:3347* 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 Anforowiczceddb032024-02-21 16:39:2365
Lukasz Anforowicz121edc92024-03-20 18:24:3366This step may require one or more of the commands below, for each git branch
67associated with an update CL (starting with the earliest branches - ones
68closest to `origin/main`):
Lukasz Anforowiczceddb032024-02-21 16:39:2369
Lukasz Anforowicz121edc92024-03-20 18:24:33701. `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
741. 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
841. Follow the cargo vet instructions to inspect diffs and certify the results
851. `git add third_party/rust/chromium_crates_io/supply-chain`.
86 `git commit -m 'cargo vet'`
Lukasz Anforowiczceddb032024-02-21 16:39:23871. `git cl upload -m 'cargo vet'`
88
Lukasz Anforowicz121edc92024-03-20 18:24:3389## Potential additional steps
Lukasz Anforowiczceddb032024-02-21 16:39:2390
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 Anforowicz121edc92024-03-20 18:24:3395* 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 Anforowicz52258d12024-03-05 20:23:36101
Lukasz Anforowicz121edc92024-03-20 18:24:33102## Landing the CL
Lukasz Anforowiczceddb032024-02-21 16:39:23103
104Other than the above, the CL can go through the normal, plain-vanilla, manual
105review and landing process.
106
1071. `git cl upload`
1081. Get a review from one of `//third_party/rust/OWNERS`
1091. Land the CL using CQ+2
Lukasz Anforowicz67783682024-03-22 16:24:49110
111## Checking for new major versions
112
113Note that `create_update_cl.py` will only handle minor version changes (e.g.
114123.1 => 123.2, or 0.123.1 => 0.123.2). Major version changes (e.g. 1.0 => 2.0,
115which may include breaking API changes and other breaking changes) need to be
116handled separately.
117
118As part of the rotation, one should attempt to check for new major versions of
119direct Chromium dependencies (i.e. dependencies directly listed in
120`third_party/rust/chromium_crates_io/Cargo.toml`). To discover direct _and_
121transitive 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
123updates):
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
135If the updating to a new major version doesn't require lots of Chromium changes,
136then it may be possible to land the update in a single CL. This is typically
137possible when the APIs affected by the major version's breaking change either
138weren'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
141introduce breaking changes in the _behavior_ of the existing APIs.
142
143To update:
144
1451. Edit `third_party/rust/chromium_crates_io/Cargo.toml` to change the version
146 being depended upon.
1471. 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
1511. Remove the old version's leftover files in `//third_party/rust/<crate>/<old
152 epoch>`
153
154### Workflow B: Incremental transition
155
156When lots of first-party code depends on the old major version, then the
157transition to the new major version may need to be done incrementally. In this
158case the transition can be split into the following steps:
159
1601. 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
1631. 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).
1681. Incrementally transition first-party code to the new major version
1691. 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
174Note that the following `Cargo.toml` syntax allows two versions of a crate to
175coexist:
176
177```
178[dependencies.serde_json_lenient_old_epoch]
179package = "serde_json_lenient"
180version = "0.1"
181
182[dependencies.serde_json_lenient]
183version = "0.2"
184```