Skip to content

Commit 8df38aa

Browse files
committed
internal: make sure that proc macro machinery doesn't depend on cwd
1 parent 9318c64 commit 8df38aa

File tree

9 files changed

+52
-22
lines changed

9 files changed

+52
-22
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/paths/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ edition = "2018"
77

88
[lib]
99
doctest = false
10+
11+
[dependencies]
12+
serde = "1"

crates/paths/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ impl PartialEq<AbsPath> for AbsPathBuf {
6666
}
6767
}
6868

69+
impl serde::Serialize for AbsPathBuf {
70+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
71+
where
72+
S: serde::Serializer,
73+
{
74+
self.0.serialize(serializer)
75+
}
76+
}
77+
78+
impl<'de> serde::Deserialize<'de> for AbsPathBuf {
79+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
80+
where
81+
D: serde::Deserializer<'de>,
82+
{
83+
let path = PathBuf::deserialize(deserializer)?;
84+
AbsPathBuf::try_from(path).map_err(|path| {
85+
serde::de::Error::custom(format!("expected absolute path, got {}", path.display()))
86+
})
87+
}
88+
}
89+
6990
impl AbsPathBuf {
7091
/// Wrap the given absolute path in `AbsPathBuf`
7192
///

crates/proc_macro_api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ memmap2 = "0.3.0"
1818
object = { version = "0.25.3", default-features = false, features = ["std", "read_core", "elf", "macho", "pe"] }
1919
snap = "1.0"
2020

21+
paths = { path = "../paths", version = "0.0.0" }
2122
tt = { path = "../tt", version = "0.0.0" }
2223
base_db = { path = "../base_db", version = "0.0.0" }
2324
stdx = { path = "../stdx", version = "0.0.0" }

crates/proc_macro_api/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ mod rpc;
1111
mod version;
1212

1313
use base_db::{Env, ProcMacro};
14+
use paths::{AbsPath, AbsPathBuf};
1415
use std::{
1516
ffi::OsStr,
1617
io,
17-
path::{Path, PathBuf},
1818
sync::{Arc, Mutex},
1919
};
2020

@@ -28,7 +28,7 @@ pub use version::{read_dylib_info, RustCInfo};
2828
#[derive(Debug, Clone)]
2929
struct ProcMacroProcessExpander {
3030
process: Arc<Mutex<ProcMacroProcessSrv>>,
31-
dylib_path: PathBuf,
31+
dylib_path: AbsPathBuf,
3232
name: SmolStr,
3333
}
3434

@@ -79,26 +79,25 @@ pub struct ProcMacroClient {
7979
impl ProcMacroClient {
8080
/// Spawns an external process as the proc macro server and returns a client connected to it.
8181
pub fn extern_process(
82-
process_path: PathBuf,
82+
process_path: AbsPathBuf,
8383
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
8484
) -> io::Result<ProcMacroClient> {
8585
let process = ProcMacroProcessSrv::run(process_path, args)?;
8686
Ok(ProcMacroClient { process: Arc::new(Mutex::new(process)) })
8787
}
8888

89-
// TODO: use paths::AbsPath here
90-
pub fn by_dylib_path(&self, dylib_path: &Path) -> Vec<ProcMacro> {
89+
pub fn by_dylib_path(&self, dylib_path: &AbsPath) -> Vec<ProcMacro> {
9190
let _p = profile::span("ProcMacroClient::by_dylib_path");
9291
match version::read_dylib_info(dylib_path) {
9392
Ok(info) => {
9493
if info.version.0 < 1 || info.version.1 < 47 {
95-
eprintln!("proc-macro {} built by {:#?} is not supported by Rust Analyzer, please update your rust version.", dylib_path.to_string_lossy(), info);
94+
eprintln!("proc-macro {} built by {:#?} is not supported by Rust Analyzer, please update your rust version.", dylib_path.display(), info);
9695
}
9796
}
9897
Err(err) => {
9998
eprintln!(
10099
"proc-macro {} failed to find the given version. Reason: {}",
101-
dylib_path.to_string_lossy(),
100+
dylib_path.display(),
102101
err
103102
);
104103
}
@@ -129,7 +128,7 @@ impl ProcMacroClient {
129128
let expander = Arc::new(ProcMacroProcessExpander {
130129
process: self.process.clone(),
131130
name: name.clone(),
132-
dylib_path: dylib_path.into(),
131+
dylib_path: dylib_path.to_path_buf(),
133132
});
134133

135134
ProcMacro { name, kind, expander }

crates/proc_macro_api/src/process.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::{
44
convert::{TryFrom, TryInto},
55
ffi::{OsStr, OsString},
66
io::{self, BufRead, BufReader, Write},
7-
path::{Path, PathBuf},
87
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
98
};
109

10+
use paths::{AbsPath, AbsPathBuf};
1111
use stdx::JodChild;
1212

1313
use crate::{
@@ -24,7 +24,7 @@ pub(crate) struct ProcMacroProcessSrv {
2424

2525
impl ProcMacroProcessSrv {
2626
pub(crate) fn run(
27-
process_path: PathBuf,
27+
process_path: AbsPathBuf,
2828
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
2929
) -> io::Result<ProcMacroProcessSrv> {
3030
let mut process = Process::run(process_path, args)?;
@@ -37,7 +37,7 @@ impl ProcMacroProcessSrv {
3737

3838
pub(crate) fn find_proc_macros(
3939
&mut self,
40-
dylib_path: &Path,
40+
dylib_path: &AbsPath,
4141
) -> Result<Vec<(String, ProcMacroKind)>, tt::ExpansionError> {
4242
let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
4343

@@ -84,7 +84,7 @@ struct Process {
8484

8585
impl Process {
8686
fn run(
87-
path: PathBuf,
87+
path: AbsPathBuf,
8888
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
8989
) -> io::Result<Process> {
9090
let args: Vec<OsString> = args.into_iter().map(|s| s.as_ref().into()).collect();
@@ -101,8 +101,11 @@ impl Process {
101101
}
102102
}
103103

104-
fn mk_child(path: &Path, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> io::Result<Child> {
105-
Command::new(&path)
104+
fn mk_child(
105+
path: &AbsPath,
106+
args: impl IntoIterator<Item = impl AsRef<OsStr>>,
107+
) -> io::Result<Child> {
108+
Command::new(path.as_os_str())
106109
.args(args)
107110
.stdin(Stdio::piped())
108111
.stdout(Stdio::piped())

crates/proc_macro_api/src/rpc.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
//! to be much easier, we deliberately duplicate `tt` structs with `#[serde(with = "XXDef")]`
77
//! for separation of code responsibility.
88
9-
use std::path::PathBuf;
10-
9+
use paths::AbsPathBuf;
1110
use serde::{Deserialize, Serialize};
1211
use tt::{
1312
Delimiter, DelimiterKind, Ident, Leaf, Literal, Punct, SmolStr, Spacing, Subtree, TokenId,
@@ -16,7 +15,7 @@ use tt::{
1615

1716
#[derive(Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
1817
pub struct ListMacrosTask {
19-
pub lib: PathBuf,
18+
pub lib: AbsPathBuf,
2019
}
2120

2221
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
@@ -50,7 +49,7 @@ pub struct ExpansionTask {
5049
#[serde(with = "opt_subtree_def")]
5150
pub attributes: Option<Subtree>,
5251

53-
pub lib: PathBuf,
52+
pub lib: AbsPathBuf,
5453

5554
/// Environment variables to set during macro expansion.
5655
pub env: Vec<(String, String)>,

crates/proc_macro_api/src/version.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
use std::{
44
fs::File,
55
io::{self, Read},
6-
path::Path,
76
};
87

98
use memmap2::Mmap;
109
use object::read::{File as BinaryFile, Object, ObjectSection};
10+
use paths::AbsPath;
1111
use snap::read::FrameDecoder as SnapDecoder;
1212

1313
#[derive(Debug)]
@@ -19,7 +19,7 @@ pub struct RustCInfo {
1919
}
2020

2121
/// Read rustc dylib information
22-
pub fn read_dylib_info(dylib_path: &Path) -> io::Result<RustCInfo> {
22+
pub fn read_dylib_info(dylib_path: &AbsPath) -> io::Result<RustCInfo> {
2323
macro_rules! err {
2424
($e:literal) => {
2525
io::Error::new(io::ErrorKind::InvalidData, $e)
@@ -96,7 +96,7 @@ fn read_section<'a>(dylib_binary: &'a [u8], section_name: &str) -> io::Result<&'
9696
/// * [some more bytes that we don really care but still there] :-)
9797
/// Check this issue for more about the bytes layout:
9898
/// <https://github.com/rust-analyzer/rust-analyzer/issues/6174>
99-
fn read_version(dylib_path: &Path) -> io::Result<String> {
99+
fn read_version(dylib_path: &AbsPath) -> io::Result<String> {
100100
let dylib_file = File::open(dylib_path)?;
101101
let dylib_mmaped = unsafe { Mmap::map(&dylib_file) }?;
102102

crates/project_model/src/workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl ProjectWorkspace {
312312
) -> CrateGraph {
313313
let _p = profile::span("ProjectWorkspace::to_crate_graph");
314314
let proc_macro_loader = |path: &AbsPath| match proc_macro_client {
315-
Some(client) => client.by_dylib_path(path.as_ref()), // TODO
315+
Some(client) => client.by_dylib_path(path),
316316
None => Vec::new(),
317317
};
318318

0 commit comments

Comments
 (0)