]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_dev/src/ra_setup.rs
Rollup merge of #81572 - pierwill:edit-error-codes-1, r=jonas-schievink
[rust.git] / src / tools / clippy / clippy_dev / src / ra_setup.rs
1 use std::fs;
2 use std::fs::File;
3 use std::io::prelude::*;
4 use std::path::{Path, PathBuf};
5
6 // This module takes an absolute path to a rustc repo and alters the dependencies to point towards
7 // the respective rustc subcrates instead of using extern crate xyz.
8 // This allows rust analyzer to analyze rustc internals and show proper information inside clippy
9 // code. See https://github.com/rust-analyzer/rust-analyzer/issues/3517 and https://github.com/rust-lang/rust-clippy/issues/5514 for details
10
11 pub fn run(rustc_path: Option<&str>) {
12     // we can unwrap here because the arg is required by clap
13     let rustc_path = PathBuf::from(rustc_path.unwrap());
14     assert!(rustc_path.is_dir(), "path is not a directory");
15     let rustc_source_basedir = rustc_path.join("compiler");
16     assert!(
17         rustc_source_basedir.is_dir(),
18         "are you sure the path leads to a rustc repo?"
19     );
20
21     let clippy_root_manifest = fs::read_to_string("Cargo.toml").expect("failed to read ./Cargo.toml");
22     let clippy_root_lib_rs = fs::read_to_string("src/driver.rs").expect("failed to read ./src/driver.rs");
23     inject_deps_into_manifest(
24         &rustc_source_basedir,
25         "Cargo.toml",
26         &clippy_root_manifest,
27         &clippy_root_lib_rs,
28     )
29     .expect("Failed to inject deps into ./Cargo.toml");
30
31     let clippy_lints_manifest =
32         fs::read_to_string("clippy_lints/Cargo.toml").expect("failed to read ./clippy_lints/Cargo.toml");
33     let clippy_lints_lib_rs =
34         fs::read_to_string("clippy_lints/src/lib.rs").expect("failed to read ./clippy_lints/src/lib.rs");
35     inject_deps_into_manifest(
36         &rustc_source_basedir,
37         "clippy_lints/Cargo.toml",
38         &clippy_lints_manifest,
39         &clippy_lints_lib_rs,
40     )
41     .expect("Failed to inject deps into ./clippy_lints/Cargo.toml");
42 }
43
44 fn inject_deps_into_manifest(
45     rustc_source_dir: &Path,
46     manifest_path: &str,
47     cargo_toml: &str,
48     lib_rs: &str,
49 ) -> std::io::Result<()> {
50     // do not inject deps if we have aleady done so
51     if cargo_toml.contains("[target.'cfg(NOT_A_PLATFORM)'.dependencies]") {
52         eprintln!(
53             "cargo dev ra_setup: warning: deps already found inside {}, doing nothing.",
54             manifest_path
55         );
56         return Ok(());
57     }
58
59     let extern_crates = lib_rs
60         .lines()
61         // get the deps
62         .filter(|line| line.starts_with("extern crate"))
63         // we have something like "extern crate foo;", we only care about the "foo"
64         //              ↓          ↓
65         // extern crate rustc_middle;
66         .map(|s| &s[13..(s.len() - 1)]);
67
68     let new_deps = extern_crates.map(|dep| {
69         // format the dependencies that are going to be put inside the Cargo.toml
70         format!(
71             "{dep} = {{ path = \"{source_path}/{dep}\" }}\n",
72             dep = dep,
73             source_path = rustc_source_dir.display()
74         )
75     });
76
77     // format a new [dependencies]-block with the new deps we need to inject
78     let mut all_deps = String::from("[target.'cfg(NOT_A_PLATFORM)'.dependencies]\n");
79     new_deps.for_each(|dep_line| {
80         all_deps.push_str(&dep_line);
81     });
82     all_deps.push_str("\n[dependencies]\n");
83
84     // replace "[dependencies]" with
85     // [dependencies]
86     // dep1 = { path = ... }
87     // dep2 = { path = ... }
88     // etc
89     let new_manifest = cargo_toml.replacen("[dependencies]\n", &all_deps, 1);
90
91     // println!("{}", new_manifest);
92     let mut file = File::create(manifest_path)?;
93     file.write_all(new_manifest.as_bytes())?;
94
95     println!("Dependency paths injected: {}", manifest_path);
96
97     Ok(())
98 }