File tree 3 files changed +28
-0
lines changed
3 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -156,12 +156,18 @@ impl ProcMacro {
156
156
attr : Option < & Subtree > ,
157
157
env : Vec < ( String , String ) > ,
158
158
) -> Result < Result < Subtree , PanicMessage > , ServerError > {
159
+ let current_dir = env
160
+ . iter ( )
161
+ . find ( |( name, _) | name == "CARGO_MANIFEST_DIR" )
162
+ . map ( |( _, value) | value. clone ( ) ) ;
163
+
159
164
let task = ExpandMacro {
160
165
macro_body : FlatTree :: new ( subtree) ,
161
166
macro_name : self . name . to_string ( ) ,
162
167
attributes : attr. map ( FlatTree :: new) ,
163
168
lib : self . dylib_path . to_path_buf ( ) . into ( ) ,
164
169
env,
170
+ current_dir,
165
171
} ;
166
172
167
173
let request = msg:: Request :: ExpandMacro ( task) ;
Original file line number Diff line number Diff line change @@ -48,6 +48,8 @@ pub struct ExpandMacro {
48
48
49
49
/// Environment variables to set during macro expansion.
50
50
pub env : Vec < ( String , String ) > ,
51
+
52
+ pub current_dir : Option < String > ,
51
53
}
52
54
53
55
pub trait Message : Serialize + DeserializeOwned {
@@ -143,6 +145,7 @@ mod tests {
143
145
attributes : None ,
144
146
lib : std:: env:: current_dir ( ) . unwrap ( ) ,
145
147
env : Default :: default ( ) ,
148
+ current_dir : Default :: default ( ) ,
146
149
} ;
147
150
148
151
let json = serde_json:: to_string ( & task) . unwrap ( ) ;
Original file line number Diff line number Diff line change @@ -43,6 +43,16 @@ impl ProcMacroSrv {
43
43
prev_env. insert ( k. as_str ( ) , env:: var_os ( k) ) ;
44
44
env:: set_var ( k, v) ;
45
45
}
46
+ let prev_working_dir = match task. current_dir {
47
+ Some ( dir) => {
48
+ let prev_working_dir = std:: env:: current_dir ( ) . ok ( ) ;
49
+ if let Err ( err) = std:: env:: set_current_dir ( & dir) {
50
+ eprintln ! ( "Failed to set the current working dir to {}. Error: {:?}" , dir, err)
51
+ }
52
+ prev_working_dir
53
+ }
54
+ None => None ,
55
+ } ;
46
56
47
57
let macro_body = task. macro_body . to_subtree ( ) ;
48
58
let attributes = task. attributes . map ( |it| it. to_subtree ( ) ) ;
@@ -56,6 +66,15 @@ impl ProcMacroSrv {
56
66
None => env:: remove_var ( k) ,
57
67
}
58
68
}
69
+ if let Some ( dir) = prev_working_dir {
70
+ if let Err ( err) = std:: env:: set_current_dir ( & dir) {
71
+ eprintln ! (
72
+ "Failed to set the current working dir to {}. Error: {:?}" ,
73
+ dir. display( ) ,
74
+ err
75
+ )
76
+ }
77
+ }
59
78
60
79
result. map_err ( PanicMessage )
61
80
}
You can’t perform that action at this time.
0 commit comments