]> git.lizzy.rs Git - rust.git/blobdiff - crates/project_model/src/cargo_workspace.rs
minor: simplify
[rust.git] / crates / project_model / src / cargo_workspace.rs
index a1ab9c6db57290c3a76971e26991d9793c5f18eb..8e66f240909291dbe994761526ea37f8c86417db 100644 (file)
@@ -1,27 +1,23 @@
-//! FIXME: write short doc here
+//! See [`CargoWorkspace`].
 
-use std::{
-    convert::TryInto,
-    ffi::OsStr,
-    io::BufReader,
-    ops,
-    path::{Path, PathBuf},
-    process::{Command, Stdio},
-};
+use std::convert::TryInto;
+use std::iter;
+use std::path::PathBuf;
+use std::{ops, process::Command};
 
 use anyhow::{Context, Result};
-use arena::{Arena, Idx};
 use base_db::Edition;
-use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
-use itertools::Itertools;
+use cargo_metadata::{CargoOpt, MetadataCommand};
+use la_arena::{Arena, Idx};
 use paths::{AbsPath, AbsPathBuf};
 use rustc_hash::FxHashMap;
-use stdx::JodChild;
+use serde::Deserialize;
+use serde_json::from_value;
 
-use crate::cfg_flag::CfgFlag;
-use crate::utf8_stdout;
+use crate::CfgOverrides;
+use crate::{utf8_stdout, ManifestPath};
 
-/// `CargoWorkspace` represents the logical structure of, well, a Cargo
+/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
 /// workspace. It pretty closely mirrors `cargo metadata` output.
 ///
 /// Note that internally, rust analyzer uses a different structure:
@@ -52,6 +48,15 @@ fn index(&self, index: Target) -> &TargetData {
     }
 }
 
+/// 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,
+}
+
 #[derive(Default, Clone, Debug, PartialEq, Eq)]
 pub struct CargoConfig {
     /// Do not activate the `default` feature.
@@ -64,9 +69,6 @@ pub struct CargoConfig {
     /// This will be ignored if `cargo_all_features` is true.
     pub features: Vec<String>,
 
-    /// Runs cargo check on launch to figure out the correct values of OUT_DIR
-    pub load_out_dirs_from_check: bool,
-
     /// rustc target
     pub target: Option<String>,
 
@@ -75,7 +77,24 @@ pub struct CargoConfig {
     pub no_sysroot: bool,
 
     /// rustc private crate source
-    pub rustc_source: Option<AbsPathBuf>,
+    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()
+    }
 }
 
 pub type Package = Idx<PackageData>;
@@ -86,11 +105,11 @@ pub struct CargoConfig {
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub struct PackageData {
     /// Version given in the `Cargo.toml`
-    pub version: String,
+    pub version: semver::Version,
     /// Name as given in the `Cargo.toml`
     pub name: String,
     /// Path containing the `Cargo.toml`
-    pub manifest: AbsPathBuf,
+    pub manifest: ManifestPath,
     /// Targets provided by the crate (lib, bin, example, test, ...)
     pub targets: Vec<Target>,
     /// Is this package a member of the current workspace
@@ -99,25 +118,57 @@ pub struct PackageData {
     pub dependencies: Vec<PackageDependency>,
     /// Rust edition for this package
     pub edition: Edition,
-    /// List of features to activate
-    pub features: Vec<String>,
-    /// List of config flags defined by this package's build script
-    pub cfgs: Vec<CfgFlag>,
-    /// List of cargo-related environment variables with their value
-    ///
-    /// If the package has a build script which defines environment variables,
-    /// they can also be found here.
-    pub envs: Vec<(String, String)>,
-    /// Directory where a build script might place its output
-    pub out_dir: Option<AbsPathBuf>,
-    /// Path to the proc-macro library file if this package exposes proc-macros
-    pub proc_macro_dylib_path: Option<AbsPathBuf>,
+    /// 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>,
+    // 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,
 }
 
 #[derive(Debug, Clone, Eq, PartialEq)]
 pub struct PackageDependency {
     pub pkg: Package,
     pub name: String,
+    pub kind: DepKind,
+}
+
+#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
+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);
+        }
+        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,
+                cargo_metadata::DependencyKind::Unknown => continue,
+            };
+            dep_kinds.push(kind);
+        }
+        dep_kinds.sort_unstable();
+        dep_kinds.dedup();
+        dep_kinds.into_iter()
+    }
 }
 
 /// Information associated with a package's target
