Repository: rust-lang/rust
Pull request: bootstrap: Allow `--bless`ing changes to editor settings files

Previously, on any change you would have to first edit all 4 settings files by hand, then run the tests 4 times in a row to discover what the new hashes are. After this change, you still need to edit the files by hand, but you can now run `x test --bless -- hash` to update the hashes without manually editing them.

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->


Changed files:
filename: src/bootstrap/src/core/build_steps/setup.rs
patch: @@ -5,14 +5,17 @@
 //! allows setting up things that cannot be simply captured inside the bootstrap.toml, in addition to
 //! leading people away from manually editing most of the bootstrap.toml values.
 
+use std::collections::BTreeMap;
 use std::env::consts::EXE_SUFFIX;
 use std::fmt::Write as _;
 use std::fs::File;
 use std::io::Write;
 use std::path::{MAIN_SEPARATOR_STR, Path, PathBuf};
 use std::str::FromStr;
+use std::sync::LazyLock;
 use std::{fmt, fs, io};
 
+use serde_derive::{Deserialize, Serialize};
 use sha2::Digest;
 
 use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
@@ -529,7 +532,8 @@ undesirable, simply delete the `pre-push` file from .git/hooks."
 }
 
 /// Handles editor-specific setup differences
-#[derive(Clone, Debug, Eq, PartialEq)]
+#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
+#[serde(rename_all = "lowercase")]
 enum EditorKind {
     Emacs,
     Helix,
@@ -538,6 +542,13 @@ enum EditorKind {
     Zed,
 }
 
+static PARSED_HASHES: LazyLock<BTreeMap<EditorKind, Vec<&'static str>>> = LazyLock::new(|| {
+    const ALL_HASHES: &str = include_str!("setup/hashes.json");
+    let mut map: BTreeMap<_, Vec<_>> = serde_json::from_str(ALL_HASHES).unwrap();
+    map.insert(EditorKind::Vim, map.get(&EditorKind::VsCode).unwrap().clone());
+    map
+});
+
 impl EditorKind {
     // Used in `./tests.rs`.
     #[cfg(test)]
@@ -588,59 +599,7 @@ Select which editor you would like to set up [default: None]: ";
     /// New entries should be appended whenever this is updated so we can detect
     /// outdated vs. user-modified settings files.
     fn hashes(&self) -> &'static [&'static str] {
-        match self {
-            EditorKind::Emacs => &[
-                "51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
-                "d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
-                "b5dd299b93dca3ceeb9b335f929293cb3d4bf4977866fbe7ceeac2a8a9f99088",
-                "631c837b0e98ae35fd48b0e5f743b1ca60adadf2d0a2b23566ba25df372cf1a9",
-                "080955765db84bb6cbf178879f489c4e2369397626a6ecb3debedb94a9d0b3ce",
-                "f501475c6654187091c924ae26187fa5791d74d4a8ab3fb61fbbe4c0275aade1",
-                "54bc48fe1996177f5eef86d7231b33978e6d8b737cb0a899e622b7e975c95308",
-                "08d30e455ceec6e01d9bcef8b9449f2ddd14d278ca8627cdad90e02d9f44e938",
-            ],
-            EditorKind::Helix => &[
-                "2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233",
-                "6736d61409fbebba0933afd2e4c44ff2f97c1cb36cf0299a7f4a7819b8775040",
-                "f252dcc30ca85a193a699581e5e929d5bd6c19d40d7a7ade5e257a9517a124a5",
-                "198c195ed0c070d15907b279b8b4ea96198ca71b939f5376454f3d636ab54da5",
-                "1c43ead340b20792b91d02b08494ee68708e7e09f56b6766629b4b72079208f1",
-                "eec09a09452682060afd23dd5d3536ccac5615b3cdbf427366446901215fb9f6",
-                "cb653043852d9d5ff4a5be56407b859ff9928be055ad3f307eb309aad04765e6",
-                "e28b1930d16d3d8bbdeed7bd4a995613e648b49e08c9b6f5271880f520637fed",
-            ],
-            EditorKind::Vim | EditorKind::VsCode => &[
-                "ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
-                "56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
-                "af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0",
-                "3468fea433c25fff60be6b71e8a215a732a7b1268b6a83bf10d024344e140541",
-                "47d227f424bf889b0d899b9cc992d5695e1b78c406e183cd78eafefbe5488923",
-                "b526bd58d0262dd4dda2bff5bc5515b705fb668a46235ace3e057f807963a11a",
-                "828666b021d837a33e78d870b56d34c88a5e2c85de58b693607ec574f0c27000",
-                "811fb3b063c739d261fd8590dd30242e117908f5a095d594fa04585daa18ec4d",
-                "4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
-                "c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
-                "e53e9129ca5ee5dcbd6ec8b68c2d87376474eb154992deba3c6d9ab1703e0717",
-                "f954316090936c7e590c253ca9d524008375882fa13c5b41d7e2547a896ff893",
-                "701b73751efd7abd6487f2c79348dab698af7ac4427b79fa3d2087c867144b12",
-                "a61df796c0c007cb6512127330564e49e57d558dec715703916a928b072a1054",
-                "02a49ac2d31f00ef6e4531c44e00dac51cea895112e480553f1ba060b3942a47",
-                "0aa4748848de0d1cb7ece92a0123c8897fef6de2f58aff8fda1426f098b7a798",
-                "e5e357862e5d6d0d9da335e9823c07b8a7dc42bbf18d72cc5206ad1049cd8fcc",
-                "a68fd5828e75f3e921f265e29ce1e9efa554083c3773fdb4b8e1ab3b2d9dc6cd",
-            ],
-            EditorKind::Zed => &[
-                "bbce727c269d1bd0c98afef4d612eb4ce27aea3c3a8968c5f10b31affbc40b6c",
-                "a5380cf5dd9328731aecc5dfb240d16dac46ed272126b9728006151ef42f5909",
-                "2e96bf0d443852b12f016c8fc9840ab3d0a2b4fe0b0fb3a157e8d74d5e7e0e26",
-                "4fadd4c87389a601a27db0d3d74a142fa3a2e656ae78982e934dbe24bee32ad6",
-                "f0bb3d23ab1a49175ab0ef5c4071af95bb03d01d460776cdb716d91333443382",
-                "5ef83292111d9a8bb63b6afc3abf42d0bc78fe24985f0d2e039e73258b5dab8f",
-                "74420c13094b530a986b37c4f1d23cb58c0e8e2295f5858ded129fb1574e66f9",
-                "2d3b592c089b2ad2c528686a1e371af49922edad1c59accd5d5f31612a441568",
-                "0767a2398ccc253274b184adbb9e018ce931bd0ef45baad06dad19b652c52951",
-            ],
-        }
+        PARSED_HASHES.get(self).unwrap()
     }
 
     fn settings_path(&self, config: &Config) -> PathBuf {

filename: src/bootstrap/src/core/build_steps/setup/hashes.json
patch: @@ -0,0 +1,73 @@
+{
+  "emacs": [
+    "51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
+    "d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
+    "b5dd299b93dca3ceeb9b335f929293cb3d4bf4977866fbe7ceeac2a8a9f99088",
+    "631c837b0e98ae35fd48b0e5f743b1ca60adadf2d0a2b23566ba25df372cf1a9",
+    "080955765db84bb6cbf178879f489c4e2369397626a6ecb3debedb94a9d0b3ce",
+    "f501475c6654187091c924ae26187fa5791d74d4a8ab3fb61fbbe4c0275aade1",
+    "54bc48fe1996177f5eef86d7231b33978e6d8b737cb0a899e622b7e975c95308",
+    "08d30e455ceec6e01d9bcef8b9449f2ddd14d278ca8627cdad90e02d9f44e938"
+  ],
+  "helix": [
+    "2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233",
+    "6736d61409fbebba0933afd2e4c44ff2f97c1cb36cf0299a7f4a7819b8775040",
+    "f252dcc30ca85a193a699581e5e929d5bd6c19d40d7a7ade5e257a9517a124a5",
+    "198c195ed0c070d15907b279b8b4ea96198ca71b939f5376454f3d636ab54da5",
+    "1c43ead340b20792b91d02b08494ee68708e7e09f56b6766629b4b72079208f1",
+    "eec09a09452682060afd23dd5d3536ccac5615b3cdbf427366446901215fb9f6",
+    "cb653043852d9d5ff4a5be56407b859ff9928be055ad3f307eb309aad04765e6",
+    "e28b1930d16d3d8bbdeed7bd4a995613e648b49e08c9b6f5271880f520637fed"
+  ],
+  "vim": [
+    "ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
+    "56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
+    "af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0",
+    "3468fea433c25fff60be6b71e8a215a732a7b1268b6a83bf10d024344e140541",
+    "47d227f424bf889b0d899b9cc992d5695e1b78c406e183cd78eafefbe5488923",
+    "b526bd58d0262dd4dda2bff5bc5515b705fb668a46235ace3e057f807963a11a",
+    "828666b021d837a33e78d870b56d34c88a5e2c85de58b693607ec574f0c27000",
+    "811fb3b063c739d261fd8590dd30242e117908f5a095d594fa04585daa18ec4d",
+    "4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
+    "c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
+    "e53e9129ca5ee5dcbd6ec8b68c2d87376474eb154992deba3c6d9ab1703e0717",
+    "f954316090936c7e590c253ca9d524008375882fa13c5b41d7e2547a896ff893",
+    "701b73751efd7abd6487f2c79348dab698af7ac4427b79fa3d2087c867144b12",
+    "a61df796c0c007cb6512127330564e49e57d558dec715703916a928b072a1054",
+    "02a49ac2d31f00ef6e4531c44e00dac51cea895112e480553f1ba060b3942a47",
+    "0aa4748848de0d1cb7ece92a0123c8897fef6de2f58aff8fda1426f098b7a798",
+    "e5e357862e5d6d0d9da335e9823c07b8a7dc42bbf18d72cc5206ad1049cd8fcc",
+    "a68fd5828e75f3e921f265e29ce1e9efa554083c3773fdb4b8e1ab3b2d9dc6cd"
+  ],
+  "vscode": [
+    "ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
+    "56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
+    "af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0",
+    "3468fea433c25fff60be6b71e8a215a732a7b1268b6a83bf10d024344e140541",
+    "47d227f424bf889b0d899b9cc992d5695e1b78c406e183cd78eafefbe5488923",
+    "b526bd58d0262dd4dda2bff5bc5515b705fb668a46235ace3e057f807963a11a",
+    "828666b021d837a33e78d870b56d34c88a5e2c85de58b693607ec574f0c27000",
+    "811fb3b063c739d261fd8590dd30242e117908f5a095d594fa04585daa18ec4d",
+    "4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
+    "c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
+    "e53e9129ca5ee5dcbd6ec8b68c2d87376474eb154992deba3c6d9ab1703e0717",
+    "f954316090936c7e590c253ca9d524008375882fa13c5b41d7e2547a896ff893",
+    "701b73751efd7abd6487f2c79348dab698af7ac4427b79fa3d2087c867144b12",
+    "a61df796c0c007cb6512127330564e49e57d558dec715703916a928b072a1054",
+    "02a49ac2d31f00ef6e4531c44e00dac51cea895112e480553f1ba060b3942a47",
+    "0aa4748848de0d1cb7ece92a0123c8897fef6de2f58aff8fda1426f098b7a798",
+    "e5e357862e5d6d0d9da335e9823c07b8a7dc42bbf18d72cc5206ad1049cd8fcc",
+    "a68fd5828e75f3e921f265e29ce1e9efa554083c3773fdb4b8e1ab3b2d9dc6cd"
+  ],
+  "zed": [
+    "bbce727c269d1bd0c98afef4d612eb4ce27aea3c3a8968c5f10b31affbc40b6c",
+    "a5380cf5dd9328731aecc5dfb240d16dac46ed272126b9728006151ef42f5909",
+    "2e96bf0d443852b12f016c8fc9840ab3d0a2b4fe0b0fb3a157e8d74d5e7e0e26",
+    "4fadd4c87389a601a27db0d3d74a142fa3a2e656ae78982e934dbe24bee32ad6",
+    "f0bb3d23ab1a49175ab0ef5c4071af95bb03d01d460776cdb716d91333443382",
+    "5ef83292111d9a8bb63b6afc3abf42d0bc78fe24985f0d2e039e73258b5dab8f",
+    "74420c13094b530a986b37c4f1d23cb58c0e8e2295f5858ded129fb1574e66f9",
+    "2d3b592c089b2ad2c528686a1e371af49922edad1c59accd5d5f31612a441568",
+    "0767a2398ccc253274b184adbb9e018ce931bd0ef45baad06dad19b652c52951"
+  ]
+}

filename: src/bootstrap/src/core/build_steps/setup/tests.rs
patch: @@ -1,20 +1,50 @@
+use std::collections::BTreeMap;
+use std::fs::File;
+use std::io::Write;
+use std::path::Path;
+
 use sha2::Digest;
 
 use super::EditorKind;
 use crate::utils::helpers::hex_encode;
 
 #[test]
 fn check_matching_settings_hash() {
+    // Needs to be a btree so we serialize in a deterministic order.
+    let mut mismatched = BTreeMap::new();
+
     for editor in EditorKind::ALL {
         let mut hasher = sha2::Sha256::new();
         hasher.update(&editor.settings_template());
-        let hash = hex_encode(hasher.finalize().as_slice());
-        assert_eq!(
-            &hash,
-            editor.hashes().last().unwrap(),
-            "Update `EditorKind::hashes()` with the new hash of `{}` for `EditorKind::{:?}`",
-            editor.settings_template(),
-            editor,
-        );
+        let actual = hex_encode(hasher.finalize().as_slice());
+        let expected = *editor.hashes().last().unwrap();
+
+        if expected != actual {
+            mismatched.insert(editor, (expected, actual));
+        }
+    }
+
+    if mismatched.is_empty() {
+        return;
+    }
+
+    if option_env!("INSTA_UPDATE").is_some_and(|s| s != "0") {
+        let mut updated = super::PARSED_HASHES.clone();
+        for (editor, (_, actual)) in &mismatched {
+            *updated.get_mut(editor).unwrap().last_mut().unwrap() = actual;
+        }
+        let hash_path =
+            Path::new(env!("CARGO_MANIFEST_DIR")).join("src/core/build_steps/setup/hashes.json");
+        let mut hash_file = File::create(hash_path).unwrap();
+        serde_json::to_writer_pretty(&mut hash_file, &updated).unwrap();
+        hash_file.write_all(b"\n").unwrap();
+    } else {
+        for (editor, (expected, actual)) in &mismatched {
+            eprintln!("recorded hash did not match actual hash: {expected} != {actual}");
+            eprintln!(
+                "Run `x test --bless -- hash`, or manually update `setup/hashes.json` with the new hash of `{actual}` for `EditorKind::{editor:?}`"
+            );
+        }
+        panic!("mismatched hashes");
     }
 }