Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions crates/rust-analyzer/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

use std::{
ffi::OsString,
fmt, io,
fmt,
io::{self, BufWriter, Write},
marker::PhantomData,
path::PathBuf,
process::{ChildStderr, ChildStdout, Command, Stdio},
};

use crossbeam_channel::Sender;
use paths::Utf8PathBuf;
use process_wrap::std::{StdChildWrapper, StdCommandWrap};
use stdx::process::streaming_output;

Expand Down Expand Up @@ -40,7 +42,7 @@ impl<T: Sized + Send + 'static> CargoActor<T> {
}

impl<T: Sized + Send + 'static> CargoActor<T> {
fn run(self) -> io::Result<(bool, String)> {
fn run(self, outfile: Option<Utf8PathBuf>) -> io::Result<(bool, String)> {
// We manually read a line at a time, instead of using serde's
// stream deserializers, because the deserializer cannot recover
// from an error, resulting in it getting stuck, because we try to
Expand All @@ -50,6 +52,15 @@ impl<T: Sized + Send + 'static> CargoActor<T> {
// simply skip a line if it doesn't parse, which just ignores any
// erroneous output.

let mut stdout = outfile.as_ref().and_then(|path| {
_ = std::fs::create_dir_all(path);
Some(BufWriter::new(std::fs::File::create(path.join("stdout")).ok()?))
});
let mut stderr = outfile.as_ref().and_then(|path| {
_ = std::fs::create_dir_all(path);
Some(BufWriter::new(std::fs::File::create(path.join("stderr")).ok()?))
});

let mut stdout_errors = String::new();
let mut stderr_errors = String::new();
let mut read_at_least_one_stdout_message = false;
Expand All @@ -67,11 +78,19 @@ impl<T: Sized + Send + 'static> CargoActor<T> {
self.stdout,
self.stderr,
&mut |line| {
if let Some(stdout) = &mut stdout {
_ = stdout.write_all(line.as_bytes());
_ = stdout.write_all(b"\n");
}
if process_line(line, &mut stdout_errors) {
read_at_least_one_stdout_message = true;
}
},
&mut |line| {
if let Some(stderr) = &mut stderr {
_ = stderr.write_all(line.as_bytes());
_ = stderr.write_all(b"\n");
}
if process_line(line, &mut stderr_errors) {
read_at_least_one_stderr_message = true;
}
Expand Down Expand Up @@ -130,6 +149,7 @@ impl<T: Sized + Send + 'static> CommandHandle<T> {
mut command: Command,
parser: impl CargoParser<T>,
sender: Sender<T>,
out_file: Option<Utf8PathBuf>,
) -> std::io::Result<Self> {
command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());

Expand All @@ -150,7 +170,7 @@ impl<T: Sized + Send + 'static> CommandHandle<T> {
let actor = CargoActor::<T>::new(parser, sender, stdout, stderr);
let thread =
stdx::thread::Builder::new(stdx::thread::ThreadIntent::Worker, "CommandHandle")
.spawn(move || actor.run())
.spawn(move || actor.run(out_file))
.expect("failed to spawn thread");
Ok(CommandHandle { program, arguments, current_dir, child, thread, _phantom: PhantomData })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl DiscoverCommand {
cmd.args(args);

Ok(DiscoverHandle {
_handle: CommandHandle::spawn(cmd, DiscoverProjectParser, self.sender.clone())?,
_handle: CommandHandle::spawn(cmd, DiscoverProjectParser, self.sender.clone(), None)?,
span: info_span!("discover_command").entered(),
})
}
Expand Down
17 changes: 16 additions & 1 deletion crates/rust-analyzer/src/flycheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,21 @@ impl FlycheckActor {

tracing::debug!(?command, "will restart flycheck");
let (sender, receiver) = unbounded();
match CommandHandle::spawn(command, CargoCheckParser, sender) {
match CommandHandle::spawn(
command,
CargoCheckParser,
sender,
match &self.config {
FlycheckConfig::CargoCommand { options, .. } => Some(
options
.target_dir
.as_deref()
.unwrap_or("target".as_ref())
.join(format!("rust-analyzer/flycheck{}", self.id)),
),
_ => None,
},
) {
Ok(command_handle) => {
tracing::debug!(command = formatted_command, "did restart flycheck");
self.command_handle = Some(command_handle);
Expand Down Expand Up @@ -622,6 +636,7 @@ impl FlycheckActor {
{
cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(sysroot_root));
}
cmd.env("CARGO_LOG", "cargo::core::compiler::fingerprint=info");
cmd.arg(command);

match scope {
Expand Down
7 changes: 6 additions & 1 deletion crates/rust-analyzer/src/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ impl CargoTestHandle {
}

Ok(Self {
_handle: CommandHandle::spawn(cmd, CargoTestOutputParser::new(&test_target), sender)?,
_handle: CommandHandle::spawn(
cmd,
CargoTestOutputParser::new(&test_target),
sender,
None,
)?,
})
}
}