@@ -143,6 +194,7 @@ pub enum TargetKind {
     Example,
     Test,
     Bench,
+    BuildScript,
     Other,
 }
 
@@ -154,6 +206,7 @@ fn new(kinds: &[String]) -> TargetKind {
                 "test" => TargetKind::Test,
                 "bench" => TargetKind::Bench,
                 "example" => TargetKind::Example,
+                "custom-build" => TargetKind::BuildScript,
                 "proc-macro" => TargetKind::Lib,
                 _ if kind.contains("lib") => TargetKind::Lib,
                 _ => continue,
@@ -163,18 +216,25 @@ fn new(kinds: &[String]) -> TargetKind {
     }
 }
 
-impl PackageData {
-    pub fn root(&self) -> &AbsPath {
-        self.manifest.parent().unwrap()
-    }
+#[derive(Deserialize, Default)]
+// Deserialise helper for the cargo metadata
+struct PackageMetadata {
+    #[serde(rename = "rust-analyzer")]
+    rust_analyzer: Option<RustAnalyzerPackageMetaData>,
 }
 
 impl CargoWorkspace {
-    pub fn from_cargo_metadata(
-        cargo_toml: &AbsPath,
+    pub fn fetch_metadata(
+        cargo_toml: &ManifestPath,
         config: &CargoConfig,
         progress: &dyn Fn(String),
-    ) -> Result<CargoWorkspace> {
+    ) -> Result<cargo_metadata::Metadata> {
+        let target = config
+            .target
+            .clone()
+            .or_else(|| cargo_config_build_target(cargo_toml))
+            .or_else(|| rustc_discover_host_triple(cargo_toml));
+
         let mut meta = MetadataCommand::new();
         meta.cargo_path(toolchain::cargo());
         meta.manifest_path(cargo_toml.to_path_buf());
@@ -190,72 +250,25 @@ pub fn from_cargo_metadata(
                 meta.features(CargoOpt::SomeFeatures(config.features.clone()));
             }
         }
-        if let Some(parent) = cargo_toml.parent() {
-            meta.current_dir(parent.to_path_buf());
-        }
-        let target = if let Some(target) = config.target.as_ref() {
-            Some(target.clone())
-        } else {
-            // cargo metadata defaults to giving information for _all_ targets.
-            // In the absence of a preference from the user, we use the host platform.
-            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
-                }
-            }
-        };
+        meta.current_dir(cargo_toml.parent().as_os_str());
+
         if let Some(target) = target {
             meta.other_options(vec![String::from("--filter-platform"), target]);
         }
 
-        // FIXME: Currently MetadataCommand is not based on parse_stream,
-        // So we just report it as a whole
+        // 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 mut 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)
-                .map(|dir| dir.to_string_lossy().to_string())
-                .unwrap_or_else(|| "<failed to get path>".into());
-
-            format!(
-                "Failed to run `cargo metadata --manifest-path {}` in `{}`",
-                cargo_toml.display(),
-                workdir
-            )
+
+        let meta = meta.exec().with_context(|| {
+            format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display(),)
         })?;
 
-        let mut out_dir_by_id = FxHashMap::default();
-        let mut cfgs = FxHashMap::default();
-        let mut envs = FxHashMap::default();
-        let mut proc_macro_dylib_paths = FxHashMap::default();
-        if config.load_out_dirs_from_check {
-            let resources = load_extern_resources(cargo_toml, config, progress)?;
-            out_dir_by_id = resources.out_dirs;
-            cfgs = resources.cfgs;
-            envs = resources.env;
-            proc_macro_dylib_paths = resources.proc_dylib_paths;
-        }
+        Ok(meta)
+    }
 
+    pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace {
         let mut pkg_by_id = FxHashMap::default();
         let mut packages = Arena::default();
         let mut targets = Arena::default();
@@ -263,38 +276,38 @@ pub fn from_cargo_metadata(
         let ws_members = &meta.workspace_members;
 
         meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
-        for meta_pkg in meta.packages {
-            let id = meta_pkg.id.clone();
-            inject_cargo_env(&meta_pkg, envs.entry(id).or_default());
-
-            let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } =
-                meta_pkg;
-            let is_member = ws_members.contains(&id);
-            let edition = edition
-                .parse::<Edition>()
-                .with_context(|| format!("Failed to parse edition {}", edition))?;
+        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();
+            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
+            });
+
             let pkg = packages.alloc(PackageData {
-                name,
-                version: version.to_string(),
-                manifest: AbsPathBuf::assert(manifest_path),
+                id: id.repr.clone(),
+                name: name.clone(),
+                version: version.clone(),
+                manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)).try_into().unwrap(),
                 targets: Vec::new(),
                 is_member,
                 edition,
                 dependencies: Vec::new(),
