File tree 3 files changed +37
-4
lines changed
3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -681,7 +681,17 @@ impl GlobalContext {
681
681
} ) ,
682
682
] ;
683
683
684
- let path = val. resolve_templated_path ( self , replacements) ;
684
+ let path = val
685
+ . resolve_templated_path ( self , replacements)
686
+ . map_err ( |e| match e {
687
+ path:: ResolveTemplateError :: UnexpectedVariable {
688
+ variable,
689
+ raw_template,
690
+ } => anyhow ! (
691
+ "unexpected variable `{variable}` in build.build-dir path `{raw_template}`"
692
+ ) ,
693
+ } ) ?;
694
+
685
695
// Check if the target directory is set to an empty string in the config.toml file.
686
696
if val. raw_value ( ) . is_empty ( ) {
687
697
bail ! (
Original file line number Diff line number Diff line change 1
1
use super :: { GlobalContext , StringList , Value } ;
2
+ use regex:: Regex ;
2
3
use serde:: { de:: Error , Deserialize } ;
3
4
use std:: path:: PathBuf ;
4
5
@@ -41,14 +42,23 @@ impl ConfigRelativePath {
41
42
& self ,
42
43
gctx : & GlobalContext ,
43
44
replacements : impl IntoIterator < Item = ( impl AsRef < str > , impl AsRef < str > ) > ,
44
- ) -> PathBuf {
45
+ ) -> Result < PathBuf , ResolveTemplateError > {
45
46
let mut value = self . 0 . val . clone ( ) ;
46
47
47
48
for ( from, to) in replacements {
48
49
value = value. replace ( from. as_ref ( ) , to. as_ref ( ) ) ;
49
50
}
50
51
51
- self . 0 . definition . root ( gctx) . join ( & value)
52
+ // Check for expected variables
53
+ let re = Regex :: new ( r"\{(.*)\}" ) . unwrap ( ) ;
54
+ if let Some ( caps) = re. captures ( & value) {
55
+ return Err ( ResolveTemplateError :: UnexpectedVariable {
56
+ variable : caps[ 1 ] . to_string ( ) ,
57
+ raw_template : self . 0 . val . clone ( ) ,
58
+ } ) ;
59
+ } ;
60
+
61
+ Ok ( self . 0 . definition . root ( gctx) . join ( & value) )
52
62
}
53
63
54
64
/// Resolves this configuration-relative path to either an absolute path or
@@ -122,3 +132,11 @@ impl PathAndArgs {
122
132
}
123
133
}
124
134
}
135
+
136
+ #[ derive( Debug ) ]
137
+ pub enum ResolveTemplateError {
138
+ UnexpectedVariable {
139
+ variable : String ,
140
+ raw_template : String ,
141
+ } ,
142
+ }
Original file line number Diff line number Diff line change 12
12
use std:: path:: PathBuf ;
13
13
14
14
use cargo_test_support:: prelude:: * ;
15
- use cargo_test_support:: { paths, project} ;
15
+ use cargo_test_support:: { paths, project, str } ;
16
16
use std:: env:: consts:: { DLL_PREFIX , DLL_SUFFIX , EXE_SUFFIX } ;
17
17
18
18
#[ cargo_test]
@@ -508,6 +508,11 @@ fn template_should_error_for_invalid_variables() {
508
508
p. cargo ( "build -Z build-dir" )
509
509
. masquerade_as_nightly_cargo ( & [ "build-dir" ] )
510
510
. enable_mac_dsym ( )
511
+ . with_status ( 101 )
512
+ . with_stderr_data ( str![ [ r#"
513
+ [ERROR] unexpected variable `fake` in build.build-dir path `{fake}/build-dir`
514
+
515
+ "# ] ] )
511
516
. run ( ) ;
512
517
}
513
518
You can’t perform that action at this time.
0 commit comments