use which on formatter command (#8064)

This commit is contained in:
Ezekiel Warren 2023-08-30 07:51:03 -07:00 committed by GitHub
parent 7fffc0a5d1
commit 6bef982f2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -733,16 +733,16 @@ impl Document {
// We can't use anyhow::Result here since the output of the future has to be // We can't use anyhow::Result here since the output of the future has to be
// clonable to be used as shared future. So use a custom error type. // clonable to be used as shared future. So use a custom error type.
pub fn format(&self) -> Option<BoxFuture<'static, Result<Transaction, FormatterError>>> { pub fn format(&self) -> Option<BoxFuture<'static, Result<Transaction, FormatterError>>> {
if let Some(formatter) = self if let Some((fmt_cmd, fmt_args)) = self
.language_config() .language_config()
.and_then(|c| c.formatter.clone()) .and_then(|c| c.formatter.as_ref())
.filter(|formatter| which::which(&formatter.command).is_ok()) .and_then(|formatter| Some((which::which(&formatter.command).ok()?, &formatter.args)))
{ {
use std::process::Stdio; use std::process::Stdio;
let text = self.text().clone(); let text = self.text().clone();
let mut process = tokio::process::Command::new(&formatter.command); let mut process = tokio::process::Command::new(&fmt_cmd);
process process
.args(&formatter.args) .args(fmt_args)
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.stderr(Stdio::piped()); .stderr(Stdio::piped());
@ -751,7 +751,7 @@ impl Document {
let mut process = process let mut process = process
.spawn() .spawn()
.map_err(|e| FormatterError::SpawningFailed { .map_err(|e| FormatterError::SpawningFailed {
command: formatter.command.clone(), command: fmt_cmd.to_string_lossy().into(),
error: e.kind(), error: e.kind(),
})?; })?;
{ {