1 //! Workspace information we get from cargo consists of two pieces. The first is
2 //! the output of `cargo metadata`. The second is the output of running
3 //! `build.rs` files (`OUT_DIR` env var, extra cfg flags) and compiling proc
6 //! This module implements this second part. We use "build script" terminology
7 //! here, but it covers procedural macros as well.
9 use std::{cell::RefCell, io, path::PathBuf, process::Command};
11 use cargo_metadata::{camino::Utf8Path, Message};
12 use la_arena::ArenaMap;
13 use paths::AbsPathBuf;
14 use rustc_hash::FxHashMap;
16 use serde::Deserialize;
18 use crate::{cfg_flag::CfgFlag, CargoConfig, CargoWorkspace, Package};
20 #[derive(Debug, Default, Clone, PartialEq, Eq)]
21 pub struct WorkspaceBuildScripts {
22 outputs: ArenaMap<Package, Option<BuildScriptOutput>>,
23 error: Option<String>,
26 #[derive(Debug, Clone, Default, PartialEq, Eq)]
27 pub(crate) struct BuildScriptOutput {
28 /// List of config flags defined by this package's build script.
29 pub(crate) cfgs: Vec<CfgFlag>,
30 /// List of cargo-related environment variables with their value.
32 /// If the package has a build script which defines environment variables,
33 /// they can also be found here.
34 pub(crate) envs: Vec<(String, String)>,
35 /// Directory where a build script might place its output.
36 pub(crate) out_dir: Option<AbsPathBuf>,
37 /// Path to the proc-macro library file if this package exposes proc-macros.
38 pub(crate) proc_macro_dylib_path: Option<AbsPathBuf>,
41 impl WorkspaceBuildScripts {
42 fn build_command(config: &CargoConfig) -> Command {
43 if let Some([program, args @ ..]) = config.run_build_script_command.as_deref() {
44 let mut cmd = Command::new(program);
49 let mut cmd = Command::new(toolchain::cargo());
51 cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
53 // --all-targets includes tests, benches and examples in addition to the
54 // default lib and bins. This is an independent concept from the --targets
56 cmd.arg("--all-targets");
58 if let Some(target) = &config.target {
59 cmd.args(&["--target", target]);
62 if config.all_features {
63 cmd.arg("--all-features");
65 if config.no_default_features {
66 cmd.arg("--no-default-features");
68 if !config.features.is_empty() {
69 cmd.arg("--features");
70 cmd.arg(config.features.join(" "));
79 workspace: &CargoWorkspace,
80 progress: &dyn Fn(String),
81 toolchain: &Option<Version>,
82 ) -> io::Result<WorkspaceBuildScripts> {
83 const RUST_1_62: Version = Version::new(1, 62, 0);
85 match Self::run_(Self::build_command(config), config, workspace, progress) {
86 Ok(WorkspaceBuildScripts { error: Some(error), .. })
87 if toolchain.as_ref().map_or(false, |it| *it >= RUST_1_62) =>
89 // building build scripts failed, attempt to build with --keep-going so
90 // that we potentially get more build data
91 let mut cmd = Self::build_command(config);
92 cmd.args(&["-Z", "unstable-options", "--keep-going"]).env("RUSTC_BOOTSTRAP", "1");
93 let mut res = Self::run_(cmd, config, workspace, progress)?;
94 res.error = Some(error);
103 config: &CargoConfig,
104 workspace: &CargoWorkspace,
105 progress: &dyn Fn(String),
106 ) -> io::Result<WorkspaceBuildScripts> {
107 if config.wrap_rustc_in_build_scripts {
108 // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
109 // that to compile only proc macros and build scripts during the initial
111 let myself = std::env::current_exe()?;
112 cmd.env("RUSTC_WRAPPER", myself);
113 cmd.env("RA_RUSTC_WRAPPER", "1");
116 cmd.current_dir(workspace.workspace_root());
118 let mut res = WorkspaceBuildScripts::default();
119 let outputs = &mut res.outputs;
120 // NB: Cargo.toml could have been modified between `cargo metadata` and
121 // `cargo check`. We shouldn't assume that package ids we see here are
122 // exactly those from `config`.
123 let mut by_id: FxHashMap<String, Package> = FxHashMap::default();
124 for package in workspace.packages() {
125 outputs.insert(package, None);
126 by_id.insert(workspace[package].id.clone(), package);
129 let errors = RefCell::new(String::new());
130 let push_err = |err: &str| {
131 let mut e = errors.borrow_mut();
136 tracing::info!("Running build scripts: {:?}", cmd);
137 let output = stdx::process::spawn_with_streaming_output(
140 // Copy-pasted from existing cargo_metadata. It seems like we
141 // should be using serde_stacker here?
142 let mut deserializer = serde_json::Deserializer::from_str(line);
143 deserializer.disable_recursion_limit();
144 let message = Message::deserialize(&mut deserializer)
145 .unwrap_or_else(|_| Message::TextLine(line.to_string()));
148 Message::BuildScriptExecuted(message) => {
149 let package = match by_id.get(&message.package_id.repr) {
154 let mut acc = Vec::new();
155 for cfg in message.cfgs {
156 match cfg.parse::<CfgFlag>() {
157 Ok(it) => acc.push(it),
160 "invalid cfg from cargo-metadata: {}",
169 // cargo_metadata crate returns default (empty) path for
170 // older cargos, which is not absolute, so work around that.
171 let out_dir = message.out_dir.into_os_string();
172 if !out_dir.is_empty() {
173 let data = outputs[package].get_or_insert_with(Default::default);
174 data.out_dir = Some(AbsPathBuf::assert(PathBuf::from(out_dir)));
177 if !message.env.is_empty() {
178 outputs[package].get_or_insert_with(Default::default).envs =
182 Message::CompilerArtifact(message) => {
183 let package = match by_id.get(&message.package_id.repr) {
188 progress(format!("metadata {}", message.target.name));
190 if message.target.kind.iter().any(|k| k == "proc-macro") {
192 if let Some(filename) =
193 message.filenames.iter().find(|name| is_dylib(name))
195 let filename = AbsPathBuf::assert(PathBuf::from(&filename));
197 .get_or_insert_with(Default::default)
198 .proc_macro_dylib_path = Some(filename);
202 Message::CompilerMessage(message) => {
203 progress(message.target.name);
205 if let Some(diag) = message.message.rendered.as_deref() {
209 Message::BuildFinished(_) => {}
210 Message::TextLine(_) => {}
219 for package in workspace.packages() {
220 if let Some(package_build_data) = &mut outputs[package] {
223 workspace[package].manifest.parent().display(),
226 // inject_cargo_env(package, package_build_data);
227 if let Some(out_dir) = &package_build_data.out_dir {
228 // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
229 if let Some(out_dir) = out_dir.as_os_str().to_str().map(|s| s.to_owned()) {
230 package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
236 let mut errors = errors.into_inner();
237 if !output.status.success() {
238 if errors.is_empty() {
239 errors = "cargo check failed".to_string();
241 res.error = Some(errors);
247 pub fn error(&self) -> Option<&str> {
248 self.error.as_deref()
251 pub(crate) fn get_output(&self, idx: Package) -> Option<&BuildScriptOutput> {
252 self.outputs.get(idx)?.as_ref()
256 // FIXME: File a better way to know if it is a dylib.
257 fn is_dylib(path: &Utf8Path) -> bool {
258 match path.extension().map(|e| e.to_string().to_lowercase()) {
260 Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),