Set current working directory for procedural macros
This commit is contained in:
parent
e149a15edd
commit
6051318744
@ -156,12 +156,18 @@ pub fn expand(
|
|||||||
attr: Option<&Subtree>,
|
attr: Option<&Subtree>,
|
||||||
env: Vec<(String, String)>,
|
env: Vec<(String, String)>,
|
||||||
) -> Result<Result<Subtree, PanicMessage>, ServerError> {
|
) -> Result<Result<Subtree, PanicMessage>, ServerError> {
|
||||||
|
let current_dir = env
|
||||||
|
.iter()
|
||||||
|
.find(|(name, _)| name == "CARGO_MANIFEST_DIR")
|
||||||
|
.map(|(_, value)| value.clone());
|
||||||
|
|
||||||
let task = ExpandMacro {
|
let task = ExpandMacro {
|
||||||
macro_body: FlatTree::new(subtree),
|
macro_body: FlatTree::new(subtree),
|
||||||
macro_name: self.name.to_string(),
|
macro_name: self.name.to_string(),
|
||||||
attributes: attr.map(FlatTree::new),
|
attributes: attr.map(FlatTree::new),
|
||||||
lib: self.dylib_path.to_path_buf().into(),
|
lib: self.dylib_path.to_path_buf().into(),
|
||||||
env,
|
env,
|
||||||
|
current_dir,
|
||||||
};
|
};
|
||||||
|
|
||||||
let request = msg::Request::ExpandMacro(task);
|
let request = msg::Request::ExpandMacro(task);
|
||||||
|
@ -48,6 +48,8 @@ pub struct ExpandMacro {
|
|||||||
|
|
||||||
/// Environment variables to set during macro expansion.
|
/// Environment variables to set during macro expansion.
|
||||||
pub env: Vec<(String, String)>,
|
pub env: Vec<(String, String)>,
|
||||||
|
|
||||||
|
pub current_dir: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Message: Serialize + DeserializeOwned {
|
pub trait Message: Serialize + DeserializeOwned {
|
||||||
@ -143,6 +145,7 @@ fn test_proc_macro_rpc_works() {
|
|||||||
attributes: None,
|
attributes: None,
|
||||||
lib: std::env::current_dir().unwrap(),
|
lib: std::env::current_dir().unwrap(),
|
||||||
env: Default::default(),
|
env: Default::default(),
|
||||||
|
current_dir: Default::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let json = serde_json::to_string(&task).unwrap();
|
let json = serde_json::to_string(&task).unwrap();
|
||||||
|
@ -43,6 +43,16 @@ pub fn expand(&mut self, task: ExpandMacro) -> Result<FlatTree, PanicMessage> {
|
|||||||
prev_env.insert(k.as_str(), env::var_os(k));
|
prev_env.insert(k.as_str(), env::var_os(k));
|
||||||
env::set_var(k, v);
|
env::set_var(k, v);
|
||||||
}
|
}
|
||||||
|
let prev_working_dir = match task.current_dir {
|
||||||
|
Some(dir) => {
|
||||||
|
let prev_working_dir = std::env::current_dir().ok();
|
||||||
|
if let Err(err) = std::env::set_current_dir(&dir) {
|
||||||
|
eprintln!("Failed to set the current working dir to {}. Error: {:?}", dir, err)
|
||||||
|
}
|
||||||
|
prev_working_dir
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
let macro_body = task.macro_body.to_subtree();
|
let macro_body = task.macro_body.to_subtree();
|
||||||
let attributes = task.attributes.map(|it| it.to_subtree());
|
let attributes = task.attributes.map(|it| it.to_subtree());
|
||||||
@ -56,6 +66,15 @@ pub fn expand(&mut self, task: ExpandMacro) -> Result<FlatTree, PanicMessage> {
|
|||||||
None => env::remove_var(k),
|
None => env::remove_var(k),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if let Some(dir) = prev_working_dir {
|
||||||
|
if let Err(err) = std::env::set_current_dir(&dir) {
|
||||||
|
eprintln!(
|
||||||
|
"Failed to set the current working dir to {}. Error: {:?}",
|
||||||
|
dir.display(),
|
||||||
|
err
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result.map_err(PanicMessage)
|
result.map_err(PanicMessage)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user