@@ -8,6 +8,7 @@ use serde_json::{json, Value};
88use std:: collections:: HashSet ;
99use std:: env;
1010use std:: io:: { self , Read } ;
11+ use std:: path:: PathBuf ;
1112use std:: process:: { Command , Stdio } ;
1213use std:: thread;
1314use std:: time:: Duration ;
@@ -677,7 +678,7 @@ fn ensure_ollama_is_running(client: &Client, base: &str) -> Result<()> {
677678 style( "Ollama API not responsive. Checking CLI..." ) . yellow( )
678679 ) ;
679680
680- match Command :: new ( "ollama" ) . arg ( "--version" ) . output ( ) {
681+ match ollama_command ( ) . arg ( "--version" ) . output ( ) {
681682 Ok ( _) => {
682683 println ! (
683684 "{}" ,
@@ -723,15 +724,15 @@ fn start_ollama_service() -> Result<()> {
723724 {
724725 return Ok ( ( ) ) ;
725726 }
726- // Fallback: spawn a new window running `ollama serve`
727- Command :: new ( "cmd" )
728- . args ( [ "/C" , "start" , "ollama" , " serve"] )
727+ // Fallback: directly spawn `ollama serve` (respects OLLAMA_BIN path)
728+ ollama_command ( )
729+ . arg ( " serve")
729730 . stdout ( Stdio :: null ( ) )
730731 . stderr ( Stdio :: null ( ) )
731732 . spawn ( )
732733 . context ( "Failed to start Ollama on Windows." ) ?;
733734 } else {
734- Command :: new ( "ollama" )
735+ ollama_command ( )
735736 . arg ( "serve" )
736737 . stdout ( Stdio :: null ( ) )
737738 . stderr ( Stdio :: null ( ) )
@@ -826,7 +827,7 @@ fn delete_model(client: &Client, base: &str, name: &str, use_cli_fallback: bool)
826827}
827828
828829fn cli_copy ( from : & str , to : & str ) -> Result < ( ) > {
829- let status = Command :: new ( "ollama" )
830+ let status = ollama_command ( )
830831 . args ( [ "cp" , from, to] )
831832 . status ( )
832833 . context ( "Failed to invoke `ollama` binary" ) ?;
@@ -837,7 +838,7 @@ fn cli_copy(from: &str, to: &str) -> Result<()> {
837838}
838839
839840fn cli_rm ( name : & str ) -> Result < ( ) > {
840- let status = Command :: new ( "ollama" )
841+ let status = ollama_command ( )
841842 . args ( [ "rm" , name] )
842843 . status ( )
843844 . context ( "Failed to invoke `ollama` binary" ) ?;
@@ -923,11 +924,28 @@ fn model_exists(client: &Client, base: &str, name: &str) -> Result<bool> {
923924 Ok ( list. iter ( ) . any ( |m| m. name == name) )
924925}
925926
927+ fn ollama_path ( ) -> PathBuf {
928+ if let Ok ( path) = env:: var ( "OLLAMA_BIN" ) {
929+ PathBuf :: from ( path)
930+ } else if cfg ! ( target_os = "windows" ) {
931+ PathBuf :: from ( "ollama.exe" )
932+ } else {
933+ PathBuf :: from ( "ollama" )
934+ }
935+ }
936+
937+ fn ollama_command ( ) -> Command {
938+ Command :: new ( ollama_path ( ) )
939+ }
940+
926941#[ cfg( test) ]
927942mod tests {
928943 use super :: * ;
929944 use httpmock:: prelude:: * ;
930945 use std:: env;
946+ use std:: sync:: Mutex ;
947+
948+ static ENV_LOCK : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
931949
932950 /// RAII helper to temporarily set an env var and restore it on drop.
933951 struct EnvOverride {
@@ -977,6 +995,7 @@ mod tests {
977995
978996 #[ test]
979997 fn pick_base_url_prefers_cli_value ( ) {
998+ let _lock = ENV_LOCK . lock ( ) . unwrap ( ) ;
980999 let _guard = EnvOverride :: new ( "OLLAMA_HOST" , "10.0.0.2:11434" ) ;
9811000 assert_eq ! ( pick_base_url( Some ( "1.2.3.4:9999" ) ) , "http://1.2.3.4:9999" ) ;
9821001 }
@@ -1043,4 +1062,21 @@ mod tests {
10431062 assert ! ( set. contains( "one" ) ) ;
10441063 assert ! ( set. contains( "two" ) ) ;
10451064 }
1065+
1066+ #[ test]
1067+ fn ollama_path_respects_env ( ) {
1068+ let _lock = ENV_LOCK . lock ( ) . unwrap ( ) ;
1069+ let _guard = EnvOverride :: new ( "OLLAMA_BIN" , "/custom/bin/ollama" ) ;
1070+ assert_eq ! ( ollama_path( ) , PathBuf :: from( "/custom/bin/ollama" ) ) ;
1071+ }
1072+
1073+ #[ test]
1074+ fn ollama_path_has_platform_default ( ) {
1075+ let _lock = ENV_LOCK . lock ( ) . unwrap ( ) ;
1076+ env:: remove_var ( "OLLAMA_BIN" ) ;
1077+ #[ cfg( target_os = "windows" ) ]
1078+ assert_eq ! ( ollama_path( ) , PathBuf :: from( "ollama.exe" ) ) ;
1079+ #[ cfg( not( target_os = "windows" ) ) ]
1080+ assert_eq ! ( ollama_path( ) , PathBuf :: from( "ollama" ) ) ;
1081+ }
10461082}
0 commit comments