Skip to content

Commit 65c89c3

Browse files
committed
Cleanup
1 parent b5306ea commit 65c89c3

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

crates/ra_flycheck/src/lib.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
mod conv;
55

66
use 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};
2323
pub 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

4444
impl 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

7877
struct 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

9291
impl 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 {
285284
impl error::Error for CargoError {}
286285

287286
fn 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+
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use lsp_types::{
2121
WorkDoneProgressBegin, WorkDoneProgressCreateParams, WorkDoneProgressEnd,
2222
WorkDoneProgressReport,
2323
};
24-
use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckConfig, CheckTask};
24+
use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckTask, FlycheckConfig};
2525
use ra_ide::{Canceled, FileId, InlayHintsConfig, LibraryData, SourceRootId};
2626
use ra_prof::profile;
2727
use ra_vfs::{VfsFile, VfsTask, Watch};
@@ -102,10 +102,10 @@ fn get_config(
102102
max_length: config.inlay_hints_max_length,
103103
},
104104
check: if config.cargo_watch_enable {
105-
Some(CheckConfig {
106-
args: config.cargo_watch_args.clone(),
105+
Some(FlycheckConfig {
107106
command: config.cargo_watch_command.clone(),
108107
all_targets: config.cargo_watch_all_targets,
108+
extra_args: config.cargo_watch_args.clone(),
109109
})
110110
} else {
111111
None

crates/rust-analyzer/src/world.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111
use crossbeam_channel::{unbounded, Receiver};
1212
use lsp_types::Url;
1313
use parking_lot::RwLock;
14-
use ra_flycheck::{url_from_path_with_drive_lowercasing, CheckConfig, Flycheck};
14+
use ra_flycheck::{url_from_path_with_drive_lowercasing, Flycheck, FlycheckConfig};
1515
use ra_ide::{
1616
Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayHintsConfig, LibraryData,
1717
SourceRootId,
@@ -58,7 +58,7 @@ pub struct Config {
5858
pub line_folding_only: bool,
5959
pub inlay_hints: InlayHintsConfig,
6060
pub rustfmt_args: Vec<String>,
61-
pub check: Option<CheckConfig>,
61+
pub check: Option<FlycheckConfig>,
6262
pub vscode_lldb: bool,
6363
pub proc_macro_srv: Option<String>,
6464
}

0 commit comments

Comments
 (0)