bootstrap/core/build_steps/
setup.rs

1//! First time setup of a dev environment
2//!
3//! These are build-and-run steps for `./x.py setup`, which allows quickly setting up the directory
4//! for modifying, building, and running the compiler and library. Running arbitrary configuration
5//! allows setting up things that cannot be simply captured inside the bootstrap.toml, in addition to
6//! leading people away from manually editing most of the bootstrap.toml values.
7
8use std::env::consts::EXE_SUFFIX;
9use std::fmt::Write as _;
10use std::fs::File;
11use std::io::Write;
12use std::path::{MAIN_SEPARATOR_STR, Path, PathBuf};
13use std::str::FromStr;
14use std::{fmt, fs, io};
15
16use sha2::Digest;
17
18use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
19use crate::utils::change_tracker::CONFIG_CHANGE_HISTORY;
20use crate::utils::exec::command;
21use crate::utils::helpers::{self, hex_encode};
22use crate::{Config, t};
23
24#[cfg(test)]
25mod tests;
26
27#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
28pub enum Profile {
29    Compiler,
30    Library,
31    Tools,
32    Dist,
33    None,
34}
35
36static PROFILE_DIR: &str = "src/bootstrap/defaults";
37
38impl Profile {
39    fn include_path(&self, src_path: &Path) -> PathBuf {
40        PathBuf::from(format!("{}/{PROFILE_DIR}/bootstrap.{}.toml", src_path.display(), self))
41    }
42
43    pub fn all() -> impl Iterator<Item = Self> {
44        use Profile::*;
45        // N.B. these are ordered by how they are displayed, not alphabetically
46        [Library, Compiler, Tools, Dist, None].iter().copied()
47    }
48
49    pub fn purpose(&self) -> String {
50        use Profile::*;
51        match self {
52            Library => "Contribute to the standard library",
53            Compiler => "Contribute to the compiler itself",
54            Tools => "Contribute to tools which depend on the compiler, but do not modify it directly (e.g. rustdoc, clippy, miri)",
55            Dist => "Install Rust from source",
56            None => "Do not modify `bootstrap.toml`"
57        }
58        .to_string()
59    }
60
61    pub fn all_for_help(indent: &str) -> String {
62        let mut out = String::new();
63        for choice in Profile::all() {
64            writeln!(&mut out, "{}{}: {}", indent, choice, choice.purpose()).unwrap();
65        }
66        out
67    }
68
69    pub fn as_str(&self) -> &'static str {
70        match self {
71            Profile::Compiler => "compiler",
72            Profile::Library => "library",
73            Profile::Tools => "tools",
74            Profile::Dist => "dist",
75            Profile::None => "none",
76        }
77    }
78}
79
80impl FromStr for Profile {
81    type Err = String;
82
83    fn from_str(s: &str) -> Result<Self, Self::Err> {
84        match s {
85            "lib" | "library" => Ok(Profile::Library),
86            "compiler" => Ok(Profile::Compiler),
87            "maintainer" | "dist" | "user" => Ok(Profile::Dist),
88            "tools" | "tool" | "rustdoc" | "clippy" | "miri" | "rustfmt" => Ok(Profile::Tools),
89            "none" => Ok(Profile::None),
90            "llvm" | "codegen" => Err("the \"llvm\" and \"codegen\" profiles have been removed,\
91                use \"compiler\" instead which has the same functionality"
92                .to_string()),
93            _ => Err(format!("unknown profile: '{s}'")),
94        }
95    }
96}
97
98impl fmt::Display for Profile {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        f.write_str(self.as_str())
101    }
102}
103
104impl Step for Profile {
105    type Output = ();
106    const DEFAULT: bool = true;
107
108    fn should_run(mut run: ShouldRun<'_>) -> ShouldRun<'_> {
109        for choice in Profile::all() {
110            run = run.alias(choice.as_str());
111        }
112        run
113    }
114
115    fn make_run(run: RunConfig<'_>) {
116        if run.builder.config.dry_run() {
117            return;
118        }
119
120        let path = &run.builder.config.config.clone().unwrap_or(PathBuf::from("bootstrap.toml"));
121        if path.exists() {
122            eprintln!();
123            eprintln!(
124                "ERROR: you asked for a new config file, but one already exists at `{}`",
125                t!(path.canonicalize()).display()
126            );
127
128            match prompt_user(
129                "Do you wish to override the existing configuration (which will allow the setup process to continue)?: [y/N]",
130            ) {
131                Ok(Some(PromptResult::Yes)) => {
132                    t!(fs::remove_file(path));
133                }
134                _ => {
135                    println!("Exiting.");
136                    crate::exit!(1);
137                }
138            }
139        }
140
141        // for Profile, `run.paths` will have 1 and only 1 element
142        // this is because we only accept at most 1 path from user input.
143        // If user calls `x.py setup` without arguments, the interactive TUI
144        // will guide user to provide one.
145        let profile = if run.paths.len() > 1 {
146            // HACK: `builder` runs this step with all paths if no path was passed.
147            t!(interactive_path())
148        } else {
149            run.paths
150                .first()
151                .unwrap()
152                .assert_single_path()
153                .path
154                .as_path()
155                .as_os_str()
156                .to_str()
157                .unwrap()
158                .parse()
159                .unwrap()
160        };
161
162        run.builder.ensure(profile);
163    }
164
165    fn run(self, builder: &Builder<'_>) {
166        setup(&builder.build.config, self);
167    }
168}
169
170pub fn setup(config: &Config, profile: Profile) {
171    let suggestions: &[&str] = match profile {
172        Profile::Compiler | Profile::None => &["check", "build", "test"],
173        Profile::Tools => &[
174            "check",
175            "build",
176            "test tests/rustdoc*",
177            "test src/tools/clippy",
178            "test src/tools/miri",
179            "test src/tools/rustfmt",
180        ],
181        Profile::Library => &["check", "build", "test library/std", "doc"],
182        Profile::Dist => &["dist", "build"],
183    };
184
185    println!();
186
187    println!("To get started, try one of the following commands:");
188    for cmd in suggestions {
189        println!("- `x.py {cmd}`");
190    }
191
192    if profile != Profile::Dist {
193        println!(
194            "For more suggestions, see https://rustc-dev-guide.rust-lang.org/building/suggested.html"
195        );
196    }
197
198    if profile == Profile::Tools {
199        eprintln!();
200        eprintln!(
201            "NOTE: the `tools` profile sets up the `stage2` toolchain (use \
202            `rustup toolchain link 'name' build/host/stage2` to use rustc)"
203        )
204    }
205
206    let path = &config.config.clone().unwrap_or(PathBuf::from("bootstrap.toml"));
207    setup_config_toml(path, profile, config);
208}
209
210fn setup_config_toml(path: &Path, profile: Profile, config: &Config) {
211    if profile == Profile::None {
212        return;
213    }
214
215    let latest_change_id = CONFIG_CHANGE_HISTORY.last().unwrap().change_id;
216    let settings = format!(
217        "# See bootstrap.example.toml for documentation of available options\n\
218    #\n\
219    profile = \"{profile}\"  # Includes one of the default files in {PROFILE_DIR}\n\
220    change-id = {latest_change_id}\n"
221    );
222
223    t!(fs::write(path, settings));
224
225    let include_path = profile.include_path(&config.src);
226    println!("`x.py` will now use the configuration at {}", include_path.display());
227}
228
229/// Creates a toolchain link for stage1 using `rustup`
230#[derive(Clone, Debug, Eq, PartialEq, Hash)]
231pub struct Link;
232impl Step for Link {
233    type Output = ();
234    const DEFAULT: bool = true;
235
236    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
237        run.alias("link")
238    }
239
240    fn make_run(run: RunConfig<'_>) {
241        if run.builder.config.dry_run() {
242            return;
243        }
244        if let [cmd] = &run.paths[..] {
245            if cmd.assert_single_path().path.as_path().as_os_str() == "link" {
246                run.builder.ensure(Link);
247            }
248        }
249    }
250    fn run(self, builder: &Builder<'_>) -> Self::Output {
251        let config = &builder.config;
252
253        if config.dry_run() {
254            return;
255        }
256
257        if !rustup_installed(builder) {
258            println!("WARNING: `rustup` is not installed; Skipping `stage1` toolchain linking.");
259            return;
260        }
261
262        let stage_path =
263            ["build", config.build.rustc_target_arg(), "stage1"].join(MAIN_SEPARATOR_STR);
264
265        if stage_dir_exists(&stage_path[..]) && !config.dry_run() {
266            attempt_toolchain_link(builder, &stage_path[..]);
267        }
268    }
269}
270
271fn rustup_installed(builder: &Builder<'_>) -> bool {
272    let mut rustup = command("rustup");
273    rustup.arg("--version");
274
275    rustup.allow_failure().run_always().run_capture_stdout(builder).is_success()
276}
277
278fn stage_dir_exists(stage_path: &str) -> bool {
279    match fs::create_dir(stage_path) {
280        Ok(_) => true,
281        Err(_) => Path::new(&stage_path).exists(),
282    }
283}
284
285fn attempt_toolchain_link(builder: &Builder<'_>, stage_path: &str) {
286    if toolchain_is_linked(builder) {
287        return;
288    }
289
290    if !ensure_stage1_toolchain_placeholder_exists(stage_path) {
291        eprintln!(
292            "Failed to create a template for stage 1 toolchain or confirm that it already exists"
293        );
294        return;
295    }
296
297    if try_link_toolchain(builder, stage_path) {
298        println!(
299            "Added `stage1` rustup toolchain; try `cargo +stage1 build` on a separate rust project to run a newly-built toolchain"
300        );
301    } else {
302        eprintln!("`rustup` failed to link stage 1 build to `stage1` toolchain");
303        eprintln!(
304            "To manually link stage 1 build to `stage1` toolchain, run:\n
305            `rustup toolchain link stage1 {}`",
306            &stage_path
307        );
308    }
309}
310
311fn toolchain_is_linked(builder: &Builder<'_>) -> bool {
312    match command("rustup")
313        .allow_failure()
314        .args(["toolchain", "list"])
315        .run_capture_stdout(builder)
316        .stdout_if_ok()
317    {
318        Some(toolchain_list) => {
319            if !toolchain_list.contains("stage1") {
320                return false;
321            }
322            // The toolchain has already been linked.
323            println!(
324                "`stage1` toolchain already linked; not attempting to link `stage1` toolchain"
325            );
326        }
327        None => {
328            // In this case, we don't know if the `stage1` toolchain has been linked;
329            // but `rustup` failed, so let's not go any further.
330            println!(
331                "`rustup` failed to list current toolchains; not attempting to link `stage1` toolchain"
332            );
333        }
334    }
335    true
336}
337
338fn try_link_toolchain(builder: &Builder<'_>, stage_path: &str) -> bool {
339    command("rustup")
340        .args(["toolchain", "link", "stage1", stage_path])
341        .run_capture_stdout(builder)
342        .is_success()
343}
344
345fn ensure_stage1_toolchain_placeholder_exists(stage_path: &str) -> bool {
346    let pathbuf = PathBuf::from(stage_path);
347
348    if fs::create_dir_all(pathbuf.join("lib")).is_err() {
349        return false;
350    };
351
352    let pathbuf = pathbuf.join("bin");
353    if fs::create_dir_all(&pathbuf).is_err() {
354        return false;
355    };
356
357    let pathbuf = pathbuf.join(format!("rustc{EXE_SUFFIX}"));
358
359    if pathbuf.exists() {
360        return true;
361    }
362
363    // Take care not to overwrite the file
364    let result = File::options().append(true).create(true).open(&pathbuf);
365    if result.is_err() {
366        return false;
367    }
368
369    true
370}
371
372// Used to get the path for `Subcommand::Setup`
373pub fn interactive_path() -> io::Result<Profile> {
374    fn abbrev_all() -> impl Iterator<Item = ((String, String), Profile)> {
375        ('a'..)
376            .zip(1..)
377            .map(|(letter, number)| (letter.to_string(), number.to_string()))
378            .zip(Profile::all())
379    }
380
381    fn parse_with_abbrev(input: &str) -> Result<Profile, String> {
382        let input = input.trim().to_lowercase();
383        for ((letter, number), profile) in abbrev_all() {
384            if input == letter || input == number {
385                return Ok(profile);
386            }
387        }
388        input.parse()
389    }
390
391    println!("Welcome to the Rust project! What do you want to do with x.py?");
392    for ((letter, _), profile) in abbrev_all() {
393        println!("{}) {}: {}", letter, profile, profile.purpose());
394    }
395    let template = loop {
396        print!(
397            "Please choose one ({}): ",
398            abbrev_all().map(|((l, _), _)| l).collect::<Vec<_>>().join("/")
399        );
400        io::stdout().flush()?;
401        let mut input = String::new();
402        io::stdin().read_line(&mut input)?;
403        if input.is_empty() {
404            eprintln!("EOF on stdin, when expecting answer to question.  Giving up.");
405            crate::exit!(1);
406        }
407        break match parse_with_abbrev(&input) {
408            Ok(profile) => profile,
409            Err(err) => {
410                eprintln!("ERROR: {err}");
411                eprintln!("NOTE: press Ctrl+C to exit");
412                continue;
413            }
414        };
415    };
416    Ok(template)
417}
418
419#[derive(PartialEq)]
420enum PromptResult {
421    Yes,   // y/Y/yes
422    No,    // n/N/no
423    Print, // p/P/print
424}
425
426/// Prompt a user for a answer, looping until they enter an accepted input or nothing
427fn prompt_user(prompt: &str) -> io::Result<Option<PromptResult>> {
428    let mut input = String::new();
429    loop {
430        print!("{prompt} ");
431        io::stdout().flush()?;
432        input.clear();
433        io::stdin().read_line(&mut input)?;
434        match input.trim().to_lowercase().as_str() {
435            "y" | "yes" => return Ok(Some(PromptResult::Yes)),
436            "n" | "no" => return Ok(Some(PromptResult::No)),
437            "p" | "print" => return Ok(Some(PromptResult::Print)),
438            "" => return Ok(None),
439            _ => {
440                eprintln!("ERROR: unrecognized option '{}'", input.trim());
441                eprintln!("NOTE: press Ctrl+C to exit");
442            }
443        };
444    }
445}
446
447/// Installs `src/etc/pre-push.sh` as a Git hook
448#[derive(Clone, Debug, Eq, PartialEq, Hash)]
449pub struct Hook;
450
451impl Step for Hook {
452    type Output = ();
453    const DEFAULT: bool = true;
454
455    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
456        run.alias("hook")
457    }
458
459    fn make_run(run: RunConfig<'_>) {
460        if let [cmd] = &run.paths[..] {
461            if cmd.assert_single_path().path.as_path().as_os_str() == "hook" {
462                run.builder.ensure(Hook);
463            }
464        }
465    }
466
467    fn run(self, builder: &Builder<'_>) -> Self::Output {
468        let config = &builder.config;
469
470        if config.dry_run() || !config.rust_info.is_managed_git_subrepository() {
471            return;
472        }
473
474        t!(install_git_hook_maybe(builder, config));
475    }
476}
477
478// install a git hook to automatically run tidy, if they want
479fn install_git_hook_maybe(builder: &Builder<'_>, config: &Config) -> io::Result<()> {
480    let git = helpers::git(Some(&config.src))
481        .args(["rev-parse", "--git-common-dir"])
482        .run_capture(builder)
483        .stdout();
484    let git = PathBuf::from(git.trim());
485    let hooks_dir = git.join("hooks");
486    let dst = hooks_dir.join("pre-push");
487    if dst.exists() {
488        // The git hook has already been set up, or the user already has a custom hook.
489        return Ok(());
490    }
491
492    println!(
493        "\nRust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
494If you'd like, x.py can install a git hook for you that will automatically run `test tidy` before
495pushing your code to ensure your code is up to par. If you decide later that this behavior is
496undesirable, simply delete the `pre-push` file from .git/hooks."
497    );
498
499    if prompt_user("Would you like to install the git hook?: [y/N]")? != Some(PromptResult::Yes) {
500        println!("Ok, skipping installation!");
501        return Ok(());
502    }
503    if !hooks_dir.exists() {
504        // We need to (try to) create the hooks directory first.
505        let _ = fs::create_dir(hooks_dir);
506    }
507    let src = config.src.join("src").join("etc").join("pre-push.sh");
508    match fs::hard_link(src, &dst) {
509        Err(e) => {
510            eprintln!(
511                "ERROR: could not create hook {}: do you already have the git hook installed?\n{}",
512                dst.display(),
513                e
514            );
515            return Err(e);
516        }
517        Ok(_) => println!("Linked `src/etc/pre-push.sh` to `.git/hooks/pre-push`"),
518    };
519    Ok(())
520}
521
522/// Handles editor-specific setup differences
523#[derive(Clone, Debug, Eq, PartialEq)]
524enum EditorKind {
525    Emacs,
526    Helix,
527    Vim,
528    VsCode,
529    Zed,
530}
531
532impl EditorKind {
533    // Used in `./tests.rs`.
534    #[allow(dead_code)]
535    pub const ALL: &[EditorKind] = &[
536        EditorKind::Emacs,
537        EditorKind::Helix,
538        EditorKind::Vim,
539        EditorKind::VsCode,
540        EditorKind::Zed,
541    ];
542
543    fn prompt_user() -> io::Result<Option<EditorKind>> {
544        let prompt_str = "Available editors:
5451. Emacs
5462. Helix
5473. Vim
5484. VS Code
5495. Zed
550
551Select which editor you would like to set up [default: None]: ";
552
553        let mut input = String::new();
554        loop {
555            print!("{}", prompt_str);
556            io::stdout().flush()?;
557            io::stdin().read_line(&mut input)?;
558
559            let mut modified_input = input.to_lowercase();
560            modified_input.retain(|ch| !ch.is_whitespace());
561            match modified_input.as_str() {
562                "1" | "emacs" => return Ok(Some(EditorKind::Emacs)),
563                "2" | "helix" => return Ok(Some(EditorKind::Helix)),
564                "3" | "vim" => return Ok(Some(EditorKind::Vim)),
565                "4" | "vscode" => return Ok(Some(EditorKind::VsCode)),
566                "5" | "zed" => return Ok(Some(EditorKind::Zed)),
567                "" | "none" => return Ok(None),
568                _ => {
569                    eprintln!("ERROR: unrecognized option '{}'", input.trim());
570                    eprintln!("NOTE: press Ctrl+C to exit");
571                }
572            }
573
574            input.clear();
575        }
576    }
577
578    /// A list of historical hashes of each LSP settings file
579    /// New entries should be appended whenever this is updated so we can detect
580    /// outdated vs. user-modified settings files.
581    fn hashes(&self) -> &'static [&'static str] {
582        match self {
583            EditorKind::Emacs => &[
584                "51068d4747a13732440d1a8b8f432603badb1864fa431d83d0fd4f8fa57039e0",
585                "d29af4d949bbe2371eac928a3c31cf9496b1701aa1c45f11cd6c759865ad5c45",
586                "b5dd299b93dca3ceeb9b335f929293cb3d4bf4977866fbe7ceeac2a8a9f99088",
587            ],
588            EditorKind::Helix => &[
589                "2d3069b8cf1b977e5d4023965eb6199597755e6c96c185ed5f2854f98b83d233",
590                "6736d61409fbebba0933afd2e4c44ff2f97c1cb36cf0299a7f4a7819b8775040",
591                "f252dcc30ca85a193a699581e5e929d5bd6c19d40d7a7ade5e257a9517a124a5",
592            ],
593            EditorKind::Vim | EditorKind::VsCode => &[
594                "ea67e259dedf60d4429b6c349a564ffcd1563cf41c920a856d1f5b16b4701ac8",
595                "56e7bf011c71c5d81e0bf42e84938111847a810eee69d906bba494ea90b51922",
596                "af1b5efe196aed007577899db9dae15d6dbc923d6fa42fa0934e68617ba9bbe0",
597                "3468fea433c25fff60be6b71e8a215a732a7b1268b6a83bf10d024344e140541",
598                "47d227f424bf889b0d899b9cc992d5695e1b78c406e183cd78eafefbe5488923",
599                "b526bd58d0262dd4dda2bff5bc5515b705fb668a46235ace3e057f807963a11a",
600                "828666b021d837a33e78d870b56d34c88a5e2c85de58b693607ec574f0c27000",
601                "811fb3b063c739d261fd8590dd30242e117908f5a095d594fa04585daa18ec4d",
602                "4eecb58a2168b252077369da446c30ed0e658301efe69691979d1ef0443928f4",
603                "c394386e6133bbf29ffd32c8af0bb3d4aac354cba9ee051f29612aa9350f8f8d",
604                "e53e9129ca5ee5dcbd6ec8b68c2d87376474eb154992deba3c6d9ab1703e0717",
605            ],
606            EditorKind::Zed => &[
607                "bbce727c269d1bd0c98afef4d612eb4ce27aea3c3a8968c5f10b31affbc40b6c",
608                "a5380cf5dd9328731aecc5dfb240d16dac46ed272126b9728006151ef42f5909",
609            ],
610        }
611    }
612
613    fn settings_path(&self, config: &Config) -> PathBuf {
614        config.src.join(self.settings_short_path())
615    }
616
617    fn settings_short_path(&self) -> PathBuf {
618        self.settings_folder().join(match self {
619            EditorKind::Emacs => ".dir-locals.el",
620            EditorKind::Helix => "languages.toml",
621            EditorKind::Vim => "coc-settings.json",
622            EditorKind::VsCode | EditorKind::Zed => "settings.json",
623        })
624    }
625
626    fn settings_folder(&self) -> PathBuf {
627        match self {
628            EditorKind::Emacs => PathBuf::new(),
629            EditorKind::Helix => PathBuf::from(".helix"),
630            EditorKind::Vim => PathBuf::from(".vim"),
631            EditorKind::VsCode => PathBuf::from(".vscode"),
632            EditorKind::Zed => PathBuf::from(".zed"),
633        }
634    }
635
636    fn settings_template(&self) -> &'static str {
637        match self {
638            EditorKind::Emacs => include_str!("../../../../etc/rust_analyzer_eglot.el"),
639            EditorKind::Helix => include_str!("../../../../etc/rust_analyzer_helix.toml"),
640            EditorKind::Vim | EditorKind::VsCode => {
641                include_str!("../../../../etc/rust_analyzer_settings.json")
642            }
643            EditorKind::Zed => include_str!("../../../../etc/rust_analyzer_zed.json"),
644        }
645    }
646
647    fn backup_extension(&self) -> String {
648        format!("{}.bak", self.settings_short_path().extension().unwrap().to_str().unwrap())
649    }
650}
651
652/// Sets up or displays the LSP config for one of the supported editors
653#[derive(Clone, Debug, Eq, PartialEq, Hash)]
654pub struct Editor;
655
656impl Step for Editor {
657    type Output = ();
658    const DEFAULT: bool = true;
659
660    fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
661        run.alias("editor")
662    }
663
664    fn make_run(run: RunConfig<'_>) {
665        if run.builder.config.dry_run() {
666            return;
667        }
668        if let [cmd] = &run.paths[..] {
669            if cmd.assert_single_path().path.as_path().as_os_str() == "editor" {
670                run.builder.ensure(Editor);
671            }
672        }
673    }
674
675    fn run(self, builder: &Builder<'_>) -> Self::Output {
676        let config = &builder.config;
677        if config.dry_run() {
678            return;
679        }
680        match EditorKind::prompt_user() {
681            Ok(editor_kind) => {
682                if let Some(editor_kind) = editor_kind {
683                    while !t!(create_editor_settings_maybe(config, editor_kind.clone())) {}
684                } else {
685                    println!("Ok, skipping editor setup!");
686                }
687            }
688            Err(e) => eprintln!("Could not determine the editor: {e}"),
689        }
690    }
691}
692
693/// Create the recommended editor LSP config file for rustc development, or just print it
694/// If this method should be re-called, it returns `false`.
695fn create_editor_settings_maybe(config: &Config, editor: EditorKind) -> io::Result<bool> {
696    let hashes = editor.hashes();
697    let (current_hash, historical_hashes) = hashes.split_last().unwrap();
698    let settings_path = editor.settings_path(config);
699    let settings_short_path = editor.settings_short_path();
700    let settings_filename = settings_short_path.to_str().unwrap();
701    // If None, no settings file exists
702    // If Some(true), is a previous version of settings.json
703    // If Some(false), is not a previous version (i.e. user modified)
704    // If it's up to date we can just skip this
705    let mut mismatched_settings = None;
706    if let Ok(current) = fs::read_to_string(&settings_path) {
707        let mut hasher = sha2::Sha256::new();
708        hasher.update(&current);
709        let hash = hex_encode(hasher.finalize().as_slice());
710        if hash == *current_hash {
711            return Ok(true);
712        } else if historical_hashes.contains(&hash.as_str()) {
713            mismatched_settings = Some(true);
714        } else {
715            mismatched_settings = Some(false);
716        }
717    }
718    println!(
719        "\nx.py can automatically install the recommended `{settings_filename}` file for rustc development"
720    );
721
722    match mismatched_settings {
723        Some(true) => {
724            eprintln!("WARNING: existing `{settings_filename}` is out of date, x.py will update it")
725        }
726        Some(false) => eprintln!(
727            "WARNING: existing `{settings_filename}` has been modified by user, x.py will back it up and replace it"
728        ),
729        _ => (),
730    }
731    let should_create = match prompt_user(&format!(
732        "Would you like to create/update `{settings_filename}`? (Press 'p' to preview values): [y/N]"
733    ))? {
734        Some(PromptResult::Yes) => true,
735        Some(PromptResult::Print) => false,
736        _ => {
737            println!("Ok, skipping settings!");
738            return Ok(true);
739        }
740    };
741    if should_create {
742        let settings_folder_path = config.src.join(editor.settings_folder());
743        if !settings_folder_path.exists() {
744            fs::create_dir(settings_folder_path)?;
745        }
746        let verb = match mismatched_settings {
747            // exists but outdated, we can replace this
748            Some(true) => "Updated",
749            // exists but user modified, back it up
750            Some(false) => {
751                // exists and is not current version or outdated, so back it up
752                let backup = settings_path.clone().with_extension(editor.backup_extension());
753                eprintln!(
754                    "WARNING: copying `{}` to `{}`",
755                    settings_path.file_name().unwrap().to_str().unwrap(),
756                    backup.file_name().unwrap().to_str().unwrap(),
757                );
758                fs::copy(&settings_path, &backup)?;
759                "Updated"
760            }
761            _ => "Created",
762        };
763        fs::write(&settings_path, editor.settings_template())?;
764        println!("{verb} `{}`", settings_filename);
765    } else {
766        println!("\n{}", editor.settings_template());
767    }
768    Ok(should_create)
769}