Skip to content

Do not append --compile-time-deps to overwritten build script commands #20121

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

Merged
merged 1 commit into from
Jun 29, 2025
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
12 changes: 5 additions & 7 deletions crates/proc-macro-srv/proc-macro-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ fn main() {

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]));
}
}

Expand Down
71 changes: 34 additions & 37 deletions crates/project-model/src/build_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ use toolchain::Tool;

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.
Expand Down Expand Up @@ -64,6 +62,7 @@ impl WorkspaceBuildScripts {
workspace: &CargoWorkspace,
progress: &dyn Fn(String),
sysroot: &Sysroot,
toolchain: Option<&semver::Version>,
) -> io::Result<WorkspaceBuildScripts> {
let current_dir = workspace.workspace_root();

Expand All @@ -74,6 +73,7 @@ impl WorkspaceBuildScripts {
workspace.manifest_path(),
current_dir,
sysroot,
toolchain,
)?;
Self::run_per_ws(cmd, workspace, progress)
}
Expand All @@ -95,6 +95,7 @@ impl WorkspaceBuildScripts {
&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
Expand Down Expand Up @@ -387,12 +388,13 @@ impl WorkspaceBuildScripts {
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);
Expand Down Expand Up @@ -444,40 +446,35 @@ impl WorkspaceBuildScripts {

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)
}
}

Expand Down
14 changes: 10 additions & 4 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,16 @@ impl ProjectWorkspace {
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()),
}
Expand Down