Set current working directory for procedural macros

This commit is contained in:
vlad20012 2022-01-27 15:54:06 +03:00
parent e149a15edd
commit 6051318744
No known key found for this signature in database
GPG Key ID: 58E62ED31A8B3999
3 changed files with 28 additions and 0 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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)
} }