Skip to content

Commit c17a2a9

Browse files
committed
clippy_dev: add ra_setup
This takes an absolute path to a rustc repo and adds path-dependencies that point towards the respective rustc subcrates into the Cargo.tomls of the clippy and clippy_lints crate. This allows rustc-analyzer to show proper type annotations etc on rustc-internals inside the clippy repo. Usage: cargo dev ra-setup /absolute/path/to/rust/ cc rust-lang/rust-analyzer#3517 cc #5514
1 parent 7ea7cd1 commit c17a2a9

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

clippy_dev/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use walkdir::WalkDir;
1111

1212
pub mod fmt;
1313
pub mod new_lint;
14+
pub mod ra_setup;
1415
pub mod stderr_length_check;
1516
pub mod update_lints;
1617

clippy_dev/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22

33
use clap::{App, Arg, SubCommand};
4-
use clippy_dev::{fmt, new_lint, stderr_length_check, update_lints};
4+
use clippy_dev::{fmt, new_lint, ra_setup, stderr_length_check, update_lints};
55

66
fn main() {
77
let matches = App::new("Clippy developer tooling")
@@ -87,6 +87,19 @@ fn main() {
8787
SubCommand::with_name("limit_stderr_length")
8888
.about("Ensures that stderr files do not grow longer than a certain amount of lines."),
8989
)
90+
.subcommand(
91+
SubCommand::with_name("ra-setup")
92+
.about("Alter dependencies so rust-analyzer can find rustc internals")
93+
.arg(
94+
Arg::with_name("rustc-repo-path")
95+
.long("repo-path")
96+
.short("r")
97+
.help("The path to a rustc repo that will be used for setting the dependencies")
98+
.takes_value(true)
99+
.value_name("path")
100+
.required(true),
101+
),
102+
)
90103
.get_matches();
91104

92105
match matches.subcommand() {
@@ -115,6 +128,7 @@ fn main() {
115128
("limit_stderr_length", _) => {
116129
stderr_length_check::check();
117130
},
131+
("ra-setup", Some(matches)) => ra_setup::run(matches.value_of("rustc-repo-path")),
118132
_ => {},
119133
}
120134
}

clippy_dev/src/ra_setup.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::fs;
2+
use std::fs::File;
3+
use std::io::prelude::*;
4+
use std::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 here
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("src");
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: &PathBuf,
46+
manifest_path: &str,
47+
cargo_toml: String,
48+
lib_rs: String,
49+
) -> std::io::Result<()> {
50+
let extern_crates = lib_rs
51+
.lines()
52+
// get the deps
53+
.filter(|line| line.starts_with("extern crate"))
54+
// we have something like "extern crate foo;", we only care about the "foo"
55+
// ↓ ↓
56+
// extern crate rustc_middle;
57+
.map(|s| &s[13..(s.len() - 1)]);
58+
59+
let new_deps = extern_crates.map(|dep| {
60+
// format the dependencies that are going to be put inside the Cargo.toml
61+
format!(
62+
"{dep} = {{ path = \"{source_path}/lib{dep}\" }}\n",
63+
dep = dep,
64+
source_path = rustc_source_dir.display()
65+
)
66+
});
67+
68+
// format a new [dependencies]-block with the new deps we need to inject
69+
let mut all_deps = String::from("[dependencies]\n");
70+
new_deps.for_each(|dep_line| {
71+
all_deps.push_str(&dep_line);
72+
});
73+
74+
// replace "[dependencies]" with
75+
// [dependencies]
76+
// dep1 = { path = ... }
77+
// dep2 = { path = ... }
78+
// etc
79+
let new_manifest = cargo_toml.replacen("[dependencies]\n", &all_deps, 1);
80+
81+
// println!("{}", new_manifest);
82+
let mut file = File::create(manifest_path)?;
83+
file.write_all(new_manifest.as_bytes())?;
84+
85+
println!("Dependency paths injected: {}", manifest_path);
86+
87+
Ok(())
88+
}

0 commit comments

Comments
 (0)