44mod conv;
55
66use std:: {
7- error, fmt,
7+ env , error, fmt,
88 io:: { BufRead , BufReader } ,
9- path:: { Path , PathBuf } ,
9+ path:: PathBuf ,
1010 process:: { Command , Stdio } ,
1111 time:: Instant ,
1212} ;
@@ -23,10 +23,10 @@ use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic};
2323pub use crate :: conv:: url_from_path_with_drive_lowercasing;
2424
2525#[ derive( Clone , Debug ) ]
26- pub struct CheckConfig {
27- pub args : Vec < String > ,
26+ pub struct FlycheckConfig {
2827 pub command : String ,
2928 pub all_targets : bool ,
29+ pub extra_args : Vec < String > ,
3030}
3131
3232/// Flycheck wraps the shared state and communication machinery used for
@@ -42,12 +42,11 @@ pub struct Flycheck {
4242}
4343
4444impl Flycheck {
45- pub fn new ( config : CheckConfig , workspace_root : PathBuf ) -> Flycheck {
45+ pub fn new ( config : FlycheckConfig , workspace_root : PathBuf ) -> Flycheck {
4646 let ( task_send, task_recv) = unbounded :: < CheckTask > ( ) ;
4747 let ( cmd_send, cmd_recv) = unbounded :: < CheckCommand > ( ) ;
4848 let handle = jod_thread:: spawn ( move || {
49- let mut check = FlycheckThread :: new ( config, workspace_root) ;
50- check. run ( & task_send, & cmd_recv) ;
49+ FlycheckThread :: new ( config, workspace_root) . run ( & task_send, & cmd_recv) ;
5150 } ) ;
5251 Flycheck { task_recv, cmd_send, handle }
5352 }
@@ -76,7 +75,7 @@ pub enum CheckCommand {
7675}
7776
7877struct FlycheckThread {
79- options : CheckConfig ,
78+ config : FlycheckConfig ,
8079 workspace_root : PathBuf ,
8180 last_update_req : Option < Instant > ,
8281 // XXX: drop order is significant
@@ -90,9 +89,9 @@ struct FlycheckThread {
9089}
9190
9291impl FlycheckThread {
93- fn new ( options : CheckConfig , workspace_root : PathBuf ) -> FlycheckThread {
92+ fn new ( config : FlycheckConfig , workspace_root : PathBuf ) -> FlycheckThread {
9493 FlycheckThread {
95- options ,
94+ config ,
9695 workspace_root,
9796 last_update_req : None ,
9897 message_recv : never ( ) ,
@@ -216,27 +215,27 @@ impl FlycheckThread {
216215 self . message_recv = never ( ) ;
217216 self . check_process = None ;
218217
219- let mut args: Vec < String > = vec ! [
220- self . options. command. clone( ) ,
221- "--workspace" . to_string( ) ,
222- "--message-format=json" . to_string( ) ,
223- "--manifest-path" . to_string( ) ,
224- format!( "{}/Cargo.toml" , self . workspace_root. display( ) ) ,
225- ] ;
226- if self . options . all_targets {
227- args. push ( "--all-targets" . to_string ( ) ) ;
228- }
229- args. extend ( self . options . args . iter ( ) . cloned ( ) ) ;
218+ let cmd = {
219+ let mut cmd = Command :: new ( cargo_binary ( ) ) ;
220+ cmd. arg ( & self . config . command ) ;
221+ cmd. args ( & [ "--workspace" , "--message-format=json" , "--manifest-path" ] ) ;
222+ cmd. arg ( self . workspace_root . join ( "Cargo.toml" ) ) ;
223+ if self . config . all_targets {
224+ cmd. arg ( "--all-targets" ) ;
225+ }
226+ cmd. args ( self . config . extra_args . iter ( ) ) ;
227+ cmd. current_dir ( & self . workspace_root ) ;
228+ cmd
229+ } ;
230230
231231 let ( message_send, message_recv) = unbounded ( ) ;
232- let workspace_root = self . workspace_root . to_owned ( ) ;
233232 self . message_recv = message_recv;
234233 self . check_process = Some ( jod_thread:: spawn ( move || {
235234 // If we trigger an error here, we will do so in the loop instead,
236235 // which will break out of the loop, and continue the shutdown
237236 let _ = message_send. send ( CheckEvent :: Begin ) ;
238237
239- let res = run_cargo ( & args , Some ( & workspace_root ) , & mut |message| {
238+ let res = run_cargo ( cmd , & mut |message| {
240239 // Skip certain kinds of messages to only spend time on what's useful
241240 match & message {
242241 Message :: CompilerArtifact ( artifact) if artifact. fresh => return true ,
@@ -285,17 +284,11 @@ impl fmt::Display for CargoError {
285284impl error:: Error for CargoError { }
286285
287286fn run_cargo (
288- args : & [ String ] ,
289- current_dir : Option < & Path > ,
287+ mut command : Command ,
290288 on_message : & mut dyn FnMut ( cargo_metadata:: Message ) -> bool ,
291289) -> Result < ( ) , CargoError > {
292- let mut command = Command :: new ( "cargo" ) ;
293- if let Some ( current_dir) = current_dir {
294- command. current_dir ( current_dir) ;
295- }
296-
290+ dbg ! ( & command) ;
297291 let mut child = command
298- . args ( args)
299292 . stdout ( Stdio :: piped ( ) )
300293 . stderr ( Stdio :: null ( ) )
301294 . stdin ( Stdio :: null ( ) )
@@ -346,9 +339,8 @@ fn run_cargo(
346339 // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
347340 // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
348341 format ! (
349- "the command produced no valid metadata (exit code: {:?}): cargo {}" ,
350- exit_code,
351- args. join( " " )
342+ "the command produced no valid metadata (exit code: {:?}): {:?}" ,
343+ exit_code, command
352344 )
353345 }
354346 Err ( err) => format ! ( "io error: {:?}" , err) ,
@@ -357,3 +349,7 @@ fn run_cargo(
357349
358350 Err ( CargoError ( err_msg) )
359351}
352+
353+ fn cargo_binary ( ) -> String {
354+ env:: var ( "CARGO" ) . unwrap_or_else ( |_| "cargo" . to_string ( ) )
355+ }
0 commit comments