@@ -43,17 +43,11 @@ pub struct Directories {
43
43
44
44
impl Directories {
45
45
pub fn create (
46
- engine : & Engine ,
46
+ mount_finder : & MountFinder ,
47
47
metadata : & CargoMetadata ,
48
48
cwd : & Path ,
49
49
sysroot : & Path ,
50
- docker_in_docker : bool ,
51
50
) -> Result < Self > {
52
- let mount_finder = if docker_in_docker {
53
- MountFinder :: new ( docker_read_mount_paths ( engine) ?)
54
- } else {
55
- MountFinder :: default ( )
56
- } ;
57
51
let home_dir =
58
52
home:: home_dir ( ) . ok_or_else ( || eyre:: eyre!( "could not find home directory" ) ) ?;
59
53
let cargo = home:: cargo_home ( ) ?;
@@ -89,18 +83,10 @@ impl Directories {
89
83
}
90
84
#[ cfg( not( target_os = "windows" ) ) ]
91
85
{
86
+ // NOTE: host root has already found the mount path
92
87
mount_root = host_root. to_utf8 ( ) ?. to_string ( ) ;
93
88
}
94
- let mount_cwd: String ;
95
- #[ cfg( target_os = "windows" ) ]
96
- {
97
- // On Windows, we can not mount the directory name directly. Instead, we use wslpath to convert the path to a linux compatible path.
98
- mount_cwd = cwd. as_wslpath ( ) ?;
99
- }
100
- #[ cfg( not( target_os = "windows" ) ) ]
101
- {
102
- mount_cwd = mount_finder. find_mount_path ( cwd) . to_utf8 ( ) ?. to_string ( ) ;
103
- }
89
+ let mount_cwd = mount_finder. find_path ( cwd, false ) ?;
104
90
let sysroot = mount_finder. find_mount_path ( sysroot) ;
105
91
106
92
Ok ( Directories {
@@ -163,8 +149,10 @@ pub fn get_package_info(
163
149
let cwd = std:: env:: current_dir ( ) ?;
164
150
let host_meta = rustc:: version_meta ( ) ?;
165
151
let host = host_meta. host ( ) ;
152
+
166
153
let sysroot = rustc:: get_sysroot ( & host, & target, channel, msg_info) ?. 1 ;
167
- let dirs = Directories :: create ( engine, & metadata, & cwd, & sysroot, docker_in_docker) ?;
154
+ let mount_finder = MountFinder :: create ( engine, docker_in_docker) ?;
155
+ let dirs = Directories :: create ( & mount_finder, & metadata, & cwd, & sysroot) ?;
168
156
169
157
Ok ( ( target, metadata, dirs) )
170
158
}
@@ -251,9 +239,9 @@ fn add_cargo_configuration_envvars(docker: &mut Command) {
251
239
}
252
240
}
253
241
254
- pub ( crate ) fn mount ( docker : & mut Command , val : & Path , prefix : & str ) -> Result < String > {
255
- let host_path = file :: canonicalize ( val ) ? ;
256
- let mount_path = canonicalize_mount_path ( & host_path) ?;
242
+ // NOTE: host path must be canonical
243
+ pub ( crate ) fn mount ( docker : & mut Command , host_path : & Path , prefix : & str ) -> Result < String > {
244
+ let mount_path = canonicalize_mount_path ( host_path) ?;
257
245
docker. args ( & [
258
246
"-v" ,
259
247
& format ! ( "{}:{prefix}{}" , host_path. to_utf8( ) ?, mount_path) ,
@@ -338,6 +326,7 @@ pub(crate) fn docker_cwd(
338
326
pub ( crate ) fn docker_mount (
339
327
docker : & mut Command ,
340
328
metadata : & CargoMetadata ,
329
+ mount_finder : & MountFinder ,
341
330
config : & Config ,
342
331
target : & Target ,
343
332
cwd : & Path ,
@@ -359,15 +348,19 @@ pub(crate) fn docker_mount(
359
348
} ;
360
349
361
350
if let Ok ( val) = value {
362
- let mount_path = mount_cb ( docker, val. as_ref ( ) ) ?;
363
- docker. args ( & [ "-e" , & format ! ( "{}={}" , var, mount_path) ] ) ;
351
+ let canonical_val = file:: canonicalize ( & val) ?;
352
+ let host_path = mount_finder. find_path ( & canonical_val, true ) ?;
353
+ let mount_path = mount_cb ( docker, host_path. as_ref ( ) ) ?;
354
+ docker. args ( & [ "-e" , & format ! ( "{}={}" , host_path, mount_path) ] ) ;
364
355
store_cb ( ( val, mount_path) ) ;
365
356
mount_volumes = true ;
366
357
}
367
358
}
368
359
369
360
for path in metadata. path_dependencies ( ) {
370
- let mount_path = mount_cb ( docker, path) ?;
361
+ let canonical_path = file:: canonicalize ( path) ?;
362
+ let host_path = mount_finder. find_path ( & canonical_path, true ) ?;
363
+ let mount_path = mount_cb ( docker, host_path. as_ref ( ) ) ?;
371
364
store_cb ( ( path. to_utf8 ( ) ?. to_string ( ) , mount_path) ) ;
372
365
mount_volumes = true ;
373
366
}
@@ -614,7 +607,7 @@ fn dockerinfo_parse_user_mounts(info: &serde_json::Value) -> Vec<MountDetail> {
614
607
}
615
608
616
609
#[ derive( Debug , Default ) ]
617
- struct MountFinder {
610
+ pub struct MountFinder {
618
611
mounts : Vec < MountDetail > ,
619
612
}
620
613
@@ -636,7 +629,15 @@ impl MountFinder {
636
629
MountFinder { mounts }
637
630
}
638
631
639
- fn find_mount_path ( & self , path : impl AsRef < Path > ) -> PathBuf {
632
+ pub fn create ( engine : & Engine , docker_in_docker : bool ) -> Result < MountFinder > {
633
+ Ok ( if docker_in_docker {
634
+ MountFinder :: new ( docker_read_mount_paths ( engine) ?)
635
+ } else {
636
+ MountFinder :: default ( )
637
+ } )
638
+ }
639
+
640
+ pub fn find_mount_path ( & self , path : impl AsRef < Path > ) -> PathBuf {
640
641
let path = path. as_ref ( ) ;
641
642
642
643
for info in & self . mounts {
@@ -647,6 +648,23 @@ impl MountFinder {
647
648
648
649
path. to_path_buf ( )
649
650
}
651
+
652
+ #[ allow( unused_variables, clippy:: needless_return) ]
653
+ fn find_path ( & self , path : & Path , host : bool ) -> Result < String > {
654
+ #[ cfg( target_os = "windows" ) ]
655
+ {
656
+ // On Windows, we can not mount the directory name directly. Instead, we use wslpath to convert the path to a linux compatible path.
657
+ if host {
658
+ return Ok ( path. to_utf8 ( ) ?. to_string ( ) ) ;
659
+ } else {
660
+ return path. as_wslpath ( ) ;
661
+ }
662
+ }
663
+ #[ cfg( not( target_os = "windows" ) ) ]
664
+ {
665
+ return Ok ( self . find_mount_path ( path) . to_utf8 ( ) ?. to_string ( ) ) ;
666
+ }
667
+ }
650
668
}
651
669
652
670
fn path_digest ( path : & Path ) -> Result < const_sha1:: Digest > {
0 commit comments