|
4 | 4 | use std::{
|
5 | 5 | fs,
|
6 | 6 | io::Write as _,
|
| 7 | + path::PathBuf, |
7 | 8 | process::{self, Stdio},
|
8 | 9 | };
|
9 | 10 |
|
@@ -1995,14 +1996,52 @@ fn run_rustfmt(
|
1995 | 1996 | cmd
|
1996 | 1997 | }
|
1997 | 1998 | RustfmtConfig::CustomCommand { command, args } => {
|
1998 |
| - let mut cmd = process::Command::new(command); |
| 1999 | + let cmd = PathBuf::from(&command); |
| 2000 | + let mut components = cmd.components(); |
| 2001 | + |
| 2002 | + // to support rustc's suggested, default configuration |
| 2003 | + let mut cmd = match components.next() { |
| 2004 | + Some(std::path::Component::CurDir) => { |
| 2005 | + let rest = components.as_path(); |
| 2006 | + |
| 2007 | + let roots = snap |
| 2008 | + .workspaces |
| 2009 | + .iter() |
| 2010 | + .flat_map(|ws| ws.workspace_definition_path()) |
| 2011 | + .collect::<Vec<&AbsPath>>(); |
| 2012 | + |
| 2013 | + let abs: Option<AbsPathBuf> = roots.into_iter().find_map(|base| { |
| 2014 | + let abs = base.join(rest); |
| 2015 | + std::fs::metadata(&abs).ok().map(|_| abs) |
| 2016 | + }); |
| 2017 | + |
| 2018 | + let command = match abs { |
| 2019 | + Some(cmd) => cmd, |
| 2020 | + None => { |
| 2021 | + tracing::error!( |
| 2022 | + rustfmt = ?command, |
| 2023 | + "Unable to make the format command an absolute path" |
| 2024 | + ); |
| 2025 | + anyhow::bail!( |
| 2026 | + "Unable to make the format command an absolute path: {}", |
| 2027 | + command |
| 2028 | + ); |
| 2029 | + } |
| 2030 | + }; |
| 2031 | + |
| 2032 | + process::Command::new(&command.as_os_str()) |
| 2033 | + } |
| 2034 | + _ => process::Command::new(command), |
| 2035 | + }; |
1999 | 2036 |
|
2000 | 2037 | cmd.envs(snap.config.extra_env());
|
2001 | 2038 | cmd.args(args);
|
2002 | 2039 | cmd
|
2003 | 2040 | }
|
2004 | 2041 | };
|
2005 | 2042 |
|
| 2043 | + tracing::debug!(?command, "created format command"); |
| 2044 | + |
2006 | 2045 | // try to chdir to the file so we can respect `rustfmt.toml`
|
2007 | 2046 | // FIXME: use `rustfmt --config-path` once
|
2008 | 2047 | // https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
|
|
0 commit comments