|
| 1 | +# Crater |
| 2 | + |
| 3 | +[Crater](https://github.com/rust-lang/crater) is a tool for compiling and running tests for _every_ crate on [crates.io](https://crates.io) (and a few on GitHub). |
| 4 | +It is mainly used for checking the extent of breakage when implementing potentially breaking changes and ensuring lack of breakage by running beta vs stable compiler versions. |
| 5 | + |
| 6 | +Essentially it runs some `cargo` command on every crate twice; once against the "start" toolchain and again against the "end" toolchain. |
| 7 | +For example, "start" could be the stable release, and "end" could be beta. |
| 8 | +If it passes in "start" but fails with "end", then that is reported as a regression. |
| 9 | + |
| 10 | +There is a bot called [craterbot] which is used to run crater on hardware managed by the rust-lang organization. |
| 11 | + |
| 12 | +Crater is run by the release team during the beta cycle. |
| 13 | +If there are any regressions that look like they are caused by Cargo, they should contact the Cargo team to decide how to handle it. |
| 14 | + |
| 15 | +## Running crater |
| 16 | + |
| 17 | +If you have a change that you want to test before the beta release, or you want to test behavior that is not normally exercised by crater, you can do a manual run of crater. |
| 18 | +Roughly the steps are: |
| 19 | + |
| 20 | +1. Create a branch with your changes. |
| 21 | + |
| 22 | + In your clone of cargo, make the changes to incorporate whatever new thing you want to test and push it to a branch on your fork on GitHub. |
| 23 | + |
| 24 | +2. Get a clone of <https://github.com/rust-lang/rust> |
| 25 | + |
| 26 | +3. Create a branch in your rust-lang/rust clone to add your changes. |
| 27 | + |
| 28 | +4. Change the `src/tools/cargo` submodule to point to your new branch. |
| 29 | + |
| 30 | + Modify `.gitmodules` to point to your clone and branch of cargo with the changes you want to test. |
| 31 | + For example: |
| 32 | + |
| 33 | + ```bash |
| 34 | + git submodule set-url src/tools/cargo https://github.com/ehuss/cargo.git |
| 35 | + git submodule set-branch --branch my-awesome-feature src/tools/cargo |
| 36 | + git submodule update --remote src/tools/cargo |
| 37 | + git add .gitmodules src/tools/cargo |
| 38 | + git commit |
| 39 | + ``` |
| 40 | + |
| 41 | +5. Create an PR on rust-lang/rust. |
| 42 | + |
| 43 | + Push your submodule changes to GitHub and make a PR. |
| 44 | + Start the PR title with `[EXPERIMENT]` to make it clear what the PR is for and assign yourself or @ghost. |
| 45 | + |
| 46 | +6. Make a "try" build. |
| 47 | + |
| 48 | + A "try" build creates a full release of x86_64-unknown-linux-gnu and stores it on rust-lang servers. |
| 49 | + This can be done with a comment `@bors try` on the PR (all Cargo team members should have permission to do this). |
| 50 | + |
| 51 | +7. Run crater. |
| 52 | + |
| 53 | + Look at the [craterbot] docs to determine the command that you want to run. |
| 54 | + There are different modes like `check-only`, `build-and-test`, `rustdoc`, etc. |
| 55 | + |
| 56 | + You can also choose how many crates to run against. |
| 57 | + If you are uncertain if your cargo changes will work correctly, it might be a good idea to run against `top-100` first to check its behavior. |
| 58 | + This will run much faster. |
| 59 | + You can do a full run afterwards. |
| 60 | + |
| 61 | + After the try build finishes (which should take a couple hours), ask someone to make a crater run. |
| 62 | + The Cargo team does not have that permission, so just ask someone on Zulip. |
| 63 | + They will need to write a comment to `@craterbot` with the command that you have specified. |
| 64 | + |
| 65 | +8. Wait. |
| 66 | + |
| 67 | + Crater can take anywhere from a few hours to a few weeks to run depending on how long the [craterbot queue](https://crater.rust-lang.org/) is and which mode you picked and the priority of your job. |
| 68 | + When the crater run finishes, craterbot will post a comment to the PR with a link to a report of the results. |
| 69 | + |
| 70 | +9. Investigate the report. |
| 71 | + |
| 72 | + Look through the report which contains links to build logs for any regressions or errors. |
| 73 | + |
| 74 | +10. Close the PR. |
| 75 | + |
| 76 | + Whenever you are done doing crater runs, close your PR. |
| 77 | + |
| 78 | +[craterbot]: https://github.com/rust-lang/crater/blob/master/docs/bot-usage.md |
| 79 | + |
| 80 | + |
| 81 | +## Advanced crater modes |
| 82 | + |
| 83 | +Crater only has a few built-in modes, such as running `cargo check` or `cargo test`. |
| 84 | +You can pass extra flags with `+cargoflags`. |
| 85 | + |
| 86 | +More complex tests can be accomplished by customizing Cargo to perform whatever actions you want. |
| 87 | +Since crater essentially runs `cargo check`, you can modify the `check` command to perform whichever actions you want. |
| 88 | +For example, to test `cargo fix --edition`, [this commit](https://github.com/ehuss/cargo/commit/6901690a6f8d519efb4fabf48c1c2b94af0c3bd8) intercepted `cargo check` and modified it to instead: |
| 89 | + |
| 90 | +1. Only run on crates with the 2018 edition. |
| 91 | +2. Run `cargo fix --edition`. |
| 92 | +3. Modify the manifest to switch to the 2021 edition. |
| 93 | +4. Run `cargo check` to verify. |
| 94 | + |
| 95 | +If you need to compare the before and after of a command that is not part of crater's built-in modes, that can be more difficult. |
| 96 | +Two possible options: |
| 97 | + |
| 98 | +* Work with the infra team to add a new mode. |
| 99 | +* Build two custom try builds. |
| 100 | + Each one should modify the `cargo check` command as described above. |
| 101 | + The "start" build should perform whichever action you want with an otherwise unmodified cargo. |
| 102 | + The "end" build should perform whichever action you want with your modified cargo. |
| 103 | + Then, in the `@craterbot` command, specify the start and end hashes of the two try builds. |
| 104 | + |
| 105 | +## Limitations |
| 106 | + |
| 107 | +There are some limitations of crater to consider when running Cargo: |
| 108 | + |
| 109 | +* A crater run without regressions is not a green light to move forward. |
| 110 | + * A large portion of Rust code is not tested, such as closed-source projects or things otherwise not collected by crater. |
| 111 | + * Many crates can't build in crater's environment or are otherwise broken. |
| 112 | + * Some crates have flaky tests. |
| 113 | +* Crater runs in an isolated environment. |
| 114 | + * It only runs on Linux x86-64. |
| 115 | + * It does not have network access. |
| 116 | + * The crate source is in a read-only mount. |
| 117 | +* Crater does several steps before running the test (using its own copy of the stable toolchain): |
| 118 | + * It generates a lockfile using `generate-lockfile` and includes `-Zno-index-update` to prevent index updates (which makes it run much faster). |
| 119 | + * All dependencies are downloaded ahead-of-time with `cargo fetch`. |
| 120 | +* The built-in modes pass several flags to cargo such as `--frozen` or `--message-format=json`. |
| 121 | + It will sometimes use `--all-targets` and sometimes not. |
| 122 | + Check the [crater source](https://github.com/rust-lang/crater/blob/master/src/runner/test.rs) for more details on how it works. |
0 commit comments