From b21d68cd2ab828faee4f49c43bb37fe444561def Mon Sep 17 00:00:00 2001 From: ClSlaid Date: Mon, 4 Dec 2023 21:08:29 +0800 Subject: [PATCH 1/3] feat: add proc-macro rebuild on save option Signed-off-by: ClSlaid --- crates/rust-analyzer/src/config.rs | 7 +++++++ crates/rust-analyzer/src/global_state.rs | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 90d1d6b055..33ba466236 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -105,6 +105,9 @@ config_data! { /// ``` /// . cargo_buildScripts_overrideCommand: Option> = "null", + /// rerun proc-macros building/build-scripts running when proc-macro + /// or build-script sources change and are saved. + cargo_buildScripts_rebuildOnSave: bool = "false", /// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to /// avoid checking unnecessary things. cargo_buildScripts_useRustcWrapper: bool = "true", @@ -1368,6 +1371,10 @@ impl Config { self.data.checkOnSave } + pub fn script_rebuild_on_save(&self) -> bool { + self.data.cargo_buildScripts_rebuildOnSave + } + pub fn runnables(&self) -> RunnablesConfig { RunnablesConfig { override_cargo: self.data.runnables_command.clone(), diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index c09f57252c..60dc5dc522 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -351,6 +351,10 @@ impl GlobalState { crates.iter().any(|&krate| crate_graph[krate].is_proc_macro) }); + if self.proc_macro_changed && self.config.script_rebuild_on_save() { + self.fetch_build_data_queue + .request_op(format!("proc-macro or build script source changed"), ()) + } } true From ad27b6e05372a2d68c4abe3718ca85396a456f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=95=A5?= Date: Fri, 8 Dec 2023 11:31:56 +0800 Subject: [PATCH 2/3] patch: only rerun build scripts when saved. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蔡略 --- crates/rust-analyzer/src/config.rs | 2 +- crates/rust-analyzer/src/global_state.rs | 4 ---- crates/rust-analyzer/src/handlers/notification.rs | 7 +++++++ docs/user/generated_config.adoc | 6 ++++++ editors/code/package.json | 5 +++++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 33ba466236..8700db5d8f 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -105,7 +105,7 @@ config_data! { /// ``` /// . cargo_buildScripts_overrideCommand: Option> = "null", - /// rerun proc-macros building/build-scripts running when proc-macro + /// Rerun proc-macros building/build-scripts running when proc-macro /// or build-script sources change and are saved. cargo_buildScripts_rebuildOnSave: bool = "false", /// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 60dc5dc522..c09f57252c 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -351,10 +351,6 @@ impl GlobalState { crates.iter().any(|&krate| crate_graph[krate].is_proc_macro) }); - if self.proc_macro_changed && self.config.script_rebuild_on_save() { - self.fetch_build_data_queue - .request_op(format!("proc-macro or build script source changed"), ()) - } } true diff --git a/crates/rust-analyzer/src/handlers/notification.rs b/crates/rust-analyzer/src/handlers/notification.rs index f9070d2735..60a3edb9dd 100644 --- a/crates/rust-analyzer/src/handlers/notification.rs +++ b/crates/rust-analyzer/src/handlers/notification.rs @@ -130,6 +130,13 @@ pub(crate) fn handle_did_save_text_document( state: &mut GlobalState, params: DidSaveTextDocumentParams, ) -> anyhow::Result<()> { + if state.config.script_rebuild_on_save() && state.proc_macro_changed { + // reset the flag + state.proc_macro_changed = false; + // rebuild the proc macros + state.fetch_build_data_queue.request_op(format!("ScriptRebuildOnSave"), ()); + } + if let Ok(vfs_path) = from_proto::vfs_path(¶ms.text_document.uri) { // Re-fetch workspaces if a workspace related file has changed if let Some(abs_path) = vfs_path.as_path() { diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 8a2d080844..93663c5206 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -71,6 +71,12 @@ cargo check --quiet --workspace --message-format=json --all-targets ``` . -- +[[rust-analyzer.cargo.buildScripts.rebuildOnSave]]rust-analyzer.cargo.buildScripts.rebuildOnSave (default: `false`):: ++ +-- +Rerun proc-macros building/build-scripts running when proc-macro +or build-script sources change and are saved. +-- [[rust-analyzer.cargo.buildScripts.useRustcWrapper]]rust-analyzer.cargo.buildScripts.useRustcWrapper (default: `true`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index cfaf421327..2f5eeba41a 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -586,6 +586,11 @@ "type": "string" } }, + "rust-analyzer.cargo.buildScripts.rebuildOnSave": { + "markdownDescription": "Rerun proc-macros building/build-scripts running when proc-macro\nor build-script sources change and are saved.", + "default": false, + "type": "boolean" + }, "rust-analyzer.cargo.buildScripts.useRustcWrapper": { "markdownDescription": "Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to\navoid checking unnecessary things.", "default": true, From c99089c2b39e3e7168a4bd0e68c0f3aeef4f26de Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 2 Jan 2024 10:27:41 +0100 Subject: [PATCH 3/3] Update crates/rust-analyzer/src/handlers/notification.rs --- crates/rust-analyzer/src/handlers/notification.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/handlers/notification.rs b/crates/rust-analyzer/src/handlers/notification.rs index 60a3edb9dd..7e6219991b 100644 --- a/crates/rust-analyzer/src/handlers/notification.rs +++ b/crates/rust-analyzer/src/handlers/notification.rs @@ -134,7 +134,7 @@ pub(crate) fn handle_did_save_text_document( // reset the flag state.proc_macro_changed = false; // rebuild the proc macros - state.fetch_build_data_queue.request_op(format!("ScriptRebuildOnSave"), ()); + state.fetch_build_data_queue.request_op("ScriptRebuildOnSave".to_owned(), ()); } if let Ok(vfs_path) = from_proto::vfs_path(¶ms.text_document.uri) {