aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_flycheck/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_flycheck/src/lib.rs')
-rw-r--r--crates/ra_flycheck/src/lib.rs64
1 files changed, 30 insertions, 34 deletions
diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs
index 75aece45f..ee09d8de3 100644
--- a/crates/ra_flycheck/src/lib.rs
+++ b/crates/ra_flycheck/src/lib.rs
@@ -4,9 +4,9 @@
4mod conv; 4mod conv;
5 5
6use std::{ 6use std::{
7 error, fmt, 7 env, error, fmt,
8 io::{BufRead, BufReader}, 8 io::{BufRead, BufReader},
9 path::{Path, PathBuf}, 9 path::PathBuf,
10 process::{Command, Stdio}, 10 process::{Command, Stdio},
11 time::Instant, 11 time::Instant,
12}; 12};
@@ -23,10 +23,10 @@ use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic};
23pub use crate::conv::url_from_path_with_drive_lowercasing; 23pub use crate::conv::url_from_path_with_drive_lowercasing;
24 24
25#[derive(Clone, Debug)] 25#[derive(Clone, Debug)]
26pub struct CheckConfig { 26pub struct FlycheckConfig {
27 pub args: Vec<String>,
28 pub command: String, 27 pub command: String,
29 pub all_targets: bool, 28 pub all_targets: bool,
29 pub extra_args: Vec<String>,
30} 30}
31 31
32/// Flycheck wraps the shared state and communication machinery used for 32/// Flycheck wraps the shared state and communication machinery used for
@@ -42,12 +42,11 @@ pub struct Flycheck {
42} 42}
43 43
44impl Flycheck { 44impl Flycheck {
45 pub fn new(config: CheckConfig, workspace_root: PathBuf) -> Flycheck { 45 pub fn new(config: FlycheckConfig, workspace_root: PathBuf) -> Flycheck {
46 let (task_send, task_recv) = unbounded::<CheckTask>(); 46 let (task_send, task_recv) = unbounded::<CheckTask>();
47 let (cmd_send, cmd_recv) = unbounded::<CheckCommand>(); 47 let (cmd_send, cmd_recv) = unbounded::<CheckCommand>();
48 let handle = jod_thread::spawn(move || { 48 let handle = jod_thread::spawn(move || {
49 let mut check = FlycheckThread::new(config, workspace_root); 49 FlycheckThread::new(config, workspace_root).run(&task_send, &cmd_recv);
50 check.run(&task_send, &cmd_recv);
51 }); 50 });
52 Flycheck { task_recv, cmd_send, handle } 51 Flycheck { task_recv, cmd_send, handle }
53 } 52 }
@@ -76,7 +75,7 @@ pub enum CheckCommand {
76} 75}
77 76
78struct FlycheckThread { 77struct FlycheckThread {
79 options: CheckConfig, 78 config: FlycheckConfig,
80 workspace_root: PathBuf, 79 workspace_root: PathBuf,
81 last_update_req: Option<Instant>, 80 last_update_req: Option<Instant>,
82 // XXX: drop order is significant 81 // XXX: drop order is significant
@@ -90,9 +89,9 @@ struct FlycheckThread {
90} 89}
91 90
92impl FlycheckThread { 91impl FlycheckThread {
93 fn new(options: CheckConfig, workspace_root: PathBuf) -> FlycheckThread { 92 fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckThread {
94 FlycheckThread { 93 FlycheckThread {
95 options, 94 config,
96 workspace_root, 95 workspace_root,
97 last_update_req: None, 96 last_update_req: None,
98 message_recv: never(), 97 message_recv: never(),
@@ -216,27 +215,27 @@ impl FlycheckThread {
216 self.message_recv = never(); 215 self.message_recv = never();
217 self.check_process = None; 216 self.check_process = None;
218 217
219 let mut args: Vec<String> = vec![ 218 let cmd = {
220 self.options.command.clone(), 219 let mut cmd = Command::new(cargo_binary());
221 "--workspace".to_string(), 220 cmd.arg(&self.config.command);
222 "--message-format=json".to_string(), 221 cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]);
223 "--manifest-path".to_string(), 222 cmd.arg(self.workspace_root.join("Cargo.toml"));
224 format!("{}/Cargo.toml", self.workspace_root.display()), 223 if self.config.all_targets {
225 ]; 224 cmd.arg("--all-targets");
226 if self.options.all_targets { 225 }
227 args.push("--all-targets".to_string()); 226 cmd.args(self.config.extra_args.iter());
228 } 227 cmd.current_dir(&self.workspace_root);
229 args.extend(self.options.args.iter().cloned()); 228 cmd
229 };
230 230
231 let (message_send, message_recv) = unbounded(); 231 let (message_send, message_recv) = unbounded();
232 let workspace_root = self.workspace_root.to_owned();
233 self.message_recv = message_recv; 232 self.message_recv = message_recv;
234 self.check_process = Some(jod_thread::spawn(move || { 233 self.check_process = Some(jod_thread::spawn(move || {
235 // If we trigger an error here, we will do so in the loop instead, 234 // If we trigger an error here, we will do so in the loop instead,
236 // which will break out of the loop, and continue the shutdown 235 // which will break out of the loop, and continue the shutdown
237 let _ = message_send.send(CheckEvent::Begin); 236 let _ = message_send.send(CheckEvent::Begin);
238 237
239 let res = run_cargo(&args, Some(&workspace_root), &mut |message| { 238 let res = run_cargo(cmd, &mut |message| {
240 // Skip certain kinds of messages to only spend time on what's useful 239 // Skip certain kinds of messages to only spend time on what's useful
241 match &message { 240 match &message {
242 Message::CompilerArtifact(artifact) if artifact.fresh => return true, 241 Message::CompilerArtifact(artifact) if artifact.fresh => return true,
@@ -285,17 +284,11 @@ impl fmt::Display for CargoError {
285impl error::Error for CargoError {} 284impl error::Error for CargoError {}
286 285
287fn run_cargo( 286fn run_cargo(
288 args: &[String], 287 mut command: Command,
289 current_dir: Option<&Path>,
290 on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool, 288 on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool,
291) -> Result<(), CargoError> { 289) -> Result<(), CargoError> {
292 let mut command = Command::new("cargo"); 290 dbg!(&command);
293 if let Some(current_dir) = current_dir {
294 command.current_dir(current_dir);
295 }
296
297 let mut child = command 291 let mut child = command
298 .args(args)
299 .stdout(Stdio::piped()) 292 .stdout(Stdio::piped())
300 .stderr(Stdio::null()) 293 .stderr(Stdio::null())
301 .stdin(Stdio::null()) 294 .stdin(Stdio::null())
@@ -346,9 +339,8 @@ fn run_cargo(
346 // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment: 339 // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
347 // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298 340 // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
348 format!( 341 format!(
349 "the command produced no valid metadata (exit code: {:?}): cargo {}", 342 "the command produced no valid metadata (exit code: {:?}): {:?}",
350 exit_code, 343 exit_code, command
351 args.join(" ")
352 ) 344 )
353 } 345 }
354 Err(err) => format!("io error: {:?}", err), 346 Err(err) => format!("io error: {:?}", err),
@@ -357,3 +349,7 @@ fn run_cargo(
357 349
358 Err(CargoError(err_msg)) 350 Err(CargoError(err_msg))
359} 351}
352
353fn cargo_binary() -> String {
354 env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
355}