Skip to content

Commit

Permalink
Add notification on corelib mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 committed Aug 14, 2024
1 parent c883b9d commit 3d0859d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/cairo-lang-compiler/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl RootDatabaseBuilder {
}

/// Validates that the corelib version matches the expected one.
fn validate_corelib(db: &RootDatabase) -> Result<()> {
pub fn validate_corelib(db: &dyn FilesGroup) -> Result<()> {
let Some(config) = db.crate_config(CrateLongId::Real(CORELIB_CRATE_NAME.into()).intern(db))
else {
return Ok(());
Expand Down
18 changes: 18 additions & 0 deletions crates/cairo-lang-language-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use std::sync::Arc;
use std::time::{Duration, SystemTime};

use anyhow::{bail, Context};
use cairo_lang_compiler::db::validate_corelib;
use cairo_lang_compiler::project::{setup_project, update_crate_roots_from_project_config};
use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{
Expand Down Expand Up @@ -80,6 +81,7 @@ use serde_json::Value;
use tokio::sync::Semaphore;
use tokio::task::spawn_blocking;
use tower_lsp::jsonrpc::{Error as LSPError, Result as LSPResult};
use tower_lsp::lsp_types::notification::Notification;
use tower_lsp::lsp_types::request::Request;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, ClientSocket, LanguageServer, LspService, Server};
Expand Down Expand Up @@ -597,6 +599,14 @@ impl Backend {
// Try to set up a corelib at least.
try_to_init_unmanaged_core(&*self.config.read().await, db);
}

let corelib_validation = validate_corelib(db);
if let Err(result) = corelib_validation {
let notifier = Notifier::new(&self.client);
spawn_blocking(move || {
notifier.send_notification::<CorelibVersionMismatch>(result.to_string());
});
}
}

Some(ProjectManifestPath::CairoProject(config_path)) => {
Expand Down Expand Up @@ -648,6 +658,14 @@ impl Backend {
}
}

#[derive(Debug)]
struct CorelibVersionMismatch {}

impl Notification for CorelibVersionMismatch {
type Params = String;
const METHOD: &'static str = "corelib/version-mismatch";
}

enum ServerCommands {
Reload,
}
Expand Down
10 changes: 10 additions & 0 deletions vscode-cairo/src/cairols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Scarb } from "./scarb";
import { isScarbProject } from "./scarbProject";
import { StandaloneLS } from "./standalonels";
import { registerMacroExpandProvider, registerVfsProvider } from "./textDocumentProviders";
import { NotificationType } from "vscode-jsonrpc/lib/common/messages";

export interface LanguageServerExecutableProvider {
languageServerExecutable(): lc.Executable;
Expand Down Expand Up @@ -82,6 +83,15 @@ export async function setupLanguageServer(ctx: Context): Promise<lc.LanguageClie
);
});

client.onNotification(new NotificationType<string>("corelib/version-mismatch"), (params) => {
const errorMessage =
"Corelib version missmatch. If you are using Scarb try reopening the project to fix this error. Resort to `scarb cache clean` if it doesn't help. ERROR: " +

Check warning on line 88 in vscode-cairo/src/cairols.ts

View workflow job for this annotation

GitHub Actions / typos

"missmatch" should be "mismatch".
params;

vscode.window.showErrorMessage(errorMessage);
ctx.log.error(errorMessage);
});

await client.start();

return client;
Expand Down

0 comments on commit 3d0859d

Please sign in to comment.