1 //! See [`CargoWorkspace`].
4 use std::path::PathBuf;
5 use std::str::from_utf8;
6 use std::{ops, process::Command};
8 use anyhow::{Context, Result};
10 use cargo_metadata::{CargoOpt, MetadataCommand};
11 use la_arena::{Arena, Idx};
12 use paths::{AbsPath, AbsPathBuf};
13 use rustc_hash::FxHashMap;
14 use serde::Deserialize;
15 use serde_json::from_value;
17 use crate::CfgOverrides;
18 use crate::{utf8_stdout, ManifestPath};
20 /// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
21 /// workspace. It pretty closely mirrors `cargo metadata` output.
23 /// Note that internally, rust-analyzer uses a different structure:
24 /// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates,
25 /// while this knows about `Packages` & `Targets`: purely cargo-related
28 /// We use absolute paths here, `cargo metadata` guarantees to always produce
30 #[derive(Debug, Clone, Eq, PartialEq)]
31 pub struct CargoWorkspace {
32 packages: Arena<PackageData>,
33 targets: Arena<TargetData>,
34 workspace_root: AbsPathBuf,
37 impl ops::Index<Package> for CargoWorkspace {
38 type Output = PackageData;
39 fn index(&self, index: Package) -> &PackageData {
44 impl ops::Index<Target> for CargoWorkspace {
45 type Output = TargetData;
46 fn index(&self, index: Target) -> &TargetData {
51 /// Describes how to set the rustc source directory.
52 #[derive(Clone, Debug, PartialEq, Eq)]
53 pub enum RustcSource {
54 /// Explicit path for the rustc source directory.
56 /// Try to automatically detect where the rustc source directory is.
60 /// Crates to disable `#[cfg(test)]` on.
61 #[derive(Clone, Debug, PartialEq, Eq)]
62 pub enum UnsetTestCrates {
68 impl Default for UnsetTestCrates {
69 fn default() -> Self {
74 #[derive(Default, Clone, Debug, PartialEq, Eq)]
75 pub struct CargoConfig {
76 /// Do not activate the `default` feature.
77 pub no_default_features: bool,
79 /// Activate all available features
80 pub all_features: bool,
82 /// List of features to activate.
83 /// This will be ignored if `cargo_all_features` is true.
84 pub features: Vec<String>,
87 pub target: Option<String>,
89 /// Don't load sysroot crates (`std`, `core` & friends). Might be useful
90 /// when debugging isolated issues.
93 /// rustc private crate source
94 pub rustc_source: Option<RustcSource>,
96 /// crates to disable `#[cfg(test)]` on
97 pub unset_test_crates: UnsetTestCrates,
99 pub wrap_rustc_in_build_scripts: bool,
101 pub run_build_script_command: Option<Vec<String>>,
103 pub extra_env: FxHashMap<String, String>,
107 pub fn cfg_overrides(&self) -> CfgOverrides {
108 match &self.unset_test_crates {
109 UnsetTestCrates::None => CfgOverrides::Selective(iter::empty().collect()),
110 UnsetTestCrates::Only(unset_test_crates) => CfgOverrides::Selective(
114 .zip(iter::repeat_with(|| {
115 cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())])
120 UnsetTestCrates::All => CfgOverrides::Wildcard(
121 cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())]).unwrap(),
127 pub type Package = Idx<PackageData>;
129 pub type Target = Idx<TargetData>;
131 /// Information associated with a cargo crate
132 #[derive(Debug, Clone, Eq, PartialEq)]
133 pub struct PackageData {
134 /// Version given in the `Cargo.toml`
135 pub version: semver::Version,
136 /// Name as given in the `Cargo.toml`
138 /// Repository as given in the `Cargo.toml`
139 pub repository: Option<String>,
140 /// Path containing the `Cargo.toml`
141 pub manifest: ManifestPath,
142 /// Targets provided by the crate (lib, bin, example, test, ...)
143 pub targets: Vec<Target>,
144 /// Does this package come from the local filesystem (and is editable)?
146 // Whether this package is a member of the workspace
148 /// List of packages this package depends on
149 pub dependencies: Vec<PackageDependency>,
150 /// Rust edition for this package
151 pub edition: Edition,
152 /// Features provided by the crate, mapped to the features required by that feature.
153 pub features: FxHashMap<String, Vec<String>>,
154 /// List of features enabled on this package
155 pub active_features: Vec<String>,
156 /// String representation of package id
158 /// The contents of [package.metadata.rust-analyzer]
159 pub metadata: RustAnalyzerPackageMetaData,
162 #[derive(Deserialize, Default, Debug, Clone, Eq, PartialEq)]
163 pub struct RustAnalyzerPackageMetaData {
164 pub rustc_private: bool,
167 #[derive(Debug, Clone, Eq, PartialEq)]
168 pub struct PackageDependency {
174 #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
176 /// Available to the library, binary, and dev targets in the package (but not the build script).
178 /// Available only to test and bench targets (and the library target, when built with `cfg(test)`).
180 /// Available only to the build script target.
185 fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator<Item = Self> + '_ {
186 let mut dep_kinds = Vec::new();
188 dep_kinds.push(Self::Normal);
191 let kind = match info.kind {
192 cargo_metadata::DependencyKind::Normal => Self::Normal,
193 cargo_metadata::DependencyKind::Development => Self::Dev,
194 cargo_metadata::DependencyKind::Build => Self::Build,
195 cargo_metadata::DependencyKind::Unknown => continue,
197 dep_kinds.push(kind);
199 dep_kinds.sort_unstable();
201 dep_kinds.into_iter()
205 /// Information associated with a package's target
206 #[derive(Debug, Clone, Eq, PartialEq)]
207 pub struct TargetData {
208 /// Package that provided this target
209 pub package: Package,
210 /// Name as given in the `Cargo.toml` or generated from the file name
212 /// Path to the main source file of the target
213 pub root: AbsPathBuf,
215 pub kind: TargetKind,
216 /// Is this target a proc-macro
217 pub is_proc_macro: bool,
218 /// Required features of the target without which it won't build
219 pub required_features: Vec<String>,
222 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
223 pub enum TargetKind {
225 /// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
235 fn new(kinds: &[String]) -> TargetKind {
237 return match kind.as_str() {
238 "bin" => TargetKind::Bin,
239 "test" => TargetKind::Test,
240 "bench" => TargetKind::Bench,
241 "example" => TargetKind::Example,
242 "custom-build" => TargetKind::BuildScript,
243 "proc-macro" => TargetKind::Lib,
244 _ if kind.contains("lib") => TargetKind::Lib,
252 #[derive(Deserialize, Default)]
253 // Deserialise helper for the cargo metadata
254 struct PackageMetadata {
255 #[serde(rename = "rust-analyzer")]
256 rust_analyzer: Option<RustAnalyzerPackageMetaData>,
259 impl CargoWorkspace {
260 pub fn fetch_metadata(
261 cargo_toml: &ManifestPath,
262 current_dir: &AbsPath,
263 config: &CargoConfig,
264 progress: &dyn Fn(String),
265 ) -> Result<cargo_metadata::Metadata> {
269 .or_else(|| cargo_config_build_target(cargo_toml, config))
270 .or_else(|| rustc_discover_host_triple(cargo_toml, config));
272 let mut meta = MetadataCommand::new();
273 meta.cargo_path(toolchain::cargo());
274 meta.manifest_path(cargo_toml.to_path_buf());
275 if config.all_features {
276 meta.features(CargoOpt::AllFeatures);
278 if config.no_default_features {
279 // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
280 // https://github.com/oli-obk/cargo_metadata/issues/79
281 meta.features(CargoOpt::NoDefaultFeatures);
283 if !config.features.is_empty() {
284 meta.features(CargoOpt::SomeFeatures(config.features.clone()));
287 meta.current_dir(current_dir.as_os_str());
289 if let Some(target) = target {
290 meta.other_options(vec![String::from("--filter-platform"), target]);
293 // FIXME: Fetching metadata is a slow process, as it might require
294 // calling crates.io. We should be reporting progress here, but it's
295 // unclear whether cargo itself supports it.
296 progress("metadata".to_string());
299 command: &cargo_metadata::MetadataCommand,
300 extra_env: &FxHashMap<String, String>,
301 ) -> Result<cargo_metadata::Metadata, cargo_metadata::Error> {
302 let mut command = command.cargo_command();
303 command.envs(extra_env);
304 let output = command.output()?;
305 if !output.status.success() {
306 return Err(cargo_metadata::Error::CargoMetadata {
307 stderr: String::from_utf8(output.stderr)?,
310 let stdout = from_utf8(&output.stdout)?
312 .find(|line| line.starts_with('{'))
313 .ok_or(cargo_metadata::Error::NoJson)?;
314 cargo_metadata::MetadataCommand::parse(stdout)
317 let meta = exec_with_env(&meta, &config.extra_env)
318 .with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))?;
323 pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace {
324 let mut pkg_by_id = FxHashMap::default();
325 let mut packages = Arena::default();
326 let mut targets = Arena::default();
328 let ws_members = &meta.workspace_members;
330 meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
331 for meta_pkg in &meta.packages {
332 let cargo_metadata::Package {
342 let meta = from_value::<PackageMetadata>(metadata.clone()).unwrap_or_default();
343 let edition = match edition {
344 cargo_metadata::Edition::E2015 => Edition::Edition2015,
345 cargo_metadata::Edition::E2018 => Edition::Edition2018,
346 cargo_metadata::Edition::E2021 => Edition::Edition2021,
348 tracing::error!("Unsupported edition `{:?}`", edition);
352 // We treat packages without source as "local" packages. That includes all members of
353 // the current workspace, as well as any path dependency outside the workspace.
354 let is_local = meta_pkg.source.is_none();
355 let is_member = ws_members.contains(id);
357 let pkg = packages.alloc(PackageData {
360 version: version.clone(),
361 manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)).try_into().unwrap(),
366 repository: repository.clone(),
367 dependencies: Vec::new(),
368 features: meta_pkg.features.clone().into_iter().collect(),
369 active_features: Vec::new(),
370 metadata: meta.rust_analyzer.unwrap_or_default(),
372 let pkg_data = &mut packages[pkg];
373 pkg_by_id.insert(id, pkg);
374 for meta_tgt in &meta_pkg.targets {
375 let is_proc_macro = meta_tgt.kind.as_slice() == ["proc-macro"];
376 let tgt = targets.alloc(TargetData {
378 name: meta_tgt.name.clone(),
379 root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
380 kind: TargetKind::new(meta_tgt.kind.as_slice()),
382 required_features: meta_tgt.required_features.clone(),
384 pkg_data.targets.push(tgt);
387 let resolve = meta.resolve.expect("metadata executed with deps");
388 for mut node in resolve.nodes {
389 let source = match pkg_by_id.get(&node.id) {
391 // FIXME: replace this and a similar branch below with `.unwrap`, once
392 // https://github.com/rust-lang/cargo/issues/7841
393 // is fixed and hits stable (around 1.43-is probably?).
395 tracing::error!("Node id do not match in cargo metadata, ignoring {}", node.id);
399 node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
400 for (dep_node, kind) in node
403 .flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind)))
405 let pkg = match pkg_by_id.get(&dep_node.pkg) {
409 "Dep node id do not match in cargo metadata, ignoring {}",
415 let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind };
416 packages[source].dependencies.push(dep);
418 packages[source].active_features.extend(node.features);
422 AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
424 CargoWorkspace { packages, targets, workspace_root }
427 pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
428 self.packages.iter().map(|(id, _pkg)| id)
431 pub fn target_by_root(&self, root: &AbsPath) -> Option<Target> {
433 .filter(|&pkg| self[pkg].is_member)
434 .find_map(|pkg| self[pkg].targets.iter().find(|&&it| &self[it].root == root))
438 pub fn workspace_root(&self) -> &AbsPath {
442 pub fn package_flag(&self, package: &PackageData) -> String {
443 if self.is_unique(&*package.name) {
446 format!("{}:{}", package.name, package.version)
450 pub fn parent_manifests(&self, manifest_path: &ManifestPath) -> Option<Vec<ManifestPath>> {
451 let mut found = false;
452 let parent_manifests = self
455 if !found && &self[pkg].manifest == manifest_path {
458 self[pkg].dependencies.iter().find_map(|dep| {
459 if &self[dep.pkg].manifest == manifest_path {
460 return Some(self[pkg].manifest.clone());
465 .collect::<Vec<ManifestPath>>();
467 // some packages has this pkg as dep. return their manifests
468 if parent_manifests.len() > 0 {
469 return Some(parent_manifests);
472 // this pkg is inside this cargo workspace, fallback to workspace root
475 ManifestPath::try_from(self.workspace_root().join("Cargo.toml")).ok()?
479 // not in this workspace
483 fn is_unique(&self, name: &str) -> bool {
484 self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
488 fn rustc_discover_host_triple(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<String> {
489 let mut rustc = Command::new(toolchain::rustc());
490 rustc.envs(&config.extra_env);
491 rustc.current_dir(cargo_toml.parent()).arg("-vV");
492 tracing::debug!("Discovering host platform by {:?}", rustc);
493 match utf8_stdout(rustc) {
495 let field = "host: ";
496 let target = stdout.lines().find_map(|l| l.strip_prefix(field));
497 if let Some(target) = target {
498 Some(target.to_string())
500 // If we fail to resolve the host platform, it's not the end of the world.
501 tracing::info!("rustc -vV did not report host platform, got:\n{}", stdout);
506 tracing::warn!("Failed to discover host platform: {}", e);
512 fn cargo_config_build_target(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<String> {
513 let mut cargo_config = Command::new(toolchain::cargo());
514 cargo_config.envs(&config.extra_env);
516 .current_dir(cargo_toml.parent())
517 .args(&["-Z", "unstable-options", "config", "get", "build.target"])
518 .env("RUSTC_BOOTSTRAP", "1");
519 // if successful we receive `build.target = "target-triple"`
520 tracing::debug!("Discovering cargo config target by {:?}", cargo_config);
521 match utf8_stdout(cargo_config) {
523 .strip_prefix("build.target = \"")
524 .and_then(|stdout| stdout.strip_suffix('"'))
525 .map(ToOwned::to_owned),