diff --git a/toolchains/solidity/core/Cargo.lock b/toolchains/solidity/core/Cargo.lock index 49613805..757eb476 100644 --- a/toolchains/solidity/core/Cargo.lock +++ b/toolchains/solidity/core/Cargo.lock @@ -80,26 +80,6 @@ version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" -[[package]] -name = "async-stdin" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1ff8b5d9b5ec29e0f49583ba71847b8c8888b67a8510133048a380903aa6822" -dependencies = [ - "tokio", -] - -[[package]] -name = "async-throttle" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c99532de164435a0b91279e715bff4fa0d164643b409a67761907ffc210ee8f" -dependencies = [ - "backoff", - "dashmap", - "tokio", -] - [[package]] name = "async-trait" version = "0.1.77" @@ -129,20 +109,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" -[[package]] -name = "backoff" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62ddb9cb1ec0a098ad4bbf9344d0713fa193ae1a80af55febcff2627b6a00c1" -dependencies = [ - "futures-core", - "getrandom", - "instant", - "pin-project-lite", - "rand", - "tokio", -] - [[package]] name = "backtrace" version = "0.3.69" @@ -257,15 +223,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "crossbeam-queue" -version = "0.3.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df0346b5d5e76ac2fe4e327c5fd1118d6be7c51dfb18f9b7922923f287471e35" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-utils" version = "0.8.18" @@ -405,17 +362,6 @@ dependencies = [ "slab", ] -[[package]] -name = "getrandom" -version = "0.2.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "gimli" version = "0.28.1" @@ -481,15 +427,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", -] - [[package]] name = "itoa" version = "1.0.10" @@ -724,12 +661,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "ppv-lite86" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" - [[package]] name = "proc-macro-error" version = "1.0.4" @@ -772,36 +703,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "redox_syscall" version = "0.4.1" @@ -922,15 +823,6 @@ dependencies = [ "serde", ] -[[package]] -name = "shutdown-async" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2799e69bde7e68bedd86c6d94bffa783219114f1f31435ddda61f4aeba348ff" -dependencies = [ - "tokio", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -962,7 +854,6 @@ dependencies = [ "thiserror", "tokio", "tokio-util", - "tokio-utils", "toml", "tower-lsp", ] @@ -1141,18 +1032,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "tokio-utils" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de75f75f464153a50fe48b9675360e3cf2ae1d7d81f9751363bd2ee4888f5ce8" -dependencies = [ - "async-stdin", - "async-throttle", - "shutdown-async", - "tub", -] - [[package]] name = "toml" version = "0.8.8" @@ -1278,16 +1157,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "tub" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bca43faba247bc76eb1d6c1b8b561e4a1c5bdd427cc3d7a007faabea75c683a" -dependencies = [ - "crossbeam-queue", - "tokio", -] - [[package]] name = "unicode-bidi" version = "0.3.14" diff --git a/toolchains/solidity/core/crates/slither-server/Cargo.toml b/toolchains/solidity/core/crates/slither-server/Cargo.toml index 12839a65..a333b85c 100644 --- a/toolchains/solidity/core/crates/slither-server/Cargo.toml +++ b/toolchains/solidity/core/crates/slither-server/Cargo.toml @@ -19,5 +19,4 @@ colored = "2.0.4" thiserror = "1.0.50" glob = "0.3.1" toml = "0.8.8" -tokio-utils = "0.1.2" tokio-util = "0.7.10" diff --git a/toolchains/solidity/core/crates/slither-server/src/main.rs b/toolchains/solidity/core/crates/slither-server/src/main.rs index 5beb6011..a38f8e19 100644 --- a/toolchains/solidity/core/crates/slither-server/src/main.rs +++ b/toolchains/solidity/core/crates/slither-server/src/main.rs @@ -7,7 +7,7 @@ use crate::{error::SlitherError, slither::parse_slither_out, types::*}; use std::sync::Arc; use std::vec; -use tokio::sync::Mutex; +use tokio::sync::{Mutex, MutexGuard}; use tokio_util::sync::CancellationToken; use tower_lsp::jsonrpc::Result; use tower_lsp::lsp_types::*; @@ -56,8 +56,12 @@ impl LanguageServer for Backend { self.client .log_message(MessageType::INFO, "Initializing diagnostic receiver ...") .await; - - let mut receiver = self.data.lock().await.receiver.take().unwrap(); + 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() @@ -76,19 +80,13 @@ impl LanguageServer for Backend { ) .await; - let folders = params.workspace_folders; - if let Some(folder) = folders { - eprintln!("Initializing filters ..."); - match self.initialize_filters(folder).await { - Ok(_) => { - eprintln!("Filters initialized!"); - } - Err(e) => { - eprintln!("Error while initializing filters: {:?}", e); - } + match self.initialize_filters(&mut state) { + Ok(_) => { + eprintln!("Filters initialized!"); + } + Err(e) => { + eprintln!("Error while initializing filters: {:?}", e); } - } else { - eprintln!("No workspace folders found!"); } Ok(InitializeResult { @@ -183,7 +181,6 @@ impl Backend { let state = self.data.lock().await; for lib in state.libs_paths.iter() { let fsrc = format!("/{}/", lib.replace('\"', "")); - eprintln!("Check path: '{}' contains lib: '{}'", path, fsrc); if path.contains(&fsrc) { return true; } @@ -195,7 +192,6 @@ impl Backend { let state = self.data.lock().await; for src in state.src_paths.iter() { let fsrc = format!("/{}/", src.replace('\"', "")); - eprintln!("Check path: '{}' contains src: '{}'", path, fsrc); if path.contains(&fsrc) { return true; } @@ -207,7 +203,6 @@ impl Backend { let state = self.data.lock().await; for test in state.tests_paths.iter() { let fsrc = format!("/{}/", test.replace('\"', "")); - eprintln!("Check path: '{}' contains test: '{}'", path, fsrc); if path.contains(&fsrc) { return true; } @@ -215,43 +210,42 @@ impl Backend { false } - async fn initialize_filters(&self, workspaces: Vec) -> Result<()> { - let mut state = self.data.lock().await; + fn initialize_filters(&self, state: &mut MutexGuard) -> Result<()> { //register all work directories folder aliases using foundry.toml for each workspace folder - for folder in workspaces { - let folderpath = normalize_slither_path(folder.uri.path()); - let foundry_path = find_foundry_toml_config(&folderpath); - if let Ok(path) = foundry_path { - let foundry = std::fs::read_to_string(path.clone()); - match foundry { - Ok(foundry) => { - parse_foundry_toml(foundry, &mut state); - } - Err(e) => { - eprintln!( - "Error while reading foundry.toml file: {:?}, path: {}", - e, path - ); - } - } + let foundry_path = find_foundry_toml_config(&state.workspace); + if let Ok(path) = foundry_path { + let foundry = std::fs::read_to_string(path.clone()); + match foundry { + Ok(foundry) => { + parse_foundry_toml(foundry, state); } + Err(e) => { + eprintln!( + "Error while reading foundry.toml file: {:?}, path: {}", + e, path + ); + } + } } Ok(()) } async fn launch_slither(&self, uri: Url) { + let filepath = normalize_slither_path(uri.path()); + let mut state = self.data.lock().await; let token = CancellationToken::new(); let clone = token.clone(); - self.data.lock().await.slither_processes.push(token); - let sender_handle = self.data.lock().await.sender.clone(); + state.slither_processes.push(token); + let sender_handle = state.sender.clone(); let client = self.client.clone(); + let workspace = state.workspace.clone(); tokio::spawn(async move { tokio::select! { _ = clone.cancelled() => { eprintln!("SLITHER CANCELLED"); } - output = parse_slither_out(uri.path()) => { + output = parse_slither_out(&filepath, &workspace) => { match output { Ok(res) => { let _ = sender_handle.send(SlitherDiag::new(uri, res)).await; @@ -262,7 +256,7 @@ impl Backend { MessageType::ERROR, format!( "File '{}' did generate an error while parsing the output: {:?}", - uri.path(), + filepath, e ), ) @@ -273,7 +267,7 @@ impl Backend { client .log_message( MessageType::ERROR, - format!("File '{}' did generate an error: {:?}", uri.path(), e), + format!("File '{}' did generate an error: {:?}", filepath, e), ) .await; } diff --git a/toolchains/solidity/core/crates/slither-server/src/slither.rs b/toolchains/solidity/core/crates/slither-server/src/slither.rs index b3bff2b0..dd4d4cf6 100644 --- a/toolchains/solidity/core/crates/slither-server/src/slither.rs +++ b/toolchains/solidity/core/crates/slither-server/src/slither.rs @@ -1,13 +1,15 @@ -use crate::{error::SlitherError, types::SlitherResult, utils::normalize_slither_path}; +use crate::{error::SlitherError, types::SlitherResult}; use std::process::Stdio; use tokio::{io::AsyncReadExt, process::Command}; use tower_lsp::lsp_types::Diagnostic; -pub async fn parse_slither_out(uri: &str) -> Result, SlitherError> { +pub async fn parse_slither_out( + uri: &str, + workspace: &str, +) -> Result, SlitherError> { let mut results: Vec = Vec::new(); - eprintln!("SLITHER STARTING"); - let mut output = exec_slither(uri)?; + let mut output = exec_slither(uri, workspace)?; let out = match output.stdout.take() { Some(out) => out, None => { @@ -21,11 +23,9 @@ pub async fn parse_slither_out(uri: &str) -> Result, SlitherErro let mut dst = String::new(); output.wait().await?; - eprintln!("SLITHER FINISHED"); buffer.read_to_string(&mut dst).await?; let json: Result = serde_json::from_str(&dst); - match json { Ok(json) => { for detector in json.results.detectors { @@ -41,9 +41,10 @@ pub async fn parse_slither_out(uri: &str) -> Result, SlitherErro Ok(results) } -fn exec_slither(uri: &str) -> Result { +fn exec_slither(uri: &str, workspace: &str) -> Result { Command::new("slither") - .arg(normalize_slither_path(uri)) + .current_dir(workspace) + .arg(uri) .arg("--exclude") .arg("naming-convention") .arg("--json") diff --git a/toolchains/solidity/core/crates/slither-server/src/types.rs b/toolchains/solidity/core/crates/slither-server/src/types.rs index 28550c6e..53917d83 100644 --- a/toolchains/solidity/core/crates/slither-server/src/types.rs +++ b/toolchains/solidity/core/crates/slither-server/src/types.rs @@ -25,6 +25,7 @@ pub struct SlitherData { pub libs_paths: Vec, pub src_paths: Vec, pub tests_paths: Vec, + pub workspace: String, } impl SlitherData { @@ -37,6 +38,7 @@ impl SlitherData { slither_processes: vec![], receiver: Some(receiver), sender, + workspace: String::new(), } } }