1- use std:: panic;
21use std:: path:: Path ;
32
43use crate :: command:: Command ;
5- use crate :: util:: handle_failed_output;
64
75/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
86/// available on the platform!
@@ -22,14 +20,19 @@ use crate::util::handle_failed_output;
2220#[ track_caller]
2321#[ must_use]
2422pub fn get_windows_path < P : AsRef < Path > > ( path : P ) -> String {
25- let caller = panic:: Location :: caller ( ) ;
26- let mut cygpath = Command :: new ( "cygpath" ) ;
27- cygpath. arg ( "-w" ) ;
28- cygpath. arg ( path. as_ref ( ) ) ;
29- let output = cygpath. run ( ) ;
30- if !output. status ( ) . success ( ) {
31- handle_failed_output ( & cygpath, output, caller. line ( ) ) ;
23+ // If the path looks unixy then use cygpath otherwise return it unchanged.
24+ // If cygpath fails then fallback to just using the path as given.
25+ if path. as_ref ( ) . starts_with ( "/" ) {
26+ let mut cygpath = Command :: new ( "cygpath" ) ;
27+ cygpath. arg ( "-w" ) ;
28+ cygpath. arg ( path. as_ref ( ) ) ;
29+ let output = cygpath. run ( ) ;
30+ if !output. status ( ) . success ( ) {
31+ return path. as_ref ( ) . to_str ( ) . unwrap ( ) . into ( ) ;
32+ }
33+ // cygpath -w can attach a newline
34+ output. stdout_utf8 ( ) . trim ( ) . to_string ( )
35+ } else {
36+ path. as_ref ( ) . to_str ( ) . unwrap ( ) . into ( )
3237 }
33- // cygpath -w can attach a newline
34- output. stdout_utf8 ( ) . trim ( ) . to_string ( )
3538}
0 commit comments