@@ -43,7 +43,9 @@ pub(crate) fn to_path_list(escapes: &[PathEscape], path: &str) -> String {
4343 PathEscape :: Unix => {
4444 #[ cfg( windows) ]
4545 {
46- out = windows_path:: to_unix_path_list ( & out) ;
46+ if windows_path:: should_use_unix_path ( ) {
47+ out = windows_path:: to_unix_path_list ( & out) ;
48+ }
4749 }
4850 }
4951 PathEscape :: EscapeBackslash => {
@@ -59,18 +61,28 @@ mod windows_path {
5961 use which:: which;
6062 use once_cell:: sync:: Lazy ;
6163
62- static CYGPATH_AVAILABLE : Lazy < bool > = Lazy :: new ( || which ( "cygpath" ) . is_ok ( ) ) ;
64+ // Check Unix-like shell env first, then cygpath.exe only if needed.
65+ static SHOULD_USE_UNIX_PATH : Lazy < bool > = Lazy :: new ( || {
66+ let unix_env = std:: env:: var ( "MSYSTEM" ) . is_ok ( )
67+ || std:: env:: var ( "OSTYPE" ) . map_or ( false , |v| v == "cygwin" ) ;
68+ if !unix_env {
69+ return false ;
70+ }
71+ which ( "cygpath" ) . is_ok ( )
72+ } ) ;
73+
74+ pub ( super ) fn should_use_unix_path ( ) -> bool {
75+ * SHOULD_USE_UNIX_PATH
76+ }
6377
6478 pub ( super ) fn to_unix_path_list ( path : & str ) -> String {
65- if * CYGPATH_AVAILABLE {
66- if let Ok ( output) = std:: process:: Command :: new ( "cygpath" )
67- . args ( [ "-u" , "-p" , path] )
68- . output ( )
69- {
70- if output. status . success ( ) {
71- if let Ok ( s) = String :: from_utf8 ( output. stdout ) {
72- return s. trim ( ) . to_string ( ) ;
73- }
79+ if let Ok ( output) = std:: process:: Command :: new ( "cygpath" )
80+ . args ( [ "-u" , "-p" , path] )
81+ . output ( )
82+ {
83+ if output. status . success ( ) {
84+ if let Ok ( s) = String :: from_utf8 ( output. stdout ) {
85+ return s. trim ( ) . to_string ( ) ;
7486 }
7587 }
7688 }
@@ -92,16 +104,13 @@ mod tests {
92104 #[ cfg( windows) ]
93105 mod windows_tests {
94106 use super :: { to_path_list, PathEscape } ;
95- use which:: which;
96- use once_cell:: sync:: Lazy ;
97-
98- static CYGPATH_AVAILABLE : Lazy < bool > = Lazy :: new ( || which ( "cygpath" ) . is_ok ( ) ) ;
107+ use super :: super :: windows_path;
99108
100109 #[ test]
101110 fn test_to_path_list_unix ( ) {
102111 let input = "C:\\ foo;D:\\ bar" ;
103112 let output = to_path_list ( & [ PathEscape :: Unix ] , input) ;
104- if * CYGPATH_AVAILABLE {
113+ if windows_path :: should_use_unix_path ( ) {
105114 assert_eq ! ( output, "/c/foo:/d/bar" ) ;
106115 } else {
107116 assert_eq ! ( output, input) ;
0 commit comments