1
- use std:: panic;
2
1
use std:: path:: Path ;
3
2
4
3
use crate :: command:: Command ;
5
- use crate :: util:: handle_failed_output;
6
4
7
5
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
8
6
/// available on the platform!
@@ -22,14 +20,19 @@ use crate::util::handle_failed_output;
22
20
#[ track_caller]
23
21
#[ must_use]
24
22
pub 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 ( )
32
37
}
33
- // cygpath -w can attach a newline
34
- output. stdout_utf8 ( ) . trim ( ) . to_string ( )
35
38
}
0 commit comments