From 1076d21fc0d86ae3b7bb7fca610548fedd42ab4a Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 6 Mar 2021 12:17:22 +0000 Subject: [PATCH] Implement opt-in (and opt-out) rustc_private --- crates/project_model/src/cargo_workspace.rs | 23 +++++++++++++++++++-- crates/project_model/src/workspace.rs | 6 +++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs index f7241b71136..beda2f61fa4 100644 --- a/crates/project_model/src/cargo_workspace.rs +++ b/crates/project_model/src/cargo_workspace.rs @@ -9,6 +9,8 @@ use la_arena::{Arena, Idx}; use paths::{AbsPath, AbsPathBuf}; use rustc_hash::FxHashMap; +use serde::Deserialize; +use serde_json::from_value; use crate::build_data::BuildDataConfig; use crate::utf8_stdout; @@ -104,6 +106,13 @@ pub struct PackageData { pub active_features: Vec, // 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: Option, } #[derive(Debug, Clone, Eq, PartialEq)] @@ -161,6 +170,13 @@ pub fn root(&self) -> &AbsPath { } } +#[derive(Deserialize, Default)] +// Deserialise helper for the cargo metadata +struct PackageMetadata { + #[serde(rename = "rust-analyzer")] + rust_analyzer: Option, +} + impl CargoWorkspace { pub fn from_cargo_metadata( cargo_toml: &AbsPath, @@ -244,8 +260,10 @@ pub fn from_cargo_metadata( meta.packages.sort_by(|a, b| a.id.cmp(&b.id)); for meta_pkg in &meta.packages { - let cargo_metadata::Package { id, edition, name, manifest_path, version, .. } = - meta_pkg; + let cargo_metadata::Package { + id, edition, name, manifest_path, version, metadata, .. + } = meta_pkg; + let meta = from_value::(metadata.clone()).unwrap_or_default(); let is_member = ws_members.contains(&id); let edition = edition .parse::() @@ -262,6 +280,7 @@ pub fn from_cargo_metadata( dependencies: Vec::new(), 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); diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs index 0220efdb4e5..10e60854795 100644 --- a/crates/project_model/src/workspace.rs +++ b/crates/project_model/src/workspace.rs @@ -499,7 +499,11 @@ fn cargo_to_crate_graph( if let Some(&to) = pkg_to_lib_crate.get(&dep) { for pkg in cargo.packages() { - if !cargo[pkg].is_member { + let package = &cargo[pkg]; + if matches!( + (package.is_member, package.metadata.rustc_private), + (true, Some(false)) | (false, Some(false)) | (false, None) + ) { continue; } for &from in pkg_crates.get(&pkg).into_iter().flatten() {