cairo_lang_language_server/
lib.rs

1//! # CairoLS
2//!
3//! Implements the LSP protocol over stdin/out.
4//!
5//! ## Running vanilla
6//!
7//! This is basically the source code of the `cairo-language-server` and
8//! `scarb cairo-language-server` binaries.
9//!
10//! ```no_run
11//! # #![allow(clippy::needless_doctest_main)]
12//! fn main() {
13//!     cairo_lang_language_server::start();
14//! }
15//! ```
16//!
17//! ## Running with customizations
18//!
19//! Due to the immaturity of various Cairo compiler parts (especially around potentially
20//! dynamically-loadable things), for some projects it might be necessary to provide a custom build
21//! of CairoLS that includes custom modifications to the compiler.
22//! The [`start_with_tricks`] function allows building a customized build of CairoLS that supports
23//! project-specific features.
24//! See the [`Tricks`] struct documentation for available customizations.
25//!
26//! ```no_run
27//! # #![allow(clippy::needless_doctest_main)]
28//! use cairo_lang_language_server::Tricks;
29//!
30//! # fn dojo_plugin_suite() -> cairo_lang_semantic::plugin::PluginSuite {
31//! #    // Returning something realistic, to make sure restrictive trait bounds do compile.
32//! #    cairo_lang_starknet::starknet_plugin_suite()
33//! # }
34//! fn main() {
35//!     let mut tricks = Tricks::default();
36//!     tricks.extra_plugin_suites = Some(&|| vec![dojo_plugin_suite()]);
37//!     cairo_lang_language_server::start_with_tricks(tricks);
38//! }
39//! ```
40
41use std::panic::RefUnwindSafe;
42use std::path::PathBuf;
43use std::process::ExitCode;
44use std::time::SystemTime;
45use std::{io, panic};
46
47use anyhow::Result;
48use cairo_lang_filesystem::db::FilesGroup;
49use cairo_lang_filesystem::ids::FileLongId;
50use cairo_lang_semantic::plugin::PluginSuite;
51use crossbeam::channel::{Receiver, select_biased};
52use lsp_server::Message;
53use lsp_types::RegistrationParams;
54use salsa::{Database, Durability};
55use tracing::{debug, error, info};
56
57use crate::lang::lsp::LsProtoGroup;
58use crate::lang::proc_macros::controller::ProcMacroChannelsReceivers;
59use crate::lsp::capabilities::server::{
60    collect_dynamic_registrations, collect_server_capabilities,
61};
62use crate::lsp::result::LSPResult;
63use crate::project::{ProjectController, ProjectUpdate};
64use crate::server::client::{Notifier, Requester, Responder};
65use crate::server::connection::{Connection, ConnectionInitializer};
66use crate::server::panic::is_cancelled;
67use crate::server::schedule::thread::JoinHandle;
68use crate::server::schedule::{Scheduler, Task, event_loop_thread};
69use crate::state::State;
70
71mod config;
72mod env_config;
73mod ide;
74mod lang;
75pub mod lsp;
76mod project;
77mod server;
78mod state;
79mod toolchain;
80
81/// Carries various customizations that can be applied to CairoLS.
82///
83/// See [the top-level documentation][lib] documentation for usage examples.
84///
85/// [lib]: crate#running-with-customizations
86#[non_exhaustive]
87#[derive(Default, Clone)]
88pub struct Tricks {
89    /// A function that returns a list of additional compiler plugin suites to be loaded in the
90    /// language server database.
91    pub extra_plugin_suites:
92        Option<&'static (dyn Fn() -> Vec<PluginSuite> + Send + Sync + RefUnwindSafe)>,
93}
94
95/// Starts the language server.
96///
97/// See [the top-level documentation][lib] documentation for usage examples.
98///
99/// [lib]: crate#running-vanilla
100pub fn start() -> ExitCode {
101    start_with_tricks(Tricks::default())
102}
103
104/// Starts the language server with customizations.
105///
106/// See [the top-level documentation][lib] documentation for usage examples.
107///
108/// [lib]: crate#running-with-customizations
109pub fn start_with_tricks(tricks: Tricks) -> ExitCode {
110    let _log_guard = init_logging();
111    set_panic_hook();
112
113    info!("language server starting");
114    env_config::report_to_logs();
115
116    let exit_code = match Backend::new(tricks) {
117        Ok(backend) => {
118            if let Err(err) = backend.run().map(|handle| handle.join()) {
119                error!("language server encountered an unrecoverable error: {err}");
120                ExitCode::from(1)
121            } else {
122                ExitCode::from(0)
123            }
124        }
125        Err(err) => {
126            error!("language server failed during initialization: {err}");
127            ExitCode::from(1)
128        }
129    };
130
131    info!("language server stopped");
132    exit_code
133}
134
135/// Special function to run the language server in end-to-end tests.
136#[cfg(feature = "testing")]
137pub fn build_service_for_e2e_tests()
138-> (Box<dyn FnOnce() -> BackendForTesting + Send>, lsp_server::Connection) {
139    BackendForTesting::new_for_testing(Default::default())
140}
141
142/// Initialize logging infrastructure for the language server.
143///
144/// Returns a guard that should be dropped when the LS ends, to flush log files.
145fn init_logging() -> Option<impl Drop> {
146    use std::fs;
147    use std::io::IsTerminal;
148
149    use tracing_chrome::ChromeLayerBuilder;
150    use tracing_subscriber::filter::{EnvFilter, LevelFilter, Targets};
151    use tracing_subscriber::fmt::Layer;
152    use tracing_subscriber::fmt::time::Uptime;
153    use tracing_subscriber::prelude::*;
154
155    let mut guard = None;
156
157    let fmt_layer = Layer::new()
158        .with_writer(io::stderr)
159        .with_timer(Uptime::default())
160        .with_ansi(io::stderr().is_terminal())
161        .with_filter(
162            EnvFilter::builder()
163                .with_default_directive(LevelFilter::WARN.into())
164                .with_env_var(env_config::CAIRO_LS_LOG)
165                .from_env_lossy(),
166        );
167
168    let profile_layer = if env_config::tracing_profile() {
169        let mut path = PathBuf::from(format!(
170            "./cairols-profile-{}.json",
171            SystemTime::UNIX_EPOCH.elapsed().unwrap().as_micros()
172        ));
173
174        // Create the file now, so that we early panic, and `fs::canonicalize` will work.
175        let profile_file = fs::File::create(&path).expect("Failed to create profile file.");
176
177        // Try to canonicalize the path, so that it's easier to find the file from logs.
178        if let Ok(canonical) = fs::canonicalize(&path) {
179            path = canonical;
180        }
181
182        eprintln!("this LS run will output tracing profile to: {}", path.display());
183        eprintln!(
184            "open that file with https://ui.perfetto.dev (or chrome://tracing) to analyze it"
185        );
186
187        let (profile_layer, profile_layer_guard) =
188            ChromeLayerBuilder::new().writer(profile_file).include_args(true).build();
189
190        // Filter out less important Salsa logs because they are too verbose,
191        // and with them the profile file quickly grows to several GBs of data.
192        let profile_layer = profile_layer.with_filter(
193            Targets::new().with_default(LevelFilter::TRACE).with_target("salsa", LevelFilter::WARN),
194        );
195
196        guard = Some(profile_layer_guard);
197        Some(profile_layer)
198    } else {
199        None
200    };
201
202    tracing::subscriber::set_global_default(
203        tracing_subscriber::registry().with(fmt_layer).with(profile_layer),
204    )
205    .expect("Could not set up global logger.");
206
207    guard
208}
209
210/// Sets a special panic hook that skips execution for Salsa cancellation panics.
211fn set_panic_hook() {
212    let previous_hook = panic::take_hook();
213    panic::set_hook(Box::new(move |info| {
214        if !is_cancelled(info.payload()) {
215            previous_hook(info);
216        }
217    }))
218}
219
220struct Backend {
221    connection: Connection,
222    state: State,
223}
224
225#[cfg(feature = "testing")]
226pub struct BackendForTesting(Backend);
227
228#[cfg(feature = "testing")]
229impl BackendForTesting {
230    fn new_for_testing(
231        tricks: Tricks,
232    ) -> (Box<dyn FnOnce() -> BackendForTesting + Send>, lsp_server::Connection) {
233        let (connection_initializer, client) = ConnectionInitializer::memory();
234
235        let init = Box::new(|| {
236            BackendForTesting(Backend::initialize(tricks, connection_initializer).unwrap())
237        });
238
239        (init, client)
240    }
241
242    pub fn run_for_tests(self) -> Result<JoinHandle<Result<()>>> {
243        self.0.run()
244    }
245}
246
247impl Backend {
248    fn new(tricks: Tricks) -> Result<Self> {
249        let connection_initializer = ConnectionInitializer::stdio();
250
251        Self::initialize(tricks, connection_initializer)
252    }
253
254    /// Initializes the connection and crate a ready to run [`Backend`] instance.
255    ///
256    /// As part of the initialization flow, this function exchanges client and server capabilities.
257    fn initialize(tricks: Tricks, connection_initializer: ConnectionInitializer) -> Result<Self> {
258        let (id, init_params) = connection_initializer.initialize_start()?;
259
260        let client_capabilities = init_params.capabilities;
261        let server_capabilities = collect_server_capabilities(&client_capabilities);
262
263        let connection = connection_initializer.initialize_finish(id, server_capabilities)?;
264        let state = State::new(connection.make_sender(), client_capabilities, tricks);
265
266        Ok(Self { connection, state })
267    }
268
269    /// Runs the main event loop thread and wait for its completion.
270    fn run(self) -> Result<JoinHandle<Result<()>>> {
271        event_loop_thread(move || {
272            let Self { mut state, connection } = self;
273            let proc_macro_channels = state.proc_macro_controller.init_channels();
274
275            let project_updates_receiver = state.project_controller.init_channel();
276            let mut scheduler = Scheduler::new(&mut state, connection.make_sender());
277
278            Self::dispatch_setup_tasks(&mut scheduler);
279
280            // Attempt to swap the database to reduce memory use.
281            // Because diagnostics are always refreshed afterwards, the fresh database state will
282            // be quickly repopulated.
283            scheduler.on_sync_task(Self::maybe_swap_database);
284
285            // Refresh diagnostics each time state changes.
286            // Although it is possible to mutate state without affecting the analysis database,
287            // we basically never hit such a case in CairoLS in happy paths.
288            scheduler.on_sync_task(Self::refresh_diagnostics);
289
290            let result = Self::event_loop(
291                &connection,
292                proc_macro_channels,
293                project_updates_receiver,
294                scheduler,
295            );
296
297            // Trigger cancellation in any background tasks that might still be running.
298            state.db.salsa_runtime_mut().synthetic_write(Durability::LOW);
299
300            if let Err(err) = connection.close() {
301                error!("failed to close connection to the language server: {err:?}");
302            }
303
304            result
305        })
306    }
307
308    /// Runs various setup tasks before entering the main event loop.
309    fn dispatch_setup_tasks(scheduler: &mut Scheduler<'_>) {
310        scheduler.local(Self::register_dynamic_capabilities);
311
312        scheduler.local(|state, _notifier, requester, _responder| {
313            let _ = state.config.reload(requester, &state.client_capabilities);
314        });
315    }
316
317    fn register_dynamic_capabilities(
318        state: &mut State,
319        _notifier: Notifier,
320        requester: &mut Requester<'_>,
321        _responder: Responder,
322    ) {
323        let registrations = collect_dynamic_registrations(&state.client_capabilities);
324
325        let _ = requester
326            .request::<lsp_types::request::RegisterCapability>(
327                RegistrationParams { registrations },
328                |()| {
329                    debug!("configuration file watcher successfully registered");
330                    Task::nothing()
331                },
332            )
333            .inspect_err(|e| {
334                error!(
335                    "failed to register dynamic capabilities, some features may not work \
336                     properly: {e:?}"
337                )
338            });
339    }
340
341    // +--------------------------------------------------+
342    // | Function code adopted from:                      |
343    // | Repository: https://github.com/astral-sh/ruff    |
344    // | File: `crates/ruff_server/src/server.rs`         |
345    // | Commit: 46a457318d8d259376a2b458b3f814b9b795fe69 |
346    // +--------------------------------------------------+
347    fn event_loop(
348        connection: &Connection,
349        proc_macro_channels: ProcMacroChannelsReceivers,
350        project_updates_receiver: Receiver<ProjectUpdate>,
351        mut scheduler: Scheduler<'_>,
352    ) -> Result<()> {
353        let incoming = connection.incoming();
354
355        loop {
356            select_biased! {
357                // Project updates may significantly change the state, therefore
358                // they should be handled first in case of multiple operations being ready at once.
359                // To ensure it, keep project updates channel in the first arm of `select_biased!`.
360                recv(project_updates_receiver) -> project_update => {
361                    let Ok(project_update) = project_update else { break };
362
363                    scheduler.local(move |state, notifier, _, _| ProjectController::handle_update(state, notifier, project_update));
364                }
365                recv(incoming) -> msg => {
366                    let Ok(msg) = msg else { break };
367
368                    if connection.handle_shutdown(&msg)? {
369                        break;
370                    }
371                    let task = match msg {
372                        Message::Request(req) => server::request(req),
373                        Message::Notification(notification) => server::notification(notification),
374                        Message::Response(response) => scheduler.response(response),
375                    };
376                    scheduler.dispatch(task);
377                }
378                recv(proc_macro_channels.response) -> response => {
379                    let Ok(()) = response else { break };
380
381                    scheduler.local(Self::on_proc_macro_response);
382                }
383                recv(proc_macro_channels.error) -> error => {
384                    let Ok(()) = error else { break };
385
386                    scheduler.local(Self::on_proc_macro_error);
387                }
388            }
389        }
390
391        Ok(())
392    }
393
394    /// Calls [`lang::proc_macros::controller::ProcMacroClientController::handle_error`] to do its
395    /// work.
396    fn on_proc_macro_error(state: &mut State, _: Notifier, _: &mut Requester<'_>, _: Responder) {
397        state.proc_macro_controller.handle_error(&mut state.db, &state.config);
398    }
399
400    /// Calls [`lang::proc_macros::controller::ProcMacroClientController::on_response`] to do its
401    /// work.
402    fn on_proc_macro_response(state: &mut State, _: Notifier, _: &mut Requester<'_>, _: Responder) {
403        state.proc_macro_controller.on_response(&mut state.db, &state.config);
404    }
405
406    /// Calls [`lang::db::AnalysisDatabaseSwapper::maybe_swap`] to do its work.
407    fn maybe_swap_database(state: &mut State, _notifier: Notifier) {
408        state.db_swapper.maybe_swap(
409            &mut state.db,
410            &state.open_files,
411            &state.tricks,
412            &mut state.project_controller,
413        );
414    }
415
416    /// Calls [`lang::diagnostics::DiagnosticsController::refresh`] to do its work.
417    fn refresh_diagnostics(state: &mut State, _notifier: Notifier) {
418        state.diagnostics_controller.refresh(state);
419    }
420
421    /// Reload config and update project model for all open files.
422    fn reload(state: &mut State, requester: &mut Requester<'_>) -> LSPResult<()> {
423        state.project_controller.clear_loaded_workspaces();
424        state.config.reload(requester, &state.client_capabilities)?;
425
426        for uri in state.open_files.iter() {
427            let Some(file_id) = state.db.file_for_url(uri) else { continue };
428            if let FileLongId::OnDisk(file_path) = state.db.lookup_intern_file(file_id) {
429                state.project_controller.update_project_for_file(&file_path);
430            }
431        }
432
433        Ok(())
434    }
435}