Skip to content

Commit

Permalink
Add notification on corelib mismatch (#6213)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 authored Aug 21, 2024
1 parent 562bf3c commit 85275d9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
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 @@ -182,7 +182,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
8 changes: 8 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 @@ -91,6 +92,7 @@ use crate::lang::db::{AnalysisDatabase, LsSemanticGroup, LsSyntaxGroup};
use crate::lang::diagnostics::lsp::map_cairo_diagnostics_to_lsp;
use crate::lang::lsp::LsProtoGroup;
use crate::lsp::client_capabilities::ClientCapabilitiesExt;
use crate::lsp::ext::CorelibVersionMismatch;
use crate::project::scarb::update_crate_roots;
use crate::project::unmanaged_core_crate::try_to_init_unmanaged_core;
use crate::project::ProjectManifestPath;
Expand Down Expand Up @@ -597,6 +599,12 @@ impl Backend {
// Try to set up a corelib at least.
try_to_init_unmanaged_core(&*self.config.read().await, db);
}

if let Err(result) = validate_corelib(db) {
self.client
.send_notification::<CorelibVersionMismatch>(result.to_string())
.await;
}
}

Some(ProjectManifestPath::CairoProject(config_path)) => {
Expand Down
12 changes: 11 additions & 1 deletion crates/cairo-lang-language-server/src/lsp/ext.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! CairoLS extensions to the Language Server Protocol.

use tower_lsp::lsp_types::notification::Notification;
use tower_lsp::lsp_types::request::Request;
use tower_lsp::lsp_types::TextDocumentPositionParams;

// TODO(mkaput): Provide this as a command in VSCode.
/// Collect information about all Cairo crates that are currently being analyzed.
/// Collects information about all Cairo crates that are currently being analyzed.
pub struct ViewAnalyzedCrates;

impl Request for ViewAnalyzedCrates {
Expand All @@ -21,3 +22,12 @@ impl Request for ExpandMacro {
type Result = Option<String>;
const METHOD: &'static str = "cairo/expandMacro";
}

/// Notifies about corelib version mismatch.
#[derive(Debug)]
pub struct CorelibVersionMismatch;

impl Notification for CorelibVersionMismatch {
type Params = String;
const METHOD: &'static str = "cairo/corelib-version-mismatch";
}
30 changes: 30 additions & 0 deletions vscode-cairo/src/cairols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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";
import * as cp from "node:child_process";

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

client.onNotification(
new NotificationType<string>("cairo/corelib-version-mismatch"),
async (errorMessage) => {
ctx.log.error(errorMessage);

const reloadWindow = "Reload window";
const cleanScarbCache = "Clean Scarb's cache";

const selectedValue = await vscode.window.showErrorMessage(
errorMessage,
reloadWindow,
cleanScarbCache,
);

if (selectedValue === reloadWindow) {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
} else if (selectedValue === cleanScarbCache) {
cp.exec("scarb cache clean", (err, stdout, stderr) => {
ctx.log.trace("`scarb cache clean` stdout: " + stdout);
ctx.log.trace("`scarb cache clean` stderr: " + stderr);
if (err) {
ctx.log.warn("error: " + err);
}
});
}
},
);

await client.start();

return client;
Expand Down

0 comments on commit 85275d9

Please sign in to comment.