Auto merge of #12680 - Alexendoo:cargo-dev-setup-toolchain, r=llogiq

Add `cargo dev setup toolchain`

Adds a `cargo dev setup toolchain` subcommand that creates a rustup toolchain with symlinks to the local `cargo-clippy` and `clippy-driver`. Allows you to then do `cargo +clippy clippy` in other projects to run the locally built Clippy

Sometimes more convenient when you're testing changes on a separate project than `cd`ing back & forth to use `cargo dev lint [project]`

changelog: none
This commit is contained in:
bors 2024-04-17 05:46:07 +00:00
commit 1ba78212ff
7 changed files with 131 additions and 29 deletions

View File

@ -18,7 +18,6 @@ because that's clearly a non-descriptive name.
- [Cargo lints](#cargo-lints) - [Cargo lints](#cargo-lints)
- [Rustfix tests](#rustfix-tests) - [Rustfix tests](#rustfix-tests)
- [Testing manually](#testing-manually) - [Testing manually](#testing-manually)
- [Running directly](#running-directly)
- [Lint declaration](#lint-declaration) - [Lint declaration](#lint-declaration)
- [Lint registration](#lint-registration) - [Lint registration](#lint-registration)
- [Lint passes](#lint-passes) - [Lint passes](#lint-passes)
@ -176,23 +175,26 @@ the tests.
Manually testing against an example file can be useful if you have added some Manually testing against an example file can be useful if you have added some
`println!`s and the test suite output becomes unreadable. To try Clippy with `println!`s and the test suite output becomes unreadable. To try Clippy with
your local modifications, run your local modifications, run the following from the Clippy directory:
``` ```bash
cargo dev lint input.rs cargo dev lint input.rs
``` ```
from the working copy root. With tests in place, let's have a look at To run Clippy on an existing project rather than a single file you can use
implementing our lint now.
## Running directly ```bash
cargo dev lint /path/to/project
```
While it's easier to just use `cargo dev lint`, it might be desirable to get Or set up a rustup toolchain that points to the local Clippy binaries
`target/release/cargo-clippy` and `target/release/clippy-driver` to work as well in some cases.
By default, they don't work because clippy dynamically links rustc. To help them find rustc, ```bash
add the path printed by`rustc --print target-libdir` (ran inside this workspace so that the rustc version matches) cargo dev setup toolchain
to your library search path.
On linux, this can be done by setting the `LD_LIBRARY_PATH` environment variable to that path. # Then in `/path/to/project` you can run
cargo +clippy clippy
```
## Lint declaration ## Lint declaration

View File

@ -35,7 +35,6 @@ struct FmtContext {
} }
// the "main" function of cargo dev fmt // the "main" function of cargo dev fmt
#[allow(clippy::missing_panics_doc)]
pub fn run(check: bool, verbose: bool) { pub fn run(check: bool, verbose: bool) {
fn try_run(context: &FmtContext) -> Result<bool, CliError> { fn try_run(context: &FmtContext) -> Result<bool, CliError> {
let mut success = true; let mut success = true;

View File

@ -9,6 +9,7 @@
unused_lifetimes, unused_lifetimes,
unused_qualifications unused_qualifications
)] )]
#![allow(clippy::missing_panics_doc)]
// The `rustc_driver` crate seems to be required in order to use the `rust_lexer` crate. // The `rustc_driver` crate seems to be required in order to use the `rust_lexer` crate.
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]

View File

@ -46,6 +46,13 @@ fn main() {
} }
}, },
Some(("setup", sub_command)) => match sub_command.subcommand() { Some(("setup", sub_command)) => match sub_command.subcommand() {
Some(("git-hook", matches)) => {
if matches.get_flag("remove") {
setup::git_hook::remove_hook();
} else {
setup::git_hook::install_hook(matches.get_flag("force-override"));
}
},
Some(("intellij", matches)) => { Some(("intellij", matches)) => {
if matches.get_flag("remove") { if matches.get_flag("remove") {
setup::intellij::remove_rustc_src(); setup::intellij::remove_rustc_src();
@ -57,12 +64,12 @@ fn main() {
); );
} }
}, },
Some(("git-hook", matches)) => { Some(("toolchain", matches)) => {
if matches.get_flag("remove") { setup::toolchain::create(
setup::git_hook::remove_hook(); matches.get_flag("force"),
} else { matches.get_flag("release"),
setup::git_hook::install_hook(matches.get_flag("force-override")); matches.get_one::<String>("name").unwrap(),
} );
}, },
Some(("vscode-tasks", matches)) => { Some(("vscode-tasks", matches)) => {
if matches.get_flag("remove") { if matches.get_flag("remove") {
@ -210,6 +217,19 @@ fn get_clap_config() -> ArgMatches {
.about("Support for setting up your personal development environment") .about("Support for setting up your personal development environment")
.arg_required_else_help(true) .arg_required_else_help(true)
.subcommands([ .subcommands([
Command::new("git-hook")
.about("Add a pre-commit git hook that formats your code to make it look pretty")
.args([
Arg::new("remove")
.long("remove")
.action(ArgAction::SetTrue)
.help("Remove the pre-commit hook added with 'cargo dev setup git-hook'"),
Arg::new("force-override")
.long("force-override")
.short('f')
.action(ArgAction::SetTrue)
.help("Forces the override of an existing git pre-commit hook"),
]),
Command::new("intellij") Command::new("intellij")
.about("Alter dependencies so Intellij Rust can find rustc internals") .about("Alter dependencies so Intellij Rust can find rustc internals")
.args([ .args([
@ -225,18 +245,23 @@ fn get_clap_config() -> ArgMatches {
.conflicts_with("remove") .conflicts_with("remove")
.required(true), .required(true),
]), ]),
Command::new("git-hook") Command::new("toolchain")
.about("Add a pre-commit git hook that formats your code to make it look pretty") .about("Install a rustup toolchain pointing to the local clippy build")
.args([ .args([
Arg::new("remove") Arg::new("force")
.long("remove") .long("force")
.action(ArgAction::SetTrue)
.help("Remove the pre-commit hook added with 'cargo dev setup git-hook'"),
Arg::new("force-override")
.long("force-override")
.short('f') .short('f')
.action(ArgAction::SetTrue) .action(ArgAction::SetTrue)
.help("Forces the override of an existing git pre-commit hook"), .help("Override an existing toolchain"),
Arg::new("release")
.long("release")
.short('r')
.action(ArgAction::SetTrue)
.help("Point to --release clippy binaries"),
Arg::new("name")
.long("name")
.default_value("clippy")
.help("The name of the created toolchain"),
]), ]),
Command::new("vscode-tasks") Command::new("vscode-tasks")
.about("Add several tasks to vscode for formatting, validation and testing") .about("Add several tasks to vscode for formatting, validation and testing")

View File

@ -36,7 +36,6 @@ impl<T> Context for io::Result<T> {
/// # Errors /// # Errors
/// ///
/// This function errors out if the files couldn't be created or written to. /// This function errors out if the files couldn't be created or written to.
#[allow(clippy::missing_panics_doc)]
pub fn create( pub fn create(
pass: &String, pass: &String,
lint_name: Option<&String>, lint_name: Option<&String>,

View File

@ -1,5 +1,6 @@
pub mod git_hook; pub mod git_hook;
pub mod intellij; pub mod intellij;
pub mod toolchain;
pub mod vscode; pub mod vscode;
use std::path::Path; use std::path::Path;

View File

@ -0,0 +1,75 @@
use std::env::consts::EXE_SUFFIX;
use std::env::current_dir;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
use super::verify_inside_clippy_dir;
pub fn create(force: bool, release: bool, name: &str) {
if !verify_inside_clippy_dir() {
return;
}
let rustup_home = std::env::var("RUSTUP_HOME").unwrap();
let toolchain = std::env::var("RUSTUP_TOOLCHAIN").unwrap();
let src = PathBuf::from_iter([&rustup_home, "toolchains", &toolchain]);
let dest = PathBuf::from_iter([&rustup_home, "toolchains", name]);
if dest.exists() {
if force {
fs::remove_dir_all(&dest).unwrap();
} else {
println!("{} already exists, pass `--force` to override it", dest.display());
return;
}
}
for entry in WalkDir::new(&src) {
let entry = entry.unwrap();
let relative = entry.path().strip_prefix(&src).unwrap();
if relative.starts_with("bin")
&& matches!(
relative.file_stem().and_then(OsStr::to_str),
Some("cargo-clippy" | "clippy-driver")
)
{
continue;
}
let target = dest.join(relative);
if entry.file_type().is_dir() {
fs::create_dir(&target).unwrap();
} else {
fs::hard_link(entry.path(), target).unwrap();
}
}
symlink_bin("cargo-clippy", &dest, release);
symlink_bin("clippy-driver", &dest, release);
println!("Created toolchain {name}, use it in other projects with e.g. `cargo +{name} clippy`");
println!("Note: This will need to be re-run whenever the Clippy `rust-toolchain` changes");
}
fn symlink_bin(bin: &str, dest: &Path, release: bool) {
#[cfg(windows)]
use std::os::windows::fs::symlink_file as symlink;
#[cfg(not(windows))]
use std::os::unix::fs::symlink;
let profile = if release { "release" } else { "debug" };
let file_name = format!("{bin}{EXE_SUFFIX}");
let mut src = current_dir().unwrap();
src.extend(["target", profile, &file_name]);
let mut dest = dest.to_path_buf();
dest.extend(["bin", &file_name]);
symlink(src, dest).unwrap();
}