File tree 4 files changed +63
-24
lines changed
4 files changed +63
-24
lines changed Original file line number Diff line number Diff line change @@ -213,7 +213,6 @@ impl<'gctx> Workspace<'gctx> {
213
213
pub fn new ( manifest_path : & Path , gctx : & ' gctx GlobalContext ) -> CargoResult < Workspace < ' gctx > > {
214
214
let mut ws = Workspace :: new_default ( manifest_path. to_path_buf ( ) , gctx) ;
215
215
ws. target_dir = gctx. target_dir ( ) ?;
216
- ws. build_dir = gctx. build_dir ( ) ?;
217
216
218
217
if manifest_path. is_relative ( ) {
219
218
bail ! (
@@ -224,6 +223,12 @@ impl<'gctx> Workspace<'gctx> {
224
223
ws. root_manifest = ws. find_root ( manifest_path) ?;
225
224
}
226
225
226
+ ws. build_dir = gctx. build_dir (
227
+ ws. root_manifest
228
+ . as_ref ( )
229
+ . unwrap_or ( & manifest_path. to_path_buf ( ) ) ,
230
+ ) ?;
231
+
227
232
ws. custom_metadata = ws
228
233
. load_workspace_config ( ) ?
229
234
. and_then ( |cfg| cfg. custom_metadata ) ;
Original file line number Diff line number Diff line change @@ -636,13 +636,36 @@ impl GlobalContext {
636
636
/// Falls back to the target directory if not specified.
637
637
///
638
638
/// Callers should prefer [`Workspace::build_dir`] instead.
639
- pub fn build_dir ( & self ) -> CargoResult < Option < Filesystem > > {
639
+ pub fn build_dir ( & self , manifest_path : & PathBuf ) -> CargoResult < Option < Filesystem > > {
640
640
if !self . cli_unstable ( ) . build_dir {
641
641
return self . target_dir ( ) ;
642
642
}
643
643
if let Some ( val) = & self . build_config ( ) ?. build_dir {
644
- let path = val. resolve_path ( self ) ;
645
-
644
+ let replacements = vec ! [
645
+ (
646
+ "{workspace-root}" ,
647
+ manifest_path
648
+ . parent( )
649
+ . unwrap( )
650
+ . to_str( )
651
+ . context( "workspace root was not valid utf-8" ) ?
652
+ . to_string( ) ,
653
+ ) ,
654
+ (
655
+ "{cargo-cache}" ,
656
+ self . home( )
657
+ . as_path_unlocked( )
658
+ . to_str( )
659
+ . context( "cargo home was not valid utf-8" ) ?
660
+ . to_string( ) ,
661
+ ) ,
662
+ (
663
+ "{workspace-manifest-path-hash}" ,
664
+ crate :: util:: hex:: short_hash( manifest_path) ,
665
+ ) ,
666
+ ] ;
667
+
668
+ let path = val. resolve_templated_path ( self , replacements) ;
646
669
// Check if the target directory is set to an empty string in the config.toml file.
647
670
if val. raw_value ( ) . is_empty ( ) {
648
671
bail ! (
Original file line number Diff line number Diff line change @@ -32,6 +32,25 @@ impl ConfigRelativePath {
32
32
self . 0 . definition . root ( gctx) . join ( & self . 0 . val )
33
33
}
34
34
35
+ /// Same as [`Self::resolve_path`] but will make string replacements
36
+ /// before resolving the path.
37
+ ///
38
+ /// `replacements` should be an an [`IntoIterator`] of tuples with the "from" and "to" for the
39
+ /// string replacement
40
+ pub fn resolve_templated_path (
41
+ & self ,
42
+ gctx : & GlobalContext ,
43
+ replacements : impl IntoIterator < Item = ( impl AsRef < str > , impl AsRef < str > ) > ,
44
+ ) -> PathBuf {
45
+ let mut value = self . 0 . val . clone ( ) ;
46
+
47
+ for ( from, to) in replacements {
48
+ value = value. replace ( from. as_ref ( ) , to. as_ref ( ) ) ;
49
+ }
50
+
51
+ self . 0 . definition . root ( gctx) . join ( & value)
52
+ }
53
+
35
54
/// Resolves this configuration-relative path to either an absolute path or
36
55
/// something appropriate to execute from `PATH`.
37
56
///
Original file line number Diff line number Diff line change @@ -498,20 +498,15 @@ mod templating {
498
498
499
499
#[ cargo_test]
500
500
fn workspace_root ( ) {
501
- let p = project ( ) ;
502
- let root = p. root ( ) ;
503
- let p = p
501
+ let p = project ( )
504
502
. file ( "src/main.rs" , r#"fn main() { println!("Hello, World!") }"# )
505
503
. file (
506
504
".cargo/config.toml" ,
507
- & format ! (
508
- r#"
509
- [build]
510
- build-dir = "{}/build-dir"
511
- target-dir = "target-dir"
512
- "# ,
513
- root. display( )
514
- ) ,
505
+ r#"
506
+ [build]
507
+ build-dir = "{workspace-root}/build-dir"
508
+ target-dir = "target-dir"
509
+ "# ,
515
510
)
516
511
. build ( ) ;
517
512
@@ -533,14 +528,11 @@ mod templating {
533
528
. file ( "src/main.rs" , r#"fn main() { println!("Hello, World!") }"# )
534
529
. file (
535
530
".cargo/config.toml" ,
536
- & format ! (
537
- r#"
538
- [build]
539
- build-dir = "{}/build-dir"
540
- target-dir = "target-dir"
541
- "# ,
542
- paths:: home( ) . join( ".cargo" ) . display( )
543
- ) ,
531
+ r#"
532
+ [build]
533
+ build-dir = "{cargo-cache}/build-dir"
534
+ target-dir = "target-dir"
535
+ "# ,
544
536
)
545
537
. build ( ) ;
546
538
@@ -574,7 +566,7 @@ mod templating {
574
566
".cargo/config.toml" ,
575
567
r#"
576
568
[build]
577
- build-dir = "foo/a70a942ddb7da6b4 /build-dir"
569
+ build-dir = "foo/{workspace-manifest-path-hash} /build-dir"
578
570
target-dir = "target-dir"
579
571
"# ,
580
572
)
You can’t perform that action at this time.
0 commit comments