rust-analyzer/crates/project_model/src/cargo_workspace.rs

454 lines
15 KiB
Rust
Raw Normal View History

2021-05-22 13:53:47 +00:00
//! See [`CargoWorkspace`].
use std::iter;
2021-03-02 12:27:29 +00:00
use std::path::PathBuf;
use std::{convert::TryInto, ops, process::Command};
2019-01-10 19:21:14 +00:00
use anyhow::{Context, Result};
2020-08-13 14:25:38 +00:00
use base_db::Edition;
2021-01-22 11:11:01 +00:00
use cargo_metadata::{CargoOpt, MetadataCommand};
2021-01-14 15:47:42 +00:00
use la_arena::{Arena, Idx};
use paths::{AbsPath, AbsPathBuf};
use rustc_hash::FxHashMap;
use serde::Deserialize;
use serde_json::from_value;
2019-01-10 19:21:14 +00:00
use crate::utf8_stdout;
use crate::CfgOverrides;
2021-05-22 13:53:47 +00:00
/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
2019-01-10 19:21:14 +00:00
/// workspace. It pretty closely mirrors `cargo metadata` output.
///
/// Note that internally, rust analyzer uses a different structure:
2019-01-10 19:21:14 +00:00
/// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates,
2019-02-11 16:18:27 +00:00
/// while this knows about `Packages` & `Targets`: purely cargo-related
2019-01-10 19:21:14 +00:00
/// concepts.
///
/// We use absolute paths here, `cargo metadata` guarantees to always produce
/// abs paths.
#[derive(Debug, Clone, Eq, PartialEq)]
2019-01-10 19:21:14 +00:00
pub struct CargoWorkspace {
2020-03-19 15:00:11 +00:00
packages: Arena<PackageData>,
targets: Arena<TargetData>,
workspace_root: AbsPathBuf,
2019-01-10 19:21:14 +00:00
}
2020-03-19 16:53:31 +00:00
impl ops::Index<Package> for CargoWorkspace {
type Output = PackageData;
fn index(&self, index: Package) -> &PackageData {
&self.packages[index]
}
}
impl ops::Index<Target> for CargoWorkspace {
type Output = TargetData;
fn index(&self, index: Target) -> &TargetData {
&self.targets[index]
}
}
/// Describes how to set the rustc source directory.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RustcSource {
/// Explicit path for the rustc source directory.
Path(AbsPathBuf),
/// Try to automatically detect where the rustc source directory is.
Discover,
}
2020-07-02 14:06:00 +00:00
#[derive(Default, Clone, Debug, PartialEq, Eq)]
2020-04-01 16:51:16 +00:00
pub struct CargoConfig {
2019-12-13 10:16:34 +00:00
/// Do not activate the `default` feature.
pub no_default_features: bool,
/// Activate all available features
pub all_features: bool,
/// List of features to activate.
/// This will be ignored if `cargo_all_features` is true.
pub features: Vec<String>,
2020-04-27 22:15:54 +00:00
/// rustc target
2020-05-05 16:01:54 +00:00
pub target: Option<String>,
2020-11-13 16:38:26 +00:00
/// Don't load sysroot crates (`std`, `core` & friends). Might be useful
/// when debugging isolated issues.
pub no_sysroot: bool,
/// rustc private crate source
pub rustc_source: Option<RustcSource>,
/// crates to disable `#[cfg(test)]` on
pub unset_test_crates: Vec<String>,
pub wrap_rustc_in_build_scripts: bool,
}
impl CargoConfig {
pub fn cfg_overrides(&self) -> CfgOverrides {
self.unset_test_crates
.iter()
.cloned()
.zip(iter::repeat_with(|| {
cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())]).unwrap()
}))
.collect()
}
2019-12-13 10:16:34 +00:00
}
2020-03-19 15:00:11 +00:00
pub type Package = Idx<PackageData>;
2019-01-10 19:21:14 +00:00
2020-03-19 15:00:11 +00:00
pub type Target = Idx<TargetData>;
2019-01-10 19:21:14 +00:00
/// Information associated with a cargo crate
#[derive(Debug, Clone, Eq, PartialEq)]
2020-03-19 16:53:31 +00:00
pub struct PackageData {
/// Version given in the `Cargo.toml`
pub version: semver::Version,
/// Name as given in the `Cargo.toml`
2020-03-19 16:53:31 +00:00
pub name: String,
/// Path containing the `Cargo.toml`
pub manifest: AbsPathBuf,
/// Targets provided by the crate (lib, bin, example, test, ...)
2020-03-19 16:53:31 +00:00
pub targets: Vec<Target>,
/// Is this package a member of the current workspace
2020-03-19 16:53:31 +00:00
pub is_member: bool,
/// List of packages this package depends on
2020-03-19 16:53:31 +00:00
pub dependencies: Vec<PackageDependency>,
/// Rust edition for this package
2020-03-19 16:53:31 +00:00
pub edition: Edition,
2021-01-21 11:12:19 +00:00
/// Features provided by the crate, mapped to the features required by that feature.
pub features: FxHashMap<String, Vec<String>>,
/// List of features enabled on this package
pub active_features: Vec<String>,
2021-01-28 15:33:02 +00:00
// String representation of package id
pub id: String,
// The contents of [package.metadata.rust-analyzer]
pub metadata: RustAnalyzerPackageMetaData,
}
#[derive(Deserialize, Default, Debug, Clone, Eq, PartialEq)]
pub struct RustAnalyzerPackageMetaData {
pub rustc_private: bool,
2019-01-10 19:21:14 +00:00
}
#[derive(Debug, Clone, Eq, PartialEq)]
2019-01-10 19:21:14 +00:00
pub struct PackageDependency {
pub pkg: Package,
2019-02-09 09:51:06 +00:00
pub name: String,
2021-05-12 12:16:51 +00:00
pub kind: DepKind,
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
2021-05-12 12:16:51 +00:00
pub enum DepKind {
/// Available to the library, binary, and dev targets in the package (but not the build script).
Normal,
/// Available only to test and bench targets (and the library target, when built with `cfg(test)`).
Dev,
/// Available only to the build script target.
Build,
}
impl DepKind {
fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator<Item = Self> + '_ {
let mut dep_kinds = Vec::new();
if list.is_empty() {
dep_kinds.push(Self::Normal);
}
2021-05-12 12:16:51 +00:00
for info in list {
let kind = match info.kind {
cargo_metadata::DependencyKind::Normal => Self::Normal,
cargo_metadata::DependencyKind::Development => Self::Dev,
cargo_metadata::DependencyKind::Build => Self::Build,
2021-05-12 12:16:51 +00:00
cargo_metadata::DependencyKind::Unknown => continue,
};
dep_kinds.push(kind);
2021-05-12 12:16:51 +00:00
}
dep_kinds.sort_unstable();
dep_kinds.dedup();
dep_kinds.into_iter()
2021-05-12 12:16:51 +00:00
}
2019-01-10 19:21:14 +00:00
}
/// Information associated with a package's target
#[derive(Debug, Clone, Eq, PartialEq)]
2020-03-19 16:53:31 +00:00
pub struct TargetData {
/// Package that provided this target
2020-03-19 16:53:31 +00:00
pub package: Package,
/// Name as given in the `Cargo.toml` or generated from the file name
2020-03-19 16:53:31 +00:00
pub name: String,
/// Path to the main source file of the target
pub root: AbsPathBuf,
/// Kind of target
2020-03-19 16:53:31 +00:00
pub kind: TargetKind,
/// Is this target a proc-macro
2020-03-19 16:53:31 +00:00
pub is_proc_macro: bool,
2019-01-10 19:21:14 +00:00
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetKind {
Bin,
2019-11-24 10:33:12 +00:00
/// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
2019-01-10 19:21:14 +00:00
Lib,
Example,
Test,
Bench,
2021-05-12 12:16:51 +00:00
BuildScript,
2019-01-10 19:21:14 +00:00
Other,
}
impl TargetKind {
fn new(kinds: &[String]) -> TargetKind {
for kind in kinds {
return match kind.as_str() {
"bin" => TargetKind::Bin,
"test" => TargetKind::Test,
"bench" => TargetKind::Bench,
"example" => TargetKind::Example,
2021-05-12 12:16:51 +00:00
"custom-build" => TargetKind::BuildScript,
2019-11-24 10:33:12 +00:00
"proc-macro" => TargetKind::Lib,
2019-01-10 19:21:14 +00:00
_ if kind.contains("lib") => TargetKind::Lib,
_ => continue,
};
}
TargetKind::Other
}
}
2020-03-19 16:53:31 +00:00
impl PackageData {
pub fn root(&self) -> &AbsPath {
2020-03-19 16:53:31 +00:00
self.manifest.parent().unwrap()
2019-11-24 10:33:12 +00:00
}
2019-01-10 19:21:14 +00:00
}
#[derive(Deserialize, Default)]
// Deserialise helper for the cargo metadata
struct PackageMetadata {
#[serde(rename = "rust-analyzer")]
rust_analyzer: Option<RustAnalyzerPackageMetaData>,
}
2019-01-10 19:21:14 +00:00
impl CargoWorkspace {
pub fn fetch_metadata(
2020-07-05 09:15:35 +00:00
cargo_toml: &AbsPath,
2020-11-13 16:38:26 +00:00
config: &CargoConfig,
progress: &dyn Fn(String),
) -> Result<cargo_metadata::Metadata> {
let mut meta = MetadataCommand::new();
2020-08-12 14:52:28 +00:00
meta.cargo_path(toolchain::cargo());
2020-07-05 09:15:35 +00:00
meta.manifest_path(cargo_toml.to_path_buf());
2020-11-13 16:38:26 +00:00
if config.all_features {
2019-12-13 10:16:34 +00:00
meta.features(CargoOpt::AllFeatures);
} else {
2020-11-13 16:38:26 +00:00
if config.no_default_features {
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
// https://github.com/oli-obk/cargo_metadata/issues/79
meta.features(CargoOpt::NoDefaultFeatures);
}
2020-11-13 16:38:26 +00:00
if !config.features.is_empty() {
meta.features(CargoOpt::SomeFeatures(config.features.clone()));
}
2019-12-13 10:16:34 +00:00
}
if let Some(parent) = cargo_toml.parent() {
2020-07-05 09:15:35 +00:00
meta.current_dir(parent.to_path_buf());
}
let target = if let Some(target) = &config.target {
Some(target.clone())
} else if let stdout @ Some(_) = cargo_config_build_target(cargo_toml) {
stdout
} else {
rustc_discover_host_triple(cargo_toml)
};
if let Some(target) = target {
meta.other_options(vec![String::from("--filter-platform"), target]);
}
// FIXME: Fetching metadata is a slow process, as it might require
// calling crates.io. We should be reporting progress here, but it's
// unclear whether cargo itself supports it.
progress("metadata".to_string());
let meta = meta.exec().with_context(|| {
let cwd: Option<AbsPathBuf> =
std::env::current_dir().ok().and_then(|p| p.try_into().ok());
let workdir = cargo_toml
.parent()
.map(|p| p.to_path_buf())
.or(cwd)
2021-07-17 13:43:33 +00:00
.map(|dir| format!(" in `{}`", dir.display()))
.unwrap_or_default();
format!(
2021-07-17 13:43:33 +00:00
"Failed to run `cargo metadata --manifest-path {}`{}",
cargo_toml.display(),
workdir
)
})?;
Ok(meta)
}
pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace {
2019-01-10 19:21:14 +00:00
let mut pkg_by_id = FxHashMap::default();
let mut packages = Arena::default();
let mut targets = Arena::default();
let ws_members = &meta.workspace_members;
meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
2021-01-28 15:33:02 +00:00
for meta_pkg in &meta.packages {
let cargo_metadata::Package {
id, edition, name, manifest_path, version, metadata, ..
} = meta_pkg;
let meta = from_value::<PackageMetadata>(metadata.clone()).unwrap_or_default();
2021-06-13 03:54:16 +00:00
let is_member = ws_members.contains(id);
let edition = edition.parse::<Edition>().unwrap_or_else(|err| {
log::error!("Failed to parse edition {}", err);
Edition::CURRENT
});
2021-03-02 12:27:29 +00:00
2019-01-10 19:21:14 +00:00
let pkg = packages.alloc(PackageData {
2021-01-28 15:33:02 +00:00
id: id.repr.clone(),
name: name.clone(),
version: version.clone(),
2021-03-02 12:27:29 +00:00
manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)),
2019-01-10 19:21:14 +00:00
targets: Vec::new(),
is_member,
edition,
2019-01-10 19:21:14 +00:00
dependencies: Vec::new(),
2021-01-28 15:33:02 +00:00
features: meta_pkg.features.clone().into_iter().collect(),
2021-01-21 11:12:19 +00:00
active_features: Vec::new(),
metadata: meta.rust_analyzer.unwrap_or_default(),
2019-01-10 19:21:14 +00:00
});
let pkg_data = &mut packages[pkg];
pkg_by_id.insert(id, pkg);
2021-01-28 15:33:02 +00:00
for meta_tgt in &meta_pkg.targets {
2020-02-18 13:32:19 +00:00
let is_proc_macro = meta_tgt.kind.as_slice() == ["proc-macro"];
2019-01-10 19:21:14 +00:00
let tgt = targets.alloc(TargetData {
2020-03-19 16:53:31 +00:00
package: pkg,
2021-01-28 15:33:02 +00:00
name: meta_tgt.name.clone(),
2021-03-02 12:27:29 +00:00
root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
2019-01-10 19:21:14 +00:00
kind: TargetKind::new(meta_tgt.kind.as_slice()),
2019-11-24 10:33:12 +00:00
is_proc_macro,
2019-01-10 19:21:14 +00:00
});
pkg_data.targets.push(tgt);
}
}
let resolve = meta.resolve.expect("metadata executed with deps");
for mut node in resolve.nodes {
2020-01-28 09:08:17 +00:00
let source = match pkg_by_id.get(&node.id) {
Some(&src) => src,
2020-01-30 17:01:38 +00:00
// FIXME: replace this and a similar branch below with `.unwrap`, once
// https://github.com/rust-lang/cargo/issues/7841
// is fixed and hits stable (around 1.43-is probably?).
2020-01-28 09:08:17 +00:00
None => {
log::error!("Node id do not match in cargo metadata, ignoring {}", node.id);
continue;
}
};
node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
for (dep_node, kind) in node
.deps
.iter()
.flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind)))
{
2020-01-28 09:08:17 +00:00
let pkg = match pkg_by_id.get(&dep_node.pkg) {
Some(&pkg) => pkg,
None => {
log::error!(
"Dep node id do not match in cargo metadata, ignoring {}",
dep_node.pkg
);
continue;
}
};
let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind };
2019-01-10 19:21:14 +00:00
packages[source].dependencies.push(dep);
}
2021-01-21 11:12:19 +00:00
packages[source].active_features.extend(node.features);
2019-01-10 19:21:14 +00:00
}
2021-03-02 12:27:29 +00:00
let workspace_root =
AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
2021-01-28 15:33:02 +00:00
CargoWorkspace { packages, targets, workspace_root }
}
pub fn from_cargo_metadata3(
cargo_toml: &AbsPath,
config: &CargoConfig,
progress: &dyn Fn(String),
) -> Result<CargoWorkspace> {
let meta = CargoWorkspace::fetch_metadata(cargo_toml, config, progress)?;
Ok(CargoWorkspace::new(meta))
2019-01-10 19:21:14 +00:00
}
2019-08-06 08:54:51 +00:00
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
2019-01-10 19:21:14 +00:00
self.packages.iter().map(|(id, _pkg)| id)
}
pub fn target_by_root(&self, root: &AbsPath) -> Option<Target> {
2020-03-19 16:53:31 +00:00
self.packages()
.filter_map(|pkg| self[pkg].targets.iter().find(|&&it| &self[it].root == root))
2020-03-19 16:53:31 +00:00
.next()
.copied()
2019-01-10 19:21:14 +00:00
}
2020-01-10 21:41:52 +00:00
2020-06-24 13:52:07 +00:00
pub fn workspace_root(&self) -> &AbsPath {
2020-01-10 21:41:52 +00:00
&self.workspace_root
}
pub fn package_flag(&self, package: &PackageData) -> String {
if self.is_unique(&*package.name) {
package.name.clone()
} else {
format!("{}:{}", package.name, package.version)
}
}
fn is_unique(&self, name: &str) -> bool {
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
}
2019-01-10 19:21:14 +00:00
}
fn rustc_discover_host_triple(cargo_toml: &AbsPath) -> Option<String> {
let mut rustc = Command::new(toolchain::rustc());
rustc.current_dir(cargo_toml.parent().unwrap()).arg("-vV");
log::debug!("Discovering host platform by {:?}", rustc);
match utf8_stdout(rustc) {
Ok(stdout) => {
let field = "host: ";
let target = stdout.lines().find_map(|l| l.strip_prefix(field));
if let Some(target) = target {
Some(target.to_string())
} else {
// If we fail to resolve the host platform, it's not the end of the world.
log::info!("rustc -vV did not report host platform, got:\n{}", stdout);
None
}
}
Err(e) => {
log::warn!("Failed to discover host platform: {}", e);
None
}
}
}
fn cargo_config_build_target(cargo_toml: &AbsPath) -> Option<String> {
let mut cargo_config = Command::new(toolchain::cargo());
cargo_config
.current_dir(cargo_toml.parent().unwrap())
.args(&["-Z", "unstable-options", "config", "get", "build.target"])
.env("RUSTC_BOOTSTRAP", "1");
// if successful we receive `build.target = "target-triple"`
log::debug!("Discovering cargo config target by {:?}", cargo_config);
match utf8_stdout(cargo_config) {
Ok(stdout) => stdout
.strip_prefix("build.target = \"")
.and_then(|stdout| stdout.strip_suffix('"'))
.map(ToOwned::to_owned),
Err(_) => None,
}
}