Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
fix(solidity/core/foundry-compiler-server): some windows specific issues
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSwapFeeder committed Dec 8, 2023
1 parent e0eb6a0 commit d0d7c3c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 28 deletions.
7 changes: 4 additions & 3 deletions libs/foundry-wrapper/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
error::Error,
types::ProjectCompileOutput,
utils::{check_executable_argument, find_forge_executable, find_projects_paths},
utils::{check_executable_argument, find_forge_executable, find_projects_paths, normalize_path},
};
use std::process::Command;

Expand Down Expand Up @@ -31,10 +31,11 @@ impl Compiler {
}

fn find_closest_workspace(&self, file_path: &str) -> Option<String> {
let filepath = normalize_path(file_path);
self.inner
.workspaces
.iter()
.filter(|path| file_path.starts_with(path.as_str()))
.filter(|path| filepath.starts_with(path.as_str()))
.max_by_key(|path| path.len())
.map(|path| path.to_string())
}
Expand All @@ -43,7 +44,7 @@ impl Compiler {
let paths = find_projects_paths(&root_folder)?;
for path in paths {
if let Some(path) = path.to_str() {
self.inner.workspaces.push(path.to_string());
self.inner.workspaces.push(normalize_path(path));
}
}
self.inner.root_path = root_folder;
Expand Down
7 changes: 4 additions & 3 deletions libs/foundry-wrapper/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ pub struct Position {
impl Position {
pub fn from_index(idx: i32, source: &str) -> Option<Self> {
let mut idx: usize = idx as usize;
for (i, l) in source.lines().enumerate() {
if idx < l.len() {
for (i, l) in source.split("\n").enumerate() {
let line_length = l.len() + if l.ends_with("\r") { 2 } else { 1 };
if idx < line_length {
return Some(Self {
line: i as u32,
column: idx as u32,
});
}
idx -= l.len() + 1;
idx -= line_length
}
None
}
Expand Down
4 changes: 4 additions & 0 deletions libs/foundry-wrapper/src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ pub fn find_projects_paths(root_path: &str) -> Result<Vec<PathBuf>, glob::Patter
.map(|path| path.parent().unwrap().to_path_buf())
.collect())
}

pub fn normalize_path(path: &str) -> String {
path.replace("\\", "/").replace("//", "/").replace("\\\\", "/")
}
39 changes: 23 additions & 16 deletions toolchains/solidity/core/crates/foundry-compiler-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
mod utils;
use utils::{convert_severity, get_root_path};
use utils::{convert_severity, get_root_path, normalize_path, slashify_path};
mod affected_files_store;
use affected_files_store::AffectedFilesStore;

Expand Down Expand Up @@ -41,7 +41,7 @@ impl LanguageServer for Backend {
),
)
.await;
self.load_workspace(path).await;
self.load_workspace(normalize_path(path.as_str())).await;
} else {
self.client
.log_message(
Expand Down Expand Up @@ -74,7 +74,10 @@ impl LanguageServer for Backend {
format!("file opened!: {:}", params.text_document.uri),
)
.await;
self.compile(params.text_document.uri.path().to_string())
if params.text_document.uri.path().contains("forge-std") {
return;
}
self.compile(slashify_path(&normalize_path(params.text_document.uri.path())))
.await;
}

Expand All @@ -85,7 +88,7 @@ impl LanguageServer for Backend {
format!("file changed!: {:}", params.text_document.uri),
)
.await;
self.compile(params.text_document.uri.path().to_string())
self.compile(slashify_path(&normalize_path(params.text_document.uri.path())))
.await;
}

Expand Down Expand Up @@ -164,7 +167,7 @@ impl Backend {
.log_message(MessageType::INFO, format!("Compile errors: {:?}", output.get_errors()))
.await;*/
drop(state);
self.publish_errors_diagnostics(project_path, filepath, output)
self.publish_errors_diagnostics(slashify_path(&project_path), filepath, output)
.await;
}
Err(err) => {
Expand All @@ -184,7 +187,7 @@ impl Backend {
filepath: String,
output: ProjectCompileOutput,
) {
let mut diagnostics = HashMap::<Url, Vec<Diagnostic>>::new();
let mut diagnostics = HashMap::<String, Vec<Diagnostic>>::new();
for error in output.get_errors() {
eprintln!("error: {:?}", error);
let (source_content_filepath, range) =
Expand Down Expand Up @@ -212,11 +215,10 @@ impl Backend {
tags: None,
data: None,
};
let url = Url::parse(&format!(
"file://{}",
source_content_filepath.to_str().unwrap()
))
.unwrap();
let url = match source_content_filepath.to_str() {
Some(source_path) => slashify_path(source_path),
None => continue,
};
if !diagnostics.contains_key(&url) {
diagnostics.insert(url.clone(), vec![diagnostic]);
} else {
Expand All @@ -227,9 +229,13 @@ impl Backend {
self.add_not_affected_files(project_path, filepath, &mut diagnostics)
.await;
for (uri, diags) in diagnostics.iter() {
self.client
.publish_diagnostics(uri.clone(), diags.clone(), None)
if let Ok(url) = Url::parse(&format!("file://{}", &uri)) {
self.client
.publish_diagnostics(url, diags.clone(), None)
.await;
} else {
self.client.log_message(MessageType::ERROR, "error, cannot parse file uri").await;
}
}
}

Expand Down Expand Up @@ -285,7 +291,7 @@ impl Backend {
&self,
project_path: String,
filepath: String,
files: &mut HashMap<Url, Vec<Diagnostic>>,
raised_diagnostics: &mut HashMap<String, Vec<Diagnostic>>,
) {
let mut state = self.state.lock().await;

Expand All @@ -296,10 +302,11 @@ impl Backend {
let affected_files = state.affected_files.get_affected_files(&project_path);
drop(state);
let mut without_diagnostics = vec![];

for file in affected_files {
let url = Url::parse(&format!("file://{}", file)).unwrap();
if !raised_diagnostics.contains_key(&file) { // if not potential not affected file is not in raised diags
if let std::collections::hash_map::Entry::Vacant(e) = files.entry(url) {
e.insert(vec![]);
raised_diagnostics.insert(file.clone(), vec![]);
without_diagnostics.push(file);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ pub fn convert_severity(severity: Severity) -> DiagnosticSeverity {
Severity::Info => DiagnosticSeverity::INFORMATION,
}
}

#[cfg(target_family = "windows")]
pub fn normalize_path(path: &str) -> String {
let mut path = path.replace("%3A/", "://");
path.remove(0);
path.to_string()
}

#[cfg(not(target_family = "windows"))]
pub fn normalize_path(path: &str) -> String {
path.to_string()
}

pub fn slashify_path(path: &str) -> String {
path.replace("\\", "/").replace("\\\\", "/").replace("//", "/")
}
5 changes: 1 addition & 4 deletions toolchains/solidity/extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

import * as path from 'path';
import { workspace, ExtensionContext } from 'vscode';

[]
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';
import { createLinterClient } from './linter';
import { createFoundryCompilerClient } from './foundry-compiler';
Expand Down
6 changes: 4 additions & 2 deletions toolchains/solidity/extension/src/foundry-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import {
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';
import * as os from 'os';

export function createFoundryCompilerClient(context: ExtensionContext): LanguageClient {
// The server is implemented in node
const serverBinary = context.asAbsolutePath(
path.join('dist', 'foundry-compiler-server')
path.join('dist',
os.platform().startsWith("win") ? 'foundry-compiler-server.exe' : 'foundry-compiler-server')
);

// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
// Otherwise the run options are used[]
const serverOptions: ServerOptions = {
run: { command: serverBinary, transport: TransportKind.stdio },
debug: {
Expand Down

0 comments on commit d0d7c3c

Please sign in to comment.