From 7d19cd5ebe5636dfd0a22aac55a39192f485884b Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Thu, 25 Nov 2021 19:13:03 +0200 Subject: [PATCH 1/2] Refactor commandline processing This reduces the amount of code duplication, and similar flow is used for both wsl and non-wsl code paths. --- src/bridge/mod.rs | 88 ++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index e0b38c6..688714a 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -29,8 +29,12 @@ fn set_windows_creation_flags(cmd: &mut Command) { cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW } -#[cfg(windows)] -fn platform_build_nvim_cmd(bin: &str) -> Option { +fn build_nvim_cmd_with_args(bin: &str) -> Command { + let mut args = Vec::::new(); + args.push("--embed".to_string()); + args.extend(SETTINGS.get::().neovim_args); + + #[cfg(windows)] if SETTINGS.get::().wsl { let mut cmd = Command::new("wsl"); cmd.args(&[ @@ -38,62 +42,63 @@ fn platform_build_nvim_cmd(bin: &str) -> Option { "-c", "let \\$PATH=system(\"\\$SHELL -lic 'echo \\$PATH' 2>/dev/null\")", ]); - Some(cmd) - } else if Path::new(&bin).exists() { - Some(Command::new(bin)) - } else { - None - } -} - -#[cfg(unix)] -fn platform_build_nvim_cmd(bin: &str) -> Option { - if Path::new(&bin).exists() { - Some(Command::new(bin)) - } else { - None + cmd.args(args); + return cmd; } + let mut cmd = Command::new(bin); + cmd.args(args); + cmd } -fn build_nvim_cmd() -> Command { - if let Some(path) = SETTINGS.get::().neovim_bin { - if let Some(cmd) = platform_build_nvim_cmd(&path) { - return cmd; +fn platform_exists(bin: &str) -> bool { + #[cfg(windows)] + if SETTINGS.get::().wsl { + if let Ok(output) = std::process::Command::new("wsl") + .args(&["$SHELL", "-lic"]) + .arg(format!("exists -x {}", bin)) + .output() + { + return output.status.success(); } else { - warn!("NEOVIM_BIN is invalid falling back to first bin in PATH"); + error!("wsl exists failed"); + std::process::exit(1); } } + Path::new(&bin).exists() +} + +fn platform_which(bin: &str) -> Option { #[cfg(windows)] if SETTINGS.get::().wsl { if let Ok(output) = std::process::Command::new("wsl") - .args(&["$SHELL", "-lic", "which nvim"]) + .args(&["$SHELL", "-lic"]) + .arg(format!("which {}", bin)) .output() { if output.status.success() { - let path = String::from_utf8(output.stdout).unwrap(); - let mut cmd = Command::new("wsl"); - cmd.args(&[ - path.trim(), - "-c", - "let \\$PATH=system(\"\\$SHELL -lic 'echo \\$PATH' 2>/dev/null\")", - ]); - return cmd; + return Some(String::from_utf8(output.stdout).unwrap()); } else { - error!("nvim not found in WSL path"); - std::process::exit(1); + return None; } - } else { - error!("wsl which nvim failed"); - std::process::exit(1); } } - if let Ok(path) = which::which("nvim") { - if let Some(cmd) = platform_build_nvim_cmd(path.to_str().unwrap()) { - cmd + if let Ok(path) = which::which(bin) { + path.into_os_string().into_string().ok() + } else { + None + } +} + +fn build_nvim_cmd() -> Command { + if let Some(path) = SETTINGS.get::().neovim_bin { + if platform_exists(&path) { + return build_nvim_cmd_with_args(&path); } else { - error!("nvim does not have proper permissions!"); - std::process::exit(1); + warn!("NEOVIM_BIN is invalid falling back to first bin in PATH"); } + } + if let Some(path) = platform_which("nvim") { + build_nvim_cmd_with_args(&path) } else { error!("nvim not found!"); std::process::exit(1); @@ -123,9 +128,6 @@ pub fn build_neovide_command(channel: u64, num_args: u64, command: &str, event: pub fn create_nvim_command() -> Command { let mut cmd = build_nvim_cmd(); - cmd.arg("--embed") - .args(SETTINGS.get::().neovim_args.iter()); - info!("Starting neovim with: {:?}", cmd); #[cfg(not(debug_assertions))] From 0849151838ee73dad99362e4f17fa3a7a70a4ee3 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Thu, 25 Nov 2021 19:39:50 +0200 Subject: [PATCH 2/2] Run neovim directly in WSL login shell Therefore, the neovim path hack is no longer needed, since the path and other environment variables are now set correctly. --- src/bridge/mod.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index 688714a..75e75d2 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -37,12 +37,8 @@ fn build_nvim_cmd_with_args(bin: &str) -> Command { #[cfg(windows)] if SETTINGS.get::().wsl { let mut cmd = Command::new("wsl"); - cmd.args(&[ - bin.trim(), - "-c", - "let \\$PATH=system(\"\\$SHELL -lic 'echo \\$PATH' 2>/dev/null\")", - ]); - cmd.args(args); + let argstring = format!("{} {}", bin.trim(), args.join(" ")); + cmd.args(&["$SHELL", "-lc", &argstring]); return cmd; } let mut cmd = Command::new(bin);