-                features: Vec::new(),
-                cfgs: cfgs.get(&id).cloned().unwrap_or_default(),
-                envs: envs.get(&id).cloned().unwrap_or_default(),
-                out_dir: out_dir_by_id.get(&id).cloned(),
-                proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(),
+                features: meta_pkg.features.clone().into_iter().collect(),
+                active_features: Vec::new(),
+                metadata: meta.rust_analyzer.unwrap_or_default(),
             });
             let pkg_data = &mut packages[pkg];
             pkg_by_id.insert(id, pkg);
-            for meta_tgt in meta_pkg.targets {
+            for meta_tgt in &meta_pkg.targets {
                 let is_proc_macro = meta_tgt.kind.as_slice() == ["proc-macro"];
                 let tgt = targets.alloc(TargetData {
                     package: pkg,
-                    name: meta_tgt.name,
-                    root: AbsPathBuf::assert(meta_tgt.src_path.clone()),
+                    name: meta_tgt.name.clone(),
+                    root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
                     kind: TargetKind::new(meta_tgt.kind.as_slice()),
                     is_proc_macro,
                 });
@@ -314,7 +327,11 @@ pub fn from_cargo_metadata(
                 }
             };
             node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
-            for dep_node in node.deps {
+            for (dep_node, kind) in node
+                .deps
+                .iter()
+                .flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind)))
+            {
                 let pkg = match pkg_by_id.get(&dep_node.pkg) {
                     Some(&pkg) => pkg,
                     None => {
@@ -325,14 +342,25 @@ pub fn from_cargo_metadata(
                         continue;
                     }
                 };
-                let dep = PackageDependency { name: dep_node.name, pkg };
+                let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind };
                 packages[source].dependencies.push(dep);
             }
-            packages[source].features.extend(node.features);
+            packages[source].active_features.extend(node.features);
         }
 
-        let workspace_root = AbsPathBuf::assert(meta.workspace_root);
-        Ok(CargoWorkspace { packages, targets, workspace_root: workspace_root })
+        let workspace_root =
+            AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
+
+        CargoWorkspace { packages, targets, workspace_root }
+    }
+
+    pub fn from_cargo_metadata3(
+        cargo_toml: &ManifestPath,
+        config: &CargoConfig,
+        progress: &dyn Fn(String),
+    ) -> Result<CargoWorkspace> {
+        let meta = CargoWorkspace::fetch_metadata(cargo_toml, config, progress)?;
+        Ok(CargoWorkspace::new(meta))
     }
 
     pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
@@ -363,148 +391,42 @@ fn is_unique(&self, name: &str) -> bool {
     }
 }
 
-#[derive(Debug, Clone, Default)]
-pub(crate) struct ExternResources {
-    out_dirs: FxHashMap<PackageId, AbsPathBuf>,
-    proc_dylib_paths: FxHashMap<PackageId, AbsPathBuf>,
-    cfgs: FxHashMap<PackageId, Vec<CfgFlag>>,
-    env: FxHashMap<PackageId, Vec<(String, String)>>,
-}
-
-pub(crate) fn load_extern_resources(
-    cargo_toml: &Path,
-    cargo_features: &CargoConfig,
-    progress: &dyn Fn(String),
-) -> Result<ExternResources> {
-    let mut cmd = Command::new(toolchain::cargo());
-    cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"]).arg(cargo_toml);
-
-    // --all-targets includes tests, benches and examples in addition to the
-    // default lib and bins. This is an independent concept from the --targets
-    // flag below.
-    cmd.arg("--all-targets");
-
-    if let Some(target) = &cargo_features.target {
-        cmd.args(&["--target", target]);
-    }
-
-    if cargo_features.all_features {
-        cmd.arg("--all-features");
-    } else {
-        if cargo_features.no_default_features {
-            // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
-            // https://github.com/oli-obk/cargo_metadata/issues/79
-            cmd.arg("--no-default-features");
-        }
-        if !cargo_features.features.is_empty() {
-            cmd.arg("--features");
-            cmd.arg(cargo_features.features.join(" "));
-        }
-    }
-
-    cmd.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
-
-    let mut child = cmd.spawn().map(JodChild)?;
-    let child_stdout = child.stdout.take().unwrap();
-    let stdout = BufReader::new(child_stdout);
-
-    let mut res = ExternResources::default();
-    for message in cargo_metadata::Message::parse_stream(stdout) {
-        if let Ok(message) = message {
-            match message {
-                Message::BuildScriptExecuted(BuildScript {
-                    package_id,
-                    out_dir,
-                    cfgs,
-                    env,
-                    ..
-                }) => {
-                    let cfgs = {
-                        let mut acc = Vec::new();
-                        for cfg in cfgs {
-                            match cfg.parse::<CfgFlag>() {
-                                Ok(it) => acc.push(it),
-                                Err(err) => {
-                                    anyhow::bail!("invalid cfg from cargo-metadata: {}", err)
-                                }
-                            };
-                        }
-                        acc
-                    };
-                    // cargo_metadata crate returns default (empty) path for
-                    // older cargos, which is not absolute, so work around that.
-                    if out_dir != PathBuf::default() {
-                        let out_dir = AbsPathBuf::assert(out_dir);
-                        res.out_dirs.insert(package_id.clone(), out_dir);
-                        res.cfgs.insert(package_id.clone(), cfgs);
-                    }
-
-                    res.env.insert(package_id, env);
-                }
-                Message::CompilerArtifact(message) => {
-                    progress(format!("metadata {}", message.target.name));
-
-                    if message.target.kind.contains(&"proc-macro".to_string()) {
-                        let package_id = message.package_id;
-                        // Skip rmeta file
-                        if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name))
-                        {
-                            let filename = AbsPathBuf::assert(filename.clone());
-                            res.proc_dylib_paths.insert(package_id, filename);
-                        }
-                    }
-                }
-                Message::CompilerMessage(message) => {
-                    progress(message.target.name.clone());
-                }
-                Message::Unknown => (),
-                Message::BuildFinished(_) => {}
-                Message::TextLine(_) => {}
+fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> {
+    let mut rustc = Command::new(toolchain::rustc());
+    rustc.current_dir(cargo_toml.parent()).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
             }
         }
-    }
-    Ok(res)
-}
-
-// FIXME: File a better way to know if it is a dylib
-fn is_dylib(path: &Path) -> bool {
-    match path.extension().and_then(OsStr::to_str).map(|it| it.to_string().to_lowercase()) {
-        None => false,
-        Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
+        Err(e) => {
+            log::warn!("Failed to discover host platform: {}", e);
+            None
+        }
     }
 }
 
-/// Recreates the compile-time environment variables that Cargo sets.
-///
-/// Should be synced with <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
-fn inject_cargo_env(package: &cargo_metadata::Package, env: &mut Vec<(String, String)>) {
-    // FIXME: Missing variables:
-    // CARGO, CARGO_PKG_HOMEPAGE, CARGO_CRATE_NAME, CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
-
-    let mut manifest_dir = package.manifest_path.clone();
-    manifest_dir.pop();
-    if let Some(cargo_manifest_dir) = manifest_dir.to_str() {
-        env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into()));
+fn cargo_config_build_target(cargo_toml: &ManifestPath) -> Option<String> {
+    let mut cargo_config = Command::new(toolchain::cargo());
+    cargo_config
+        .current_dir(cargo_toml.parent())
+        .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,
     }
-
-    env.push(("CARGO_PKG_VERSION".into(), package.version.to_string()));
-    env.push(("CARGO_PKG_VERSION_MAJOR".into(), package.version.major.to_string()));
-    env.push(("CARGO_PKG_VERSION_MINOR".into(), package.version.minor.to_string()));
-    env.push(("CARGO_PKG_VERSION_PATCH".into(), package.version.patch.to_string()));
-
-    let pre = package.version.pre.iter().map(|id| id.to_string()).format(".");
-    env.push(("CARGO_PKG_VERSION_PRE".into(), pre.to_string()));
-
-    let authors = package.authors.join(";");
-    env.push(("CARGO_PKG_AUTHORS".into(), authors));
-
-    env.push(("CARGO_PKG_NAME".into(), package.name.clone()));
-    env.push(("CARGO_PKG_DESCRIPTION".into(), package.description.clone().unwrap_or_default()));
-    //env.push(("CARGO_PKG_HOMEPAGE".into(), package.homepage.clone().unwrap_or_default()));
-    env.push(("CARGO_PKG_REPOSITORY".into(), package.repository.clone().unwrap_or_default()));
-    env.push(("CARGO_PKG_LICENSE".into(), package.license.clone().unwrap_or_default()));
-
-    let license_file =
-        package.license_file.as_ref().map(|buf| buf.display().to_string()).unwrap_or_default();
-    env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file));
 }