Do not append `--compile-time-deps` to overwritten build script commands
diff --git a/crates/proc-macro-srv/proc-macro-test/build.rs b/crates/proc-macro-srv/proc-macro-test/build.rs
index b97569d..b9e84a4 100644
--- a/crates/proc-macro-srv/proc-macro-test/build.rs
+++ b/crates/proc-macro-srv/proc-macro-test/build.rs
@@ -109,13 +109,11 @@
 
     let mut artifact_path = None;
     for message in Message::parse_stream(output.stdout.as_slice()) {
-        if let Message::CompilerArtifact(artifact) = message.unwrap() {
-            if artifact.target.kind.contains(&cargo_metadata::TargetKind::ProcMacro)
-                && (artifact.package_id.repr.starts_with(&repr)
-                    || artifact.package_id.repr == pkgid)
-            {
-                artifact_path = Some(PathBuf::from(&artifact.filenames[0]));
-            }
+        if let Message::CompilerArtifact(artifact) = message.unwrap()
+            && artifact.target.kind.contains(&cargo_metadata::TargetKind::ProcMacro)
+            && (artifact.package_id.repr.starts_with(&repr) || artifact.package_id.repr == pkgid)
+        {
+            artifact_path = Some(PathBuf::from(&artifact.filenames[0]));
         }
     }
 
diff --git a/crates/project-model/src/build_dependencies.rs b/crates/project-model/src/build_dependencies.rs
index 31a0d82..bbaa8f4 100644
--- a/crates/project-model/src/build_dependencies.rs
+++ b/crates/project-model/src/build_dependencies.rs
@@ -20,9 +20,7 @@
 
 use crate::{
     CargoConfig, CargoFeatures, CargoWorkspace, InvocationStrategy, ManifestPath, Package, Sysroot,
-    TargetKind,
-    toolchain_info::{QueryConfig, version},
-    utf8_stdout,
+    TargetKind, utf8_stdout,
 };
 
 /// Output of the build script and proc-macro building steps for a workspace.
@@ -64,6 +62,7 @@
         workspace: &CargoWorkspace,
         progress: &dyn Fn(String),
         sysroot: &Sysroot,
+        toolchain: Option<&semver::Version>,
     ) -> io::Result<WorkspaceBuildScripts> {
         let current_dir = workspace.workspace_root();
 
@@ -74,6 +73,7 @@
             workspace.manifest_path(),
             current_dir,
             sysroot,
+            toolchain,
         )?;
         Self::run_per_ws(cmd, workspace, progress)
     }
@@ -95,6 +95,7 @@
             &ManifestPath::try_from(working_directory.clone()).unwrap(),
             working_directory,
             &Sysroot::empty(),
+            None,
         )?;
         // NB: Cargo.toml could have been modified between `cargo metadata` and
         // `cargo check`. We shouldn't assume that package ids we see here are
@@ -387,12 +388,13 @@
         manifest_path: &ManifestPath,
         current_dir: &AbsPath,
         sysroot: &Sysroot,
+        toolchain: Option<&semver::Version>,
     ) -> io::Result<Command> {
-        let mut cmd = match config.run_build_script_command.as_deref() {
+        match config.run_build_script_command.as_deref() {
             Some([program, args @ ..]) => {
                 let mut cmd = toolchain::command(program, current_dir, &config.extra_env);
                 cmd.args(args);
-                cmd
+                Ok(cmd)
             }
             _ => {
                 let mut cmd = sysroot.tool(Tool::Cargo, current_dir, &config.extra_env);
@@ -444,40 +446,35 @@
 
                 cmd.arg("--keep-going");
 
-                cmd
+                // If [`--compile-time-deps` flag](https://github.com/rust-lang/cargo/issues/14434) is
+                // available in current toolchain's cargo, use it to build compile time deps only.
+                const COMP_TIME_DEPS_MIN_TOOLCHAIN_VERSION: semver::Version = semver::Version {
+                    major: 1,
+                    minor: 89,
+                    patch: 0,
+                    pre: semver::Prerelease::EMPTY,
+                    build: semver::BuildMetadata::EMPTY,
+                };
+
+                let cargo_comp_time_deps_available =
+                    toolchain.is_some_and(|v| *v >= COMP_TIME_DEPS_MIN_TOOLCHAIN_VERSION);
+
+                if cargo_comp_time_deps_available {
+                    cmd.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
+                    cmd.arg("-Zunstable-options");
+                    cmd.arg("--compile-time-deps");
+                } else if config.wrap_rustc_in_build_scripts {
+                    // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
+                    // that to compile only proc macros and build scripts during the initial
+                    // `cargo check`.
+                    // We don't need this if we are using `--compile-time-deps` flag.
+                    let myself = std::env::current_exe()?;
+                    cmd.env("RUSTC_WRAPPER", myself);
+                    cmd.env("RA_RUSTC_WRAPPER", "1");
+                }
+                Ok(cmd)
             }
-        };
-
-        // If [`--compile-time-deps` flag](https://github.com/rust-lang/cargo/issues/14434) is
-        // available in current toolchain's cargo, use it to build compile time deps only.
-        const COMP_TIME_DEPS_MIN_TOOLCHAIN_VERSION: semver::Version = semver::Version {
-            major: 1,
-            minor: 89,
-            patch: 0,
-            pre: semver::Prerelease::EMPTY,
-            build: semver::BuildMetadata::EMPTY,
-        };
-
-        let query_config = QueryConfig::Cargo(sysroot, manifest_path);
-        let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
-        let cargo_comp_time_deps_available =
-            toolchain.is_some_and(|v| v >= COMP_TIME_DEPS_MIN_TOOLCHAIN_VERSION);
-
-        if cargo_comp_time_deps_available {
-            cmd.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
-            cmd.arg("-Zunstable-options");
-            cmd.arg("--compile-time-deps");
-        } else if config.wrap_rustc_in_build_scripts {
-            // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
-            // that to compile only proc macros and build scripts during the initial
-            // `cargo check`.
-            // We don't need this if we are using `--compile-time-deps` flag.
-            let myself = std::env::current_exe()?;
-            cmd.env("RUSTC_WRAPPER", myself);
-            cmd.env("RA_RUSTC_WRAPPER", "1");
         }
-
-        Ok(cmd)
     }
 }
 
diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs
index a589bc0..5bc64df 100644
--- a/crates/project-model/src/workspace.rs
+++ b/crates/project-model/src/workspace.rs
@@ -636,10 +636,16 @@
         match &self.kind {
             ProjectWorkspaceKind::DetachedFile { cargo: Some((cargo, _, None)), .. }
             | ProjectWorkspaceKind::Cargo { cargo, error: None, .. } => {
-                WorkspaceBuildScripts::run_for_workspace(config, cargo, progress, &self.sysroot)
-                    .with_context(|| {
-                        format!("Failed to run build scripts for {}", cargo.workspace_root())
-                    })
+                WorkspaceBuildScripts::run_for_workspace(
+                    config,
+                    cargo,
+                    progress,
+                    &self.sysroot,
+                    self.toolchain.as_ref(),
+                )
+                .with_context(|| {
+                    format!("Failed to run build scripts for {}", cargo.workspace_root())
+                })
             }
             _ => Ok(WorkspaceBuildScripts::default()),
         }