Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restart proc macro server on Scarb.toml change #9

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lang/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ impl AnalysisDatabase {
})
}

/// Removes plugin suit from database.
pub fn remove_plugin_suite(&mut self, plugins: PluginSuite) {
self.with_plugins_mut(move |macro_plugins, analyzer_plugins, inline_macro_plugins| {
remove_plugin_suite(plugins, macro_plugins, analyzer_plugins, inline_macro_plugins)
})
}

/// Adds plugin suit to database.
pub fn add_plugin_suite(&mut self, plugins: PluginSuite) {
self.with_plugins_mut(move |macro_plugins, analyzer_plugins, inline_macro_plugins| {
Expand Down
25 changes: 25 additions & 0 deletions src/lang/proc_macros/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ impl ProcMacroClientController {
}
}

/// Forcibly restarts the proc-macro-server, shutting down any currently running instances.
///
/// A new server instance is only started if there are available restart attempts left.
/// This ensures that a fresh proc-macro-server is used.
pub fn force_restart(&mut self, db: &mut AnalysisDatabase, config: &Config) {
// We have to make sure that snapshots will not report errors from previous client after we
// create new one.
db.cancel_all();

// Otherwise we can get messages from old client after initialization of new one.
self.channels.clear_all();

if let Some(plugin_suite) = self.plugin_suite.take() {
db.remove_plugin_suite(plugin_suite);
}

self.try_initialize(db, config);
}

/// Check if an error was reported. If so, try to restart.
pub fn handle_error(&mut self, db: &mut AnalysisDatabase, config: &Config) {
if !self.try_initialize(db, config) {
Expand Down Expand Up @@ -272,4 +291,10 @@ impl ProcMacroChannels {

Self { response_sender, response_receiver, error_sender, error_receiver }
}

/// Make all channels empty in a non-blocking manner.
fn clear_all(&self) {
self.error_receiver.try_iter().for_each(|_| {});
self.response_receiver.try_iter().for_each(|_| {});
}
}
2 changes: 2 additions & 0 deletions src/server/routing/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ impl SyncNotificationHandler for DidChangeWatchedFiles {
if ["Scarb.toml", "cairo_project.toml"].map(Some).contains(&changed_file_name.to_str())
{
Backend::reload(state, requester)?;

state.proc_macro_controller.force_restart(&mut state.db, &state.config);
}
}

Expand Down
Loading