Skip to content

Commit 8d8c26e

Browse files
committed
internal: a bit more of cwd safety for flycheck
1 parent 8df38aa commit 8d8c26e

File tree

16 files changed

+71
-51
lines changed

16 files changed

+71
-51
lines changed

Cargo.lock

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

crates/flycheck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ jod-thread = "0.1.1"
1818

1919
toolchain = { path = "../toolchain", version = "0.0.0" }
2020
stdx = { path = "../stdx", version = "0.0.0" }
21+
paths = { path = "../paths", version = "0.0.0" }

crates/flycheck/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
use std::{
66
fmt,
77
io::{self, BufRead, BufReader},
8-
path::PathBuf,
98
process::{self, Command, Stdio},
109
time::Duration,
1110
};
1211

1312
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
13+
use paths::AbsPathBuf;
1414
use serde::Deserialize;
1515
use stdx::JodChild;
1616

@@ -63,7 +63,7 @@ impl FlycheckHandle {
6363
id: usize,
6464
sender: Box<dyn Fn(Message) + Send>,
6565
config: FlycheckConfig,
66-
workspace_root: PathBuf,
66+
workspace_root: AbsPathBuf,
6767
) -> FlycheckHandle {
6868
let actor = FlycheckActor::new(id, sender, config, workspace_root);
6969
let (sender, receiver) = unbounded::<Restart>();
@@ -82,7 +82,7 @@ impl FlycheckHandle {
8282

8383
pub enum Message {
8484
/// Request adding a diagnostic with fixes included to a file
85-
AddDiagnostic { workspace_root: PathBuf, diagnostic: Diagnostic },
85+
AddDiagnostic { workspace_root: AbsPathBuf, diagnostic: Diagnostic },
8686

8787
/// Request check progress notification to client
8888
Progress {
@@ -121,7 +121,7 @@ struct FlycheckActor {
121121
id: usize,
122122
sender: Box<dyn Fn(Message) + Send>,
123123
config: FlycheckConfig,
124-
workspace_root: PathBuf,
124+
workspace_root: AbsPathBuf,
125125
/// WatchThread exists to wrap around the communication needed to be able to
126126
/// run `cargo check` without blocking. Currently the Rust standard library
127127
/// doesn't provide a way to read sub-process output without blocking, so we
@@ -140,7 +140,7 @@ impl FlycheckActor {
140140
id: usize,
141141
sender: Box<dyn Fn(Message) + Send>,
142142
config: FlycheckConfig,
143-
workspace_root: PathBuf,
143+
workspace_root: AbsPathBuf,
144144
) -> FlycheckActor {
145145
FlycheckActor { id, sender, config, workspace_root, cargo_handle: None }
146146
}
@@ -220,7 +220,7 @@ impl FlycheckActor {
220220
cmd.arg(command);
221221
cmd.current_dir(&self.workspace_root);
222222
cmd.args(&["--workspace", "--message-format=json", "--manifest-path"])
223-
.arg(self.workspace_root.join("Cargo.toml"));
223+
.arg(self.workspace_root.join("Cargo.toml").as_os_str());
224224

225225
if let Some(target) = target_triple {
226226
cmd.args(&["--target", target.as_str()]);

crates/paths/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ impl AbsPath {
186186
pub fn starts_with(&self, base: &AbsPath) -> bool {
187187
self.0.starts_with(&base.0)
188188
}
189+
pub fn ends_with(&self, suffix: &RelPath) -> bool {
190+
self.0.starts_with(&suffix.0)
191+
}
189192

190193
// region:delegate-methods
191194

crates/proc_macro_api/src/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ mod tests {
271271
macro_body: tt.clone(),
272272
macro_name: Default::default(),
273273
attributes: None,
274-
lib: Default::default(),
274+
lib: AbsPathBuf::assert(std::env::current_dir().unwrap()),
275275
env: Default::default(),
276276
};
277277

crates/proc_macro_srv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ memmap2 = "0.3.0"
1515

1616
tt = { path = "../tt", version = "0.0.0" }
1717
mbe = { path = "../mbe", version = "0.0.0" }
18+
paths = { path = "../paths", version = "0.0.0" }
1819
proc_macro_api = { path = "../proc_macro_api", version = "0.0.0" }
1920

2021
[dev-dependencies]

crates/proc_macro_srv/src/dylib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Handles dynamic library loading for proc macro
22
33
use std::{
4+
convert::TryInto,
45
fmt,
56
fs::File,
67
io,
@@ -10,6 +11,7 @@ use std::{
1011
use libloading::Library;
1112
use memmap2::Mmap;
1213
use object::Object;
14+
use paths::AbsPath;
1315
use proc_macro_api::{read_dylib_info, ProcMacroKind};
1416

1517
use super::abis::Abi;
@@ -116,7 +118,10 @@ impl ProcMacroLibraryLibloading {
116118
invalid_data_err(format!("Cannot find registrar symbol in file {}", file.display()))
117119
})?;
118120

119-
let version_info = read_dylib_info(file)?;
121+
let abs_file: &AbsPath = file.try_into().map_err(|_| {
122+
invalid_data_err(format!("expected an absolute path, got {}", file.display()))
123+
})?;
124+
let version_info = read_dylib_info(&abs_file)?;
120125

121126
let lib = load_library(file).map_err(invalid_data_err)?;
122127
let abi = Abi::from_lib(&lib, symbol_name, version_info)?;
@@ -136,7 +141,7 @@ impl Expander {
136141

137142
let lib = ensure_file_with_lock_free_access(&lib)?;
138143

139-
let library = ProcMacroLibraryLibloading::open(&lib)?;
144+
let library = ProcMacroLibraryLibloading::open(lib.as_ref())?;
140145

141146
Ok(Expander { inner: library })
142147
}

crates/proc_macro_srv/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) struct ProcMacroSrv {
3030

3131
impl ProcMacroSrv {
3232
pub fn expand(&mut self, task: &ExpansionTask) -> Result<ExpansionResult, String> {
33-
let expander = self.expander(&task.lib)?;
33+
let expander = self.expander(task.lib.as_ref())?;
3434

3535
let mut prev_env = HashMap::new();
3636
for (k, v) in &task.env {
@@ -54,7 +54,7 @@ impl ProcMacroSrv {
5454
}
5555

5656
pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
57-
let expander = self.expander(&task.lib)?;
57+
let expander = self.expander(task.lib.as_ref())?;
5858
Ok(ListMacrosResult { macros: expander.list_macros() })
5959
}
6060

crates/proc_macro_srv/src/tests/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[macro_use]
44
mod utils;
55
use expect_test::expect;
6+
use paths::AbsPathBuf;
67
use utils::*;
78

89
#[test]
@@ -95,7 +96,7 @@ fn list_test_macros() {
9596

9697
#[test]
9798
fn test_version_check() {
98-
let path = fixtures::proc_macro_test_dylib_path();
99+
let path = AbsPathBuf::assert(fixtures::proc_macro_test_dylib_path());
99100
let info = proc_macro_api::read_dylib_info(&path).unwrap();
100101
assert!(info.version.1 >= 50);
101102
}

crates/proc_macro_srv/src/tests/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::dylib;
44
use crate::ProcMacroSrv;
55
use expect_test::Expect;
6+
use paths::AbsPathBuf;
67
use proc_macro_api::ListMacrosTask;
78
use std::str::FromStr;
89

@@ -41,7 +42,7 @@ fn assert_expand_impl(macro_name: &str, input: &str, attr: Option<&str>, expect:
4142
}
4243

4344
pub fn list() -> Vec<String> {
44-
let path = fixtures::proc_macro_test_dylib_path();
45+
let path = AbsPathBuf::assert(fixtures::proc_macro_test_dylib_path());
4546
let task = ListMacrosTask { lib: path };
4647
let mut srv = ProcMacroSrv::default();
4748
let res = srv.list_macros(&task).unwrap();

0 commit comments

Comments
 (0)