Use clip.exe for single-line ASCII clipboard copies in WSL (#61724)

## Why

Single-line ASCII copies can use the lightweight Windows clipboard writer without starting PowerShell.

## What changed

- Try `clip.exe` for printable ASCII text and tabs, falling back to PowerShell if it is unavailable or fails. Bound discovery and execution by short deadlines.
- Keep PowerShell for Unicode, multiline text, and other control characters. Pad even-length ASCII input with a trailing NUL to prevent `clip.exe` from interpreting it as UTF-16.
- Preserve precedence and command compatibility for installed Linux `powershell.exe` adapters, and defer Windows PowerShell discovery until needed.

## Testing

Extend WSL clipboard tests to cover input eligibility and padding, adapter precedence, and expired deadlines.

GitOrigin-RevId: 69b66ec1243452d0a4df119563321f38d177bea9
This commit is contained in:
Eric Traut
2026-11-16 02:39:33 +00:00
committed by copyberry
parent aea4c6ee4b
commit 09c711b783
4 changed files with 181 additions and 37 deletions
+2
View File
@@ -3,6 +3,8 @@
pub(crate) mod env;
pub use env::is_wsl;
mod system_commands;
#[cfg(target_os = "linux")]
pub use system_commands::linux_system_executable;
pub use system_commands::system_executable;
pub use system_commands::system_path;
@@ -12,12 +12,18 @@ use std::path::PathBuf;
/// Finds an installed helper without consulting PATH, PATHEXT, or the working directory.
pub fn system_executable(name: &str) -> Option<PathBuf> {
executable_in_directories(name, &system_directories())
executable_in_directories(name, &system_directories(ExecutableScope::All))
}
/// Finds a trusted Linux installation without probing WSL's Windows executable directories.
#[cfg(target_os = "linux")]
pub fn linux_system_executable(name: &str) -> Option<PathBuf> {
executable_in_directories(name, &system_directories(ExecutableScope::Linux))
}
/// Child-process PATH for automatic helpers, not for user-requested tools.
pub fn system_path() -> std::io::Result<OsString> {
let directories = system_directories();
let directories = system_directories(ExecutableScope::All);
if directories.is_empty() {
return Err(std::io::Error::other("no trusted executable directories"));
}
@@ -72,7 +78,15 @@ fn installation_roots() -> Vec<PathBuf> {
.collect()
}
fn system_directories() -> Vec<PathBuf> {
enum ExecutableScope {
All,
#[cfg(target_os = "linux")]
Linux,
}
fn system_directories(scope: ExecutableScope) -> Vec<PathBuf> {
#[cfg(not(target_os = "linux"))]
let _ = scope;
#[cfg(unix)]
let directories = [
"/opt/homebrew/bin",
@@ -91,7 +105,7 @@ fn system_directories() -> Vec<PathBuf> {
.map(PathBuf::from);
#[cfg(target_os = "linux")]
let directories = directories.chain(
crate::is_wsl()
(matches!(scope, ExecutableScope::All) && crate::is_wsl())
.then_some([
PathBuf::from("/mnt/c/Windows/System32"),
PathBuf::from("/mnt/c/Windows/System32/WindowsPowerShell/v1.0"),