3803: Cleanup r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-04-01 10:34:36 +00:00 committed by GitHub
commit ab284f30ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 39 deletions

View File

@ -4,9 +4,9 @@
mod conv; mod conv;
use std::{ use std::{
error, fmt, env, error, fmt,
io::{BufRead, BufReader}, io::{BufRead, BufReader},
path::{Path, PathBuf}, path::PathBuf,
process::{Command, Stdio}, process::{Command, Stdio},
time::Instant, time::Instant,
}; };
@ -23,10 +23,10 @@ use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic};
pub use crate::conv::url_from_path_with_drive_lowercasing; pub use crate::conv::url_from_path_with_drive_lowercasing;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct CheckConfig { pub struct FlycheckConfig {
pub args: Vec<String>,
pub command: String, pub command: String,
pub all_targets: bool, pub all_targets: bool,
pub extra_args: Vec<String>,
} }
/// Flycheck wraps the shared state and communication machinery used for /// Flycheck wraps the shared state and communication machinery used for
@ -42,12 +42,11 @@ pub struct Flycheck {
} }
impl Flycheck { impl Flycheck {
pub fn new(config: CheckConfig, workspace_root: PathBuf) -> Flycheck { pub fn new(config: FlycheckConfig, workspace_root: PathBuf) -> Flycheck {
let (task_send, task_recv) = unbounded::<CheckTask>(); let (task_send, task_recv) = unbounded::<CheckTask>();
let (cmd_send, cmd_recv) = unbounded::<CheckCommand>(); let (cmd_send, cmd_recv) = unbounded::<CheckCommand>();
let handle = jod_thread::spawn(move || { let handle = jod_thread::spawn(move || {
let mut check = FlycheckThread::new(config, workspace_root); FlycheckThread::new(config, workspace_root).run(&task_send, &cmd_recv);
check.run(&task_send, &cmd_recv);
}); });
Flycheck { task_recv, cmd_send, handle } Flycheck { task_recv, cmd_send, handle }
} }
@ -76,7 +75,7 @@ pub enum CheckCommand {
} }
struct FlycheckThread { struct FlycheckThread {
options: CheckConfig, config: FlycheckConfig,
workspace_root: PathBuf, workspace_root: PathBuf,
last_update_req: Option<Instant>, last_update_req: Option<Instant>,
// XXX: drop order is significant // XXX: drop order is significant
@ -90,9 +89,9 @@ struct FlycheckThread {
} }
impl FlycheckThread { impl FlycheckThread {
fn new(options: CheckConfig, workspace_root: PathBuf) -> FlycheckThread { fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckThread {
FlycheckThread { FlycheckThread {
options, config,
workspace_root, workspace_root,
last_update_req: None, last_update_req: None,
message_recv: never(), message_recv: never(),
@ -216,27 +215,27 @@ impl FlycheckThread {
self.message_recv = never(); self.message_recv = never();
self.check_process = None; self.check_process = None;
let mut args: Vec<String> = vec![ let cmd = {
self.options.command.clone(), let mut cmd = Command::new(cargo_binary());
"--workspace".to_string(), cmd.arg(&self.config.command);
"--message-format=json".to_string(), cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]);
"--manifest-path".to_string(), cmd.arg(self.workspace_root.join("Cargo.toml"));
format!("{}/Cargo.toml", self.workspace_root.display()), if self.config.all_targets {
]; cmd.arg("--all-targets");
if self.options.all_targets { }
args.push("--all-targets".to_string()); cmd.args(self.config.extra_args.iter());
} cmd.current_dir(&self.workspace_root);
args.extend(self.options.args.iter().cloned()); cmd
};
let (message_send, message_recv) = unbounded(); let (message_send, message_recv) = unbounded();
let workspace_root = self.workspace_root.to_owned();
self.message_recv = message_recv; self.message_recv = message_recv;
self.check_process = Some(jod_thread::spawn(move || { self.check_process = Some(jod_thread::spawn(move || {
// If we trigger an error here, we will do so in the loop instead, // If we trigger an error here, we will do so in the loop instead,
// which will break out of the loop, and continue the shutdown // which will break out of the loop, and continue the shutdown
let _ = message_send.send(CheckEvent::Begin); let _ = message_send.send(CheckEvent::Begin);
let res = run_cargo(&args, Some(&workspace_root), &mut |message| { let res = run_cargo(cmd, &mut |message| {
// Skip certain kinds of messages to only spend time on what's useful // Skip certain kinds of messages to only spend time on what's useful
match &message { match &message {
Message::CompilerArtifact(artifact) if artifact.fresh => return true, Message::CompilerArtifact(artifact) if artifact.fresh => return true,
@ -285,17 +284,11 @@ impl fmt::Display for CargoError {
impl error::Error for CargoError {} impl error::Error for CargoError {}
fn run_cargo( fn run_cargo(
args: &[String], mut command: Command,
current_dir: Option<&Path>,
on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool, on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool,
) -> Result<(), CargoError> { ) -> Result<(), CargoError> {
let mut command = Command::new("cargo"); dbg!(&command);
if let Some(current_dir) = current_dir {
command.current_dir(current_dir);
}
let mut child = command let mut child = command
.args(args)
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::null()) .stderr(Stdio::null())
.stdin(Stdio::null()) .stdin(Stdio::null())
@ -346,9 +339,8 @@ fn run_cargo(
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment: // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298 // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
format!( format!(
"the command produced no valid metadata (exit code: {:?}): cargo {}", "the command produced no valid metadata (exit code: {:?}): {:?}",
exit_code, exit_code, command
args.join(" ")
) )
} }
Err(err) => format!("io error: {:?}", err), Err(err) => format!("io error: {:?}", err),
@ -357,3 +349,7 @@ fn run_cargo(
Err(CargoError(err_msg)) Err(CargoError(err_msg))
} }
fn cargo_binary() -> String {
env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
}

View File

@ -21,7 +21,7 @@ use lsp_types::{
WorkDoneProgressBegin, WorkDoneProgressCreateParams, WorkDoneProgressEnd, WorkDoneProgressBegin, WorkDoneProgressCreateParams, WorkDoneProgressEnd,
WorkDoneProgressReport, WorkDoneProgressReport,
}; };
use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckConfig, CheckTask}; use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckTask, FlycheckConfig};
use ra_ide::{Canceled, FileId, InlayHintsConfig, LibraryData, SourceRootId}; use ra_ide::{Canceled, FileId, InlayHintsConfig, LibraryData, SourceRootId};
use ra_prof::profile; use ra_prof::profile;
use ra_vfs::{VfsFile, VfsTask, Watch}; use ra_vfs::{VfsFile, VfsTask, Watch};
@ -102,10 +102,10 @@ fn get_config(
max_length: config.inlay_hints_max_length, max_length: config.inlay_hints_max_length,
}, },
check: if config.cargo_watch_enable { check: if config.cargo_watch_enable {
Some(CheckConfig { Some(FlycheckConfig {
args: config.cargo_watch_args.clone(),
command: config.cargo_watch_command.clone(), command: config.cargo_watch_command.clone(),
all_targets: config.cargo_watch_all_targets, all_targets: config.cargo_watch_all_targets,
extra_args: config.cargo_watch_args.clone(),
}) })
} else { } else {
None None

View File

@ -11,7 +11,7 @@ use std::{
use crossbeam_channel::{unbounded, Receiver}; use crossbeam_channel::{unbounded, Receiver};
use lsp_types::Url; use lsp_types::Url;
use parking_lot::RwLock; use parking_lot::RwLock;
use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckConfig, Flycheck}; use ra_flycheck::{url_from_path_with_drive_lowercasing, Flycheck, FlycheckConfig};
use ra_ide::{ use ra_ide::{
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayHintsConfig, LibraryData, Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayHintsConfig, LibraryData,
SourceRootId, SourceRootId,
@ -58,7 +58,7 @@ pub struct Config {
pub line_folding_only: bool, pub line_folding_only: bool,
pub inlay_hints: InlayHintsConfig, pub inlay_hints: InlayHintsConfig,
pub rustfmt_args: Vec<String>, pub rustfmt_args: Vec<String>,
pub check: Option<CheckConfig>, pub check: Option<FlycheckConfig>,
pub vscode_lldb: bool, pub vscode_lldb: bool,
pub proc_macro_srv: Option<String>, pub proc_macro_srv: Option<String>,
} }