From 745790b53a3a7881f4f343b3117d60bd2503fa11 Mon Sep 17 00:00:00 2001 From: 0xSwapFeeder Date: Thu, 22 Feb 2024 00:11:38 -0500 Subject: [PATCH] wip(solidty/core/slither-server) added logs and security when initializing workspace folder --- .../core/crates/slither-server/src/main.rs | 76 +++++++++++++++---- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/toolchains/solidity/core/crates/slither-server/src/main.rs b/toolchains/solidity/core/crates/slither-server/src/main.rs index a38f8e19..0f2f23a8 100644 --- a/toolchains/solidity/core/crates/slither-server/src/main.rs +++ b/toolchains/solidity/core/crates/slither-server/src/main.rs @@ -57,12 +57,9 @@ impl LanguageServer for Backend { .log_message(MessageType::INFO, "Initializing diagnostic receiver ...") .await; let mut state = self.data.lock().await; - state.workspace = match params.workspace_folders { - Some(workspaces) => normalize_slither_path(workspaces[0].uri.path()), - None => normalize_slither_path(params.root_uri.unwrap().path()), - }; let mut receiver = state.receiver.take().unwrap(); let client = self.client.clone(); + self.join_handle .lock() .await @@ -80,15 +77,21 @@ impl LanguageServer for Backend { ) .await; - match self.initialize_filters(&mut state) { - Ok(_) => { - eprintln!("Filters initialized!"); - } - Err(e) => { - eprintln!("Error while initializing filters: {:?}", e); - } - } + self.client + .log_message(MessageType::INFO, "Initializing Workspace ...") + .await; + state.workspace = self + .fetch_workspace(params.workspace_folders, params.root_uri) + .await; + self.client + .log_message(MessageType::INFO, "Initializing filters ...") + .await; + self.initialize_filters(&mut state); + + self.client + .log_message(MessageType::INFO, "Slither-Server initialized!") + .await; Ok(InitializeResult { server_info: None, capabilities: ServerCapabilities { @@ -113,6 +116,29 @@ impl LanguageServer for Backend { .await; } + async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) { + let mut state = self.data.lock().await; + if params.event.added.is_empty() + && !params.event.removed.is_empty() + && state.workspace == "." + { + self.client + .log_message( + MessageType::WARNING, + "No workspace folder found, please open a folder!", + ) + .await; + return; + } + let folders: Vec = params + .event + .added + .iter() + .map(|folder| folder.to_owned()) + .collect(); + state.workspace = self.fetch_workspace(Some(folders), None).await; + } + async fn shutdown(&self) -> Result<()> { let state = self.data.lock().await; for process in state.slither_processes.iter() { @@ -210,7 +236,7 @@ impl Backend { false } - fn initialize_filters(&self, state: &mut MutexGuard) -> Result<()> { + fn initialize_filters(&self, state: &mut MutexGuard) { //register all work directories folder aliases using foundry.toml for each workspace folder let foundry_path = find_foundry_toml_config(&state.workspace); if let Ok(path) = foundry_path { @@ -227,7 +253,6 @@ impl Backend { } } } - Ok(()) } async fn launch_slither(&self, uri: Url) { @@ -276,6 +301,29 @@ impl Backend { } }); } + + async fn fetch_workspace( + &self, + workspace_folders: Option>, + root_uri: Option, + ) -> String { + let mut workspace = ".".to_string(); + match workspace_folders { + Some(workspaces) => workspace = normalize_slither_path(workspaces[0].uri.path()), + None => match root_uri { + Some(uri) => workspace = normalize_slither_path(uri.path()), + None => { + self.client + .log_message( + MessageType::WARNING, + "No workspace folder found, please open a folder!", + ) + .await; + } + }, + } + workspace + } } #[tokio::main]