From 7c373341991da59e5104c7d38bff2911d2b26517 Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Sat, 4 Nov 2023 11:39:23 +0100 Subject: [PATCH] improve setup_env on Windows (real) 935a72e05be1a213a12238f522c8b35f872ecf63 --- src/main.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index a1cca7d..166aa97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -331,8 +331,20 @@ fn setup_env() { colored::control::SHOULD_COLORIZE.set_override(false); }); - if let Ok(current_exe) = env::current_exe() { - env::set_current_dir(binary_path_str); + if let Ok(system_root) = env::var("SystemRoot") { + if let Ok(current_dir) = env::current_dir() { + if current_dir.starts_with(system_root) { + if let Ok(current_exe) = env::current_exe() { + if let Some(parent) = current_exe.parent() { + if let Err(error) = env::set_current_dir(parent) { + eprintln!("{:#?}", error); + } else { + println!("Running from the system directory. Changed working directory to the executable location."); + } + } + } + } + } } }