Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit e293bb8

Browse files
committed
Tidy up the remaining implementation
1 parent 07ace5e commit e293bb8

File tree

2 files changed

+16
-55
lines changed

2 files changed

+16
-55
lines changed

rls/src/build/ipc.rs

+3-40
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::time::Duration;
66
use std::{env, fs};
77

88
use jsonrpc_core::{ErrorCode, IoHandler};
9-
use rls_vfs::{FileContents, Vfs};
109

1110
use crate::build::plan::Crate;
1211

@@ -33,7 +32,8 @@ impl Server {
3332
}
3433
}
3534

36-
/// TODO: Document me
35+
/// Starts an IPC server in the background supporting both VFS requests and data
36+
/// callbacks used by rustc for the out-of-process compilation.
3737
pub fn start_with_all(
3838
changed_files: HashMap<PathBuf, String>,
3939
analysis: Arc<Mutex<Option<rls_data::Analysis>>>,
@@ -49,18 +49,6 @@ pub fn start_with_all(
4949
self::start_with_handler(io)
5050
}
5151

52-
/// Spins up an IPC server in the background. Currently used for inter-process
53-
/// VFS, which is required for out-of-process rustc compilation.
54-
#[allow(dead_code)]
55-
pub fn start(vfs: Arc<Vfs>) -> Result<Server, ()> {
56-
use rls_ipc::rpc::file_loader::Server as _;
57-
58-
let mut io = IoHandler::new();
59-
io.extend_with(ArcVfs(vfs).to_delegate());
60-
61-
self::start_with_handler(io)
62-
}
63-
6452
/// Spins up an IPC server in the background.
6553
pub fn start_with_handler(io: IoHandler) -> Result<Server, ()> {
6654
let endpoint_path = gen_endpoint_path();
@@ -104,37 +92,12 @@ fn rpc_error(msg: &str) -> Error {
10492
Error { code: ErrorCode::InternalError, message: msg.to_owned(), data: None }
10593
}
10694

107-
struct ArcVfs(Arc<Vfs>);
108-
109-
impl rpc::file_loader::Rpc for ArcVfs {
110-
fn file_exists(&self, path: PathBuf) -> RpcResult<bool> {
111-
// Copied from syntax::source_map::RealFileLoader
112-
Ok(fs::metadata(path).is_ok())
113-
}
114-
fn abs_path(&self, path: PathBuf) -> RpcResult<Option<PathBuf>> {
115-
// Copied from syntax::source_map::RealFileLoader
116-
Ok(if path.is_absolute() {
117-
Some(path.to_path_buf())
118-
} else {
119-
env::current_dir().ok().map(|cwd| cwd.join(path))
120-
})
121-
}
122-
fn read_file(&self, path: PathBuf) -> RpcResult<String> {
123-
self.0.load_file(&path).map_err(|e| rpc_error(&e.to_string())).and_then(|contents| {
124-
match contents {
125-
FileContents::Text(text) => Ok(text),
126-
FileContents::Binary(..) => Err(rpc_error("File is binary")),
127-
}
128-
})
129-
}
130-
}
131-
13295
mod callbacks {
13396
use super::PathBuf;
13497
use super::{rpc, RpcResult};
13598
use super::{Arc, Mutex};
13699
use super::{HashMap, HashSet};
137-
// use crate::build::plan::Crate;
100+
138101
impl From<rls_ipc::rpc::Crate> for crate::build::plan::Crate {
139102
fn from(krate: rls_ipc::rpc::Crate) -> Self {
140103
Self {

rls/src/build/rustc.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ pub(crate) fn rustc(
6969

7070
let changed = vfs.get_cached_files();
7171

72-
let mut local_envs = envs.clone();
72+
let mut envs = envs.clone();
7373

7474
let clippy_preference = {
7575
let config = rls_config.lock().unwrap();
7676
if config.clear_env_rust_log {
77-
local_envs.insert(String::from("RUST_LOG"), None);
77+
envs.insert(String::from("RUST_LOG"), None);
7878
}
7979

8080
config.clippy_preference
@@ -89,15 +89,16 @@ pub(crate) fn rustc(
8989
"RLS_OUT_OF_PROCESS",
9090
) {
9191
#[cfg(feature = "ipc")]
92-
Ok(..) => run_out_of_process(changed, &args, &local_envs, clippy_preference),
92+
Ok(..) => run_out_of_process(changed.clone(), &args, &envs, clippy_preference)
93+
.unwrap_or_else(|_| {
94+
run_in_process(changed, &args, clippy_preference, lock_environment(&envs, cwd))
95+
}),
9396
#[cfg(not(feature = "ipc"))]
9497
Ok(..) => {
95-
log::warn!("Support for out-of-process compilation was not compiled. Re-build with 'ipc' feature enabled");
96-
run_in_process(changed, &args, clippy_preference, lock_environment(&local_envs, cwd))
97-
}
98-
Err(..) => {
99-
run_in_process(changed, &args, clippy_preference, lock_environment(&local_envs, cwd))
98+
log::warn!("Support for out-of-process compilation was not compiled. Rebuild with 'ipc' feature enabled");
99+
run_in_process(changed, &args, clippy_preference, lock_environment(&envs, cwd))
100100
}
101+
Err(..) => run_in_process(changed, &args, clippy_preference, lock_environment(&envs, cwd)),
101102
};
102103

103104
let stderr = String::from_utf8(stderr).unwrap();
@@ -128,13 +129,12 @@ fn run_out_of_process(
128129
args: &[String],
129130
envs: &HashMap<String, Option<OsString>>,
130131
clippy_preference: ClippyPreference,
131-
) -> CompilationResult {
132+
) -> Result<CompilationResult, ()> {
132133
let analysis = Arc::default();
133134
let input_files = Arc::default();
134135

135136
let ipc_server =
136-
super::ipc::start_with_all(changed, Arc::clone(&analysis), Arc::clone(&input_files));
137-
let ipc_server = ipc_server.unwrap(); // TODO: Handle unwrap
137+
super::ipc::start_with_all(changed, Arc::clone(&analysis), Arc::clone(&input_files))?;
138138

139139
// Compiling out of process is only supported by our own shim
140140
let rustc_shim = env::current_exe()
@@ -143,13 +143,11 @@ fn run_out_of_process(
143143
.expect("Couldn't set executable for RLS rustc shim");
144144

145145
let output = Command::new(rustc_shim)
146-
// In case RLS is set as the rustc shim signal to `main_inner()` to call
147-
// `rls_rustc::run()`.
148146
.env(crate::RUSTC_SHIM_ENV_VAR_NAME, "1")
149147
.env("RLS_IPC_ENDPOINT", ipc_server.endpoint())
150148
.env("RLS_CLIPPY_PREFERENCE", clippy_preference.to_string())
151149
.args(args.iter().skip(1))
152-
.envs(envs.clone().into_iter().filter_map(|(k, v)| v.map(|v| (k, v))))
150+
.envs(envs.iter().filter_map(|(k, v)| v.as_ref().map(|v| (k, v))))
153151
.output()
154152
.map_err(|_| ());
155153

@@ -166,7 +164,7 @@ fn run_out_of_process(
166164
let analysis = unwrap_shared(analysis, "Other ref dropped by closed IPC server");
167165
// FIXME(#25): given that we are running the compiler directly, there is no need
168166
// to serialize the error messages -- we should pass them in memory.
169-
CompilationResult { result, stderr, analysis, input_files }
167+
Ok(CompilationResult { result, stderr, analysis, input_files })
170168
}
171169

172170
fn run_in_process(

0 commit comments

Comments
 (0)