|
| 1 | +use std::{ |
| 2 | + cell::RefCell, |
| 3 | + collections::BTreeMap, |
| 4 | + ffi::OsStr, |
| 5 | + io::{self, Read, Write}, |
| 6 | + sync::Arc, |
| 7 | +}; |
| 8 | + |
| 9 | +use native_str::NativeStr; |
| 10 | +use vite_path::{self, AbsolutePath}; |
| 11 | +use vite_task_ipc_shared::{Ack, GetEnvResponse, GetEnvsResponse, IPC_ENV_NAME, Request}; |
| 12 | + |
| 13 | +#[cfg(unix)] |
| 14 | +type Stream = std::os::unix::net::UnixStream; |
| 15 | +#[cfg(windows)] |
| 16 | +type Stream = std::fs::File; |
| 17 | + |
| 18 | +pub struct Client { |
| 19 | + stream: RefCell<Stream>, |
| 20 | + scratch: RefCell<Vec<u8>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl Client { |
| 24 | + /// Scans `envs` for the runner's IPC connection info and connects if |
| 25 | + /// present. Typical callers pass `std::env::vars_os()`. |
| 26 | + /// |
| 27 | + /// Returns `Ok(None)` if the IPC env is absent (running outside the runner). |
| 28 | + /// `Err(..)` if the env is set but connecting fails. |
| 29 | + /// |
| 30 | + /// # Errors |
| 31 | + /// |
| 32 | + /// Returns an error if the env var is set but the server cannot be reached. |
| 33 | + pub fn from_envs( |
| 34 | + envs: impl Iterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>, |
| 35 | + ) -> io::Result<Option<Self>> { |
| 36 | + for (name, value) in envs { |
| 37 | + if name.as_ref() == IPC_ENV_NAME { |
| 38 | + let stream = connect(value.as_ref())?; |
| 39 | + return Ok(Some(Self::from_stream(stream))); |
| 40 | + } |
| 41 | + } |
| 42 | + Ok(None) |
| 43 | + } |
| 44 | + |
| 45 | + const fn from_stream(stream: Stream) -> Self { |
| 46 | + Self { stream: RefCell::new(stream), scratch: RefCell::new(Vec::new()) } |
| 47 | + } |
| 48 | + |
| 49 | + /// `path` can be a file or a directory; for a directory, all files inside |
| 50 | + /// it are ignored. Relative paths are resolved against the current working |
| 51 | + /// directory before being sent to the runner. |
| 52 | + /// |
| 53 | + /// # Errors |
| 54 | + /// |
| 55 | + /// Returns an error if the request fails to send, or (for a relative |
| 56 | + /// `path`) if the current working directory cannot be read. |
| 57 | + pub fn ignore_input(&self, path: &OsStr) -> io::Result<()> { |
| 58 | + let ns = resolve_path(path)?; |
| 59 | + self.send(&Request::IgnoreInput(&ns))?; |
| 60 | + self.recv_ack() |
| 61 | + } |
| 62 | + |
| 63 | + /// `path` can be a file or a directory; for a directory, all files inside |
| 64 | + /// it are ignored. Relative paths are resolved against the current working |
| 65 | + /// directory before being sent to the runner. |
| 66 | + /// |
| 67 | + /// # Errors |
| 68 | + /// |
| 69 | + /// Returns an error if the request fails to send, or (for a relative |
| 70 | + /// `path`) if the current working directory cannot be read. |
| 71 | + pub fn ignore_output(&self, path: &OsStr) -> io::Result<()> { |
| 72 | + let ns = resolve_path(path)?; |
| 73 | + self.send(&Request::IgnoreOutput(&ns))?; |
| 74 | + self.recv_ack() |
| 75 | + } |
| 76 | + |
| 77 | + /// # Errors |
| 78 | + /// |
| 79 | + /// Returns an error if the request fails to send. |
| 80 | + pub fn disable_cache(&self) -> io::Result<()> { |
| 81 | + self.send(&Request::DisableCache)?; |
| 82 | + self.recv_ack() |
| 83 | + } |
| 84 | + |
| 85 | + fn recv_ack(&self) -> io::Result<()> { |
| 86 | + self.recv_with(|bytes| { |
| 87 | + let _: Ack = wincode::deserialize_exact(bytes) |
| 88 | + .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 89 | + Ok(()) |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + /// Requests an env value from the runner. Returns `None` if the runner reports |
| 94 | + /// the env is not available. |
| 95 | + /// |
| 96 | + /// # Errors |
| 97 | + /// |
| 98 | + /// Returns an error if the request or response fails. |
| 99 | + pub fn get_env(&self, name: &OsStr, tracked: bool) -> io::Result<Option<Arc<OsStr>>> { |
| 100 | + let name = Box::<NativeStr>::from(name); |
| 101 | + |
| 102 | + self.send(&Request::GetEnv { name: &name, tracked })?; |
| 103 | + self.recv_with(|bytes| { |
| 104 | + let response: GetEnvResponse<'_> = wincode::deserialize_exact(bytes) |
| 105 | + .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 106 | + Ok(response |
| 107 | + .env_value |
| 108 | + .map(|env_value| Arc::<OsStr>::from(env_value.to_cow_os_str().as_ref()))) |
| 109 | + }) |
| 110 | + } |
| 111 | + |
| 112 | + /// Requests every env whose name matches `pattern` from the runner. The |
| 113 | + /// returned map is keyed by env name (sorted) with its value. |
| 114 | + /// |
| 115 | + /// Unlike [`Self::get_env`], this always round-trips to the server — the |
| 116 | + /// client has no way to know in advance which names the pattern matches. |
| 117 | + /// Env names that aren't valid UTF-8 are silently dropped at the server. |
| 118 | + /// |
| 119 | + /// # Errors |
| 120 | + /// |
| 121 | + /// Returns an error if the request or response fails, or if the server |
| 122 | + /// rejected the pattern as an invalid glob. |
| 123 | + pub fn get_envs( |
| 124 | + &self, |
| 125 | + pattern: &str, |
| 126 | + tracked: bool, |
| 127 | + ) -> io::Result<BTreeMap<Arc<OsStr>, Arc<OsStr>>> { |
| 128 | + self.send(&Request::GetEnvs { pattern, tracked })?; |
| 129 | + self.recv_with(|bytes| { |
| 130 | + let response: GetEnvsResponse<'_> = wincode::deserialize_exact(bytes) |
| 131 | + .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 132 | + Ok(response |
| 133 | + .entries |
| 134 | + .iter() |
| 135 | + .map(|(name, value)| { |
| 136 | + ( |
| 137 | + Arc::<OsStr>::from(name.to_cow_os_str().as_ref()), |
| 138 | + Arc::<OsStr>::from(value.to_cow_os_str().as_ref()), |
| 139 | + ) |
| 140 | + }) |
| 141 | + .collect()) |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + fn send(&self, request: &Request<'_>) -> io::Result<()> { |
| 146 | + let bytes = wincode::serialize(request) |
| 147 | + .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; |
| 148 | + let len = u32::try_from(bytes.len()) |
| 149 | + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "request too large"))?; |
| 150 | + let mut stream = self.stream.borrow_mut(); |
| 151 | + stream.write_all(&len.to_le_bytes())?; |
| 152 | + stream.write_all(&bytes)?; |
| 153 | + stream.flush()?; |
| 154 | + Ok(()) |
| 155 | + } |
| 156 | + |
| 157 | + fn recv_with<T>(&self, extract: impl FnOnce(&[u8]) -> io::Result<T>) -> io::Result<T> { |
| 158 | + let mut stream = self.stream.borrow_mut(); |
| 159 | + let mut scratch = self.scratch.borrow_mut(); |
| 160 | + let mut len_bytes = [0u8; 4]; |
| 161 | + stream.read_exact(&mut len_bytes)?; |
| 162 | + let len = u32::from_le_bytes(len_bytes) as usize; |
| 163 | + scratch.clear(); |
| 164 | + scratch.resize(len, 0); |
| 165 | + stream.read_exact(&mut scratch)?; |
| 166 | + extract(&scratch) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +#[cfg(unix)] |
| 171 | +fn connect(name: &OsStr) -> io::Result<Stream> { |
| 172 | + std::os::unix::net::UnixStream::connect(name) |
| 173 | +} |
| 174 | + |
| 175 | +/// Open a Windows named pipe as a client. |
| 176 | +/// |
| 177 | +/// `OpenOptions::open` on a named-pipe path fails with `ERROR_PIPE_BUSY` when |
| 178 | +/// the server's only pending instance has just been claimed by another client |
| 179 | +/// — the brief window between the server accepting one connection and creating |
| 180 | +/// the next instance. The retry loop bridges that window; the deadline keeps |
| 181 | +/// the wait bounded if the server has actually gone away. |
| 182 | +#[cfg(windows)] |
| 183 | +fn connect(name: &OsStr) -> io::Result<Stream> { |
| 184 | + use std::{ |
| 185 | + fs::OpenOptions, |
| 186 | + thread, |
| 187 | + time::{Duration, Instant}, |
| 188 | + }; |
| 189 | + |
| 190 | + // ERROR_PIPE_BUSY — see WinError.h. `std::io::Error` does not expose a |
| 191 | + // typed constant for this, so the raw OS code is the cleanest test. |
| 192 | + const ERROR_PIPE_BUSY: i32 = 231; |
| 193 | + |
| 194 | + let deadline = Instant::now() + Duration::from_secs(5); |
| 195 | + loop { |
| 196 | + match OpenOptions::new().read(true).write(true).open(name) { |
| 197 | + Ok(file) => return Ok(file), |
| 198 | + Err(err) if err.raw_os_error() == Some(ERROR_PIPE_BUSY) => { |
| 199 | + if Instant::now() >= deadline { |
| 200 | + return Err(err); |
| 201 | + } |
| 202 | + thread::sleep(Duration::from_millis(1)); |
| 203 | + } |
| 204 | + Err(err) => return Err(err), |
| 205 | + } |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +#[expect( |
| 210 | + clippy::disallowed_types, |
| 211 | + reason = "std::path::PathBuf is needed to round-trip through std::fs::canonicalize on Windows below" |
| 212 | +)] |
| 213 | +fn resolve_path(path: &OsStr) -> io::Result<Box<NativeStr>> { |
| 214 | + let absolute: std::path::PathBuf = if let Some(abs) = AbsolutePath::new(path) { |
| 215 | + abs.as_path().to_path_buf() |
| 216 | + } else { |
| 217 | + let mut buf = vite_path::current_dir()?; |
| 218 | + buf.push(path); |
| 219 | + buf.as_path().to_path_buf() |
| 220 | + }; |
| 221 | + |
| 222 | + // On Windows, canonicalize so the path uses the exact on-disk casing |
| 223 | + // and resolves substitute drives / junctions the same way `fspy`'s |
| 224 | + // `GetFinalPathNameByHandleW`-reported paths do. Without this, an |
| 225 | + // `ignoreInput("cache_like")` whose `current_dir()` prefix differs in |
| 226 | + // case or symlink shape from the fspy-reported reads won't filter |
| 227 | + // them out, and the runner sees a read/write overlap. Strip the |
| 228 | + // `\\?\` namespace prefix because `fspy_shared::NativePath:: |
| 229 | + // strip_path_prefix` does the same on the runner side; if the |
| 230 | + // canonical form starts with `\\?\UNC\`, fall back to the |
| 231 | + // non-canonical form so we don't accidentally rewrite a UNC path |
| 232 | + // (where dropping `\\?\` would change meaning). |
| 233 | + #[cfg(windows)] |
| 234 | + let absolute = match std::fs::canonicalize(&absolute) { |
| 235 | + Ok(canonical) => { |
| 236 | + use std::{ |
| 237 | + ffi::OsString, |
| 238 | + os::windows::ffi::{OsStrExt, OsStringExt}, |
| 239 | + }; |
| 240 | + let wide: Vec<u16> = canonical.as_os_str().encode_wide().collect(); |
| 241 | + let unc_prefix: Vec<u16> = r"\\?\UNC\".encode_utf16().collect(); |
| 242 | + let nt_prefix: Vec<u16> = r"\\?\".encode_utf16().collect(); |
| 243 | + if wide.starts_with(&unc_prefix) { |
| 244 | + // UNC path — keep canonical form (still has \\?\UNC\ for fspy parity). |
| 245 | + canonical |
| 246 | + } else if let Some(rest) = wide.strip_prefix(nt_prefix.as_slice()) { |
| 247 | + std::path::PathBuf::from(OsString::from_wide(rest)) |
| 248 | + } else { |
| 249 | + canonical |
| 250 | + } |
| 251 | + } |
| 252 | + Err(_) => absolute, |
| 253 | + }; |
| 254 | + |
| 255 | + Ok(Box::<NativeStr>::from(absolute.as_os_str())) |
| 256 | +} |
0 commit comments