|
1 | 1 | use std::env; |
2 | | -use std::process::Command; |
| 2 | +use std::ffi::{OsStr, OsString}; |
| 3 | +use std::io::Write; |
| 4 | +use std::path::Path; |
| 5 | +use std::process::{Command, CommandEnvs}; |
3 | 6 |
|
4 | 7 | use camino::{Utf8Path, Utf8PathBuf}; |
| 8 | +use tempfile::NamedTempFile; |
5 | 9 |
|
6 | 10 | #[cfg(test)] |
7 | 11 | mod tests; |
@@ -149,3 +153,112 @@ macro_rules! string_enum { |
149 | 153 | } |
150 | 154 |
|
151 | 155 | pub(crate) use string_enum; |
| 156 | + |
| 157 | +/// A wrapper around [`Command`] that adds support for arg files. |
| 158 | +/// This is useful as we have some commands that can get very long and at times |
| 159 | +/// hit the OS limit (usually Windows) |
| 160 | +/// |
| 161 | +/// This implementation is based off the of `ProcessBuilder` implementation in Cargo |
| 162 | +/// but simplified. |
| 163 | +/// |
| 164 | +/// NOTE: In most scenarios we want to avoid arg files as it makes debugging more complicated |
| 165 | +/// so we try to avoid it if the command is not close the the OS limit. |
| 166 | +#[derive(Debug)] |
| 167 | +pub(crate) struct ArgFileCommand { |
| 168 | + command: Command, |
| 169 | + args: Vec<OsString>, |
| 170 | +} |
| 171 | + |
| 172 | +#[allow(dead_code)] // Roughly match the `std::process::Command` API |
| 173 | +impl ArgFileCommand { |
| 174 | + #[track_caller] |
| 175 | + pub(crate) fn new<S: AsRef<OsStr>>(program: S) -> Self { |
| 176 | + let command = Command::new(program); |
| 177 | + Self { command, args: Vec::new() } |
| 178 | + } |
| 179 | + pub(crate) fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self { |
| 180 | + self.args.push(arg.as_ref().to_os_string()); |
| 181 | + self |
| 182 | + } |
| 183 | + |
| 184 | + pub(crate) fn args<I, S>(&mut self, args: I) -> &mut Self |
| 185 | + where |
| 186 | + I: IntoIterator<Item = S>, |
| 187 | + S: AsRef<OsStr>, |
| 188 | + { |
| 189 | + self.args.extend(args.into_iter().map(|s| s.as_ref().to_os_string())); |
| 190 | + self |
| 191 | + } |
| 192 | + |
| 193 | + pub(crate) fn env<K, V>(&mut self, key: K, val: V) -> &mut Self |
| 194 | + where |
| 195 | + K: AsRef<OsStr>, |
| 196 | + V: AsRef<OsStr>, |
| 197 | + { |
| 198 | + self.command.env(key, val); |
| 199 | + self |
| 200 | + } |
| 201 | + |
| 202 | + pub(crate) fn get_envs(&self) -> CommandEnvs<'_> { |
| 203 | + self.command.get_envs() |
| 204 | + } |
| 205 | + |
| 206 | + pub(crate) fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self { |
| 207 | + self.command.env_remove(key); |
| 208 | + self |
| 209 | + } |
| 210 | + |
| 211 | + pub(crate) fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self { |
| 212 | + self.command.current_dir(dir); |
| 213 | + self |
| 214 | + } |
| 215 | + |
| 216 | + pub(crate) fn stdin(&mut self, stdin: std::process::Stdio) -> &mut Self { |
| 217 | + self.command.stdin(stdin); |
| 218 | + self |
| 219 | + } |
| 220 | + |
| 221 | + pub(crate) fn build(mut self) -> std::io::Result<(Command, NamedTempFile)> { |
| 222 | + let mut tmp = tempfile::Builder::new().prefix("compiletest-argfile.").tempfile()?; |
| 223 | + |
| 224 | + // On Windows there is a hard limit of ~32KB, so we cut off at 30KB to |
| 225 | + // give some buffer just incase. |
| 226 | + #[cfg(windows)] |
| 227 | + let threshold: usize = 30 * 1024; |
| 228 | + // On unix the limit is defined by ARG_MAX. If its not explicitly set we set it to 1MB |
| 229 | + // which is fairly large but lower than the ~2MB that it defaults to on most systems. |
| 230 | + #[cfg(unix)] |
| 231 | + let threshold: usize = |
| 232 | + std::env::var("ARG_MAX").ok().and_then(|v| v.parse().ok()).unwrap_or(1024 * 1024); |
| 233 | + |
| 234 | + let total_arg_len: usize = self.args.iter().map(|a| a.len() + 1).sum(); |
| 235 | + if total_arg_len <= threshold { |
| 236 | + self.command.args(self.args); |
| 237 | + return Ok((self.command, tmp)); |
| 238 | + } |
| 239 | + |
| 240 | + let mut arg = OsString::from("@"); |
| 241 | + arg.push(tmp.path()); |
| 242 | + self.command.arg(arg); |
| 243 | + |
| 244 | + let cap = self.args.iter().map(|arg| arg.len() + 1).sum::<usize>(); |
| 245 | + let mut buf = Vec::with_capacity(cap); |
| 246 | + for arg in &self.args { |
| 247 | + let arg = arg.to_str().ok_or_else(|| { |
| 248 | + std::io::Error::other(format!( |
| 249 | + "argument for argfile contains invalid UTF-8 characters: `{}`", |
| 250 | + arg.to_string_lossy() |
| 251 | + )) |
| 252 | + })?; |
| 253 | + if arg.contains('\n') { |
| 254 | + return Err(std::io::Error::other(format!( |
| 255 | + "argument for argfile contains newlines: `{arg}`" |
| 256 | + ))); |
| 257 | + } |
| 258 | + writeln!(buf, "{arg}")?; |
| 259 | + } |
| 260 | + tmp.write_all(&buf)?; |
| 261 | + |
| 262 | + Ok((self.command, tmp)) |
| 263 | + } |
| 264 | +} |
0 commit comments