]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs
Auto merge of #100845 - timvermeulen:iter_compare, r=scottmcm
[rust.git] / src / tools / rust-analyzer / crates / project-model / src / cargo_workspace.rs
1 //! See [`CargoWorkspace`].
2
3 use std::iter;
4 use std::path::PathBuf;
5 use std::str::from_utf8;
6 use std::{ops, process::Command};
7
8 use anyhow::{Context, Result};
9 use base_db::Edition;
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;
16
17 use crate::CfgOverrides;
18 use crate::{utf8_stdout, ManifestPath};
19
20 /// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
21 /// workspace. It pretty closely mirrors `cargo metadata` output.
22 ///
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
26 /// concepts.
27 ///
28 /// We use absolute paths here, `cargo metadata` guarantees to always produce
29 /// abs paths.
30 #[derive(Debug, Clone, Eq, PartialEq)]
31 pub struct CargoWorkspace {
32     packages: Arena<PackageData>,
33     targets: Arena<TargetData>,
34     workspace_root: AbsPathBuf,
35 }
36
37 impl ops::Index<Package> for CargoWorkspace {
38     type Output = PackageData;
39     fn index(&self, index: Package) -> &PackageData {
40         &self.packages[index]
41     }
42 }
43
44 impl ops::Index<Target> for CargoWorkspace {
45     type Output = TargetData;
46     fn index(&self, index: Target) -> &TargetData {
47         &self.targets[index]
48     }
49 }
50
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.
55     Path(AbsPathBuf),
56     /// Try to automatically detect where the rustc source directory is.
57     Discover,
58 }
59
60 /// Crates to disable `#[cfg(test)]` on.
61 #[derive(Clone, Debug, PartialEq, Eq)]
62 pub enum UnsetTestCrates {
63     None,
64     Only(Vec<String>),
65     All,
66 }
67
68 impl Default for UnsetTestCrates {
69     fn default() -> Self {
70         Self::None
71     }
72 }
73
74 #[derive(Default, Clone, Debug, PartialEq, Eq)]
75 pub struct CargoConfig {
76     /// Do not activate the `default` feature.
77     pub no_default_features: bool,
78
79     /// Activate all available features
80     pub all_features: bool,
81
82     /// List of features to activate.
83     /// This will be ignored if `cargo_all_features` is true.
84     pub features: Vec<String>,
85
86     /// rustc target
87     pub target: Option<String>,
88
89     /// Don't load sysroot crates (`std`, `core` & friends). Might be useful
90     /// when debugging isolated issues.
91     pub no_sysroot: bool,
92
93     /// rustc private crate source
94     pub rustc_source: Option<RustcSource>,
95
96     /// crates to disable `#[cfg(test)]` on
97     pub unset_test_crates: UnsetTestCrates,
98
99     pub wrap_rustc_in_build_scripts: bool,
100
101     pub run_build_script_command: Option<Vec<String>>,
102
103     pub extra_env: FxHashMap<String, String>,
104 }
105
106 impl CargoConfig {
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(
111                 unset_test_crates
112                     .iter()
113                     .cloned()
114                     .zip(iter::repeat_with(|| {
115                         cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())])
116                             .unwrap()
117                     }))
118                     .collect(),
119             ),
120             UnsetTestCrates::All => CfgOverrides::Wildcard(
121                 cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())]).unwrap(),
122             ),
123         }
124     }
125 }
126
127 pub type Package = Idx<PackageData>;
128
129 pub type Target = Idx<TargetData>;
130
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`
137     pub name: String,
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)?
145     pub is_local: bool,
146     // Whether this package is a member of the workspace
147     pub is_member: bool,
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
157     pub id: String,
158     /// The contents of [package.metadata.rust-analyzer]
159     pub metadata: RustAnalyzerPackageMetaData,
160 }
161
162 #[derive(Deserialize, Default, Debug, Clone, Eq, PartialEq)]
163 pub struct RustAnalyzerPackageMetaData {
164     pub rustc_private: bool,
165 }
166
167 #[derive(Debug, Clone, Eq, PartialEq)]
168 pub struct PackageDependency {
169     pub pkg: Package,
170     pub name: String,
171     pub kind: DepKind,
172 }
173
174 #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
175 pub enum DepKind {
176     /// Available to the library, binary, and dev targets in the package (but not the build script).
177     Normal,
178     /// Available only to test and bench targets (and the library target, when built with `cfg(test)`).
179     Dev,
180     /// Available only to the build script target.
181     Build,
182 }
183
184 impl DepKind {
185     fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator<Item = Self> + '_ {
186         let mut dep_kinds = Vec::new();
187         if list.is_empty() {
188             dep_kinds.push(Self::Normal);
189         }
190         for info in list {
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,
196             };
197             dep_kinds.push(kind);
198         }
199         dep_kinds.sort_unstable();
200         dep_kinds.dedup();
201         dep_kinds.into_iter()
202     }
203 }
204
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
211     pub name: String,
212     /// Path to the main source file of the target
213     pub root: AbsPathBuf,
214     /// Kind of target
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>,
220 }
221
222 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
223 pub enum TargetKind {
224     Bin,
225     /// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
226     Lib,
227     Example,
228     Test,
229     Bench,
230     BuildScript,
231     Other,
232 }
233
234 impl TargetKind {
235     fn new(kinds: &[String]) -> TargetKind {
236         for kind in kinds {
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,
245                 _ => continue,
246             };
247         }
248         TargetKind::Other
249     }
250 }
251
252 #[derive(Deserialize, Default)]
253 // Deserialise helper for the cargo metadata
254 struct PackageMetadata {
255     #[serde(rename = "rust-analyzer")]
256     rust_analyzer: Option<RustAnalyzerPackageMetaData>,
257 }
258
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> {
266         let target = config
267             .target
268             .clone()
269             .or_else(|| cargo_config_build_target(cargo_toml, config))
270             .or_else(|| rustc_discover_host_triple(cargo_toml, config));
271
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);
277         } else {
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);
282             }
283             if !config.features.is_empty() {
284                 meta.features(CargoOpt::SomeFeatures(config.features.clone()));
285             }
286         }
287         meta.current_dir(current_dir.as_os_str());
288
289         if let Some(target) = target {
290             meta.other_options(vec![String::from("--filter-platform"), target]);
291         }
292
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());
297
298         fn exec_with_env(
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)?,
308                 });
309             }
310             let stdout = from_utf8(&output.stdout)?
311                 .lines()
312                 .find(|line| line.starts_with('{'))
313                 .ok_or(cargo_metadata::Error::NoJson)?;
314             cargo_metadata::MetadataCommand::parse(stdout)
315         }
316
317         let meta = exec_with_env(&meta, &config.extra_env)
318             .with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))?;
319
320         Ok(meta)
321     }
322
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();
327
328         let ws_members = &meta.workspace_members;
329
330         meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
331         for meta_pkg in &meta.packages {
332             let cargo_metadata::Package {
333                 id,
334                 edition,
335                 name,
336                 manifest_path,
337                 version,
338                 metadata,
339                 repository,
340                 ..
341             } = meta_pkg;
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,
347                 _ => {
348                     tracing::error!("Unsupported edition `{:?}`", edition);
349                     Edition::CURRENT
350                 }
351             };
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);
356
357             let pkg = packages.alloc(PackageData {
358                 id: id.repr.clone(),
359                 name: name.clone(),
360                 version: version.clone(),
361                 manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)).try_into().unwrap(),
362                 targets: Vec::new(),
363                 is_local,
364                 is_member,
365                 edition,
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(),
371             });
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 {
377                     package: pkg,
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()),
381                     is_proc_macro,
382                     required_features: meta_tgt.required_features.clone(),
383                 });
384                 pkg_data.targets.push(tgt);
385             }
386         }
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) {
390                 Some(&src) => src,
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?).
394                 None => {
395                     tracing::error!("Node id do not match in cargo metadata, ignoring {}", node.id);
396                     continue;
397                 }
398             };
399             node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
400             for (dep_node, kind) in node
401                 .deps
402                 .iter()
403                 .flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind)))
404             {
405                 let pkg = match pkg_by_id.get(&dep_node.pkg) {
406                     Some(&pkg) => pkg,
407                     None => {
408                         tracing::error!(
409                             "Dep node id do not match in cargo metadata, ignoring {}",
410                             dep_node.pkg
411                         );
412                         continue;
413                     }
414                 };
415                 let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind };
416                 packages[source].dependencies.push(dep);
417             }
418             packages[source].active_features.extend(node.features);
419         }
420
421         let workspace_root =
422             AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
423
424         CargoWorkspace { packages, targets, workspace_root }
425     }
426
427     pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
428         self.packages.iter().map(|(id, _pkg)| id)
429     }
430
431     pub fn target_by_root(&self, root: &AbsPath) -> Option<Target> {
432         self.packages()
433             .filter(|&pkg| self[pkg].is_member)
434             .find_map(|pkg| self[pkg].targets.iter().find(|&&it| &self[it].root == root))
435             .copied()
436     }
437
438     pub fn workspace_root(&self) -> &AbsPath {
439         &self.workspace_root
440     }
441
442     pub fn package_flag(&self, package: &PackageData) -> String {
443         if self.is_unique(&*package.name) {
444             package.name.clone()
445         } else {
446             format!("{}:{}", package.name, package.version)
447         }
448     }
449
450     pub fn parent_manifests(&self, manifest_path: &ManifestPath) -> Option<Vec<ManifestPath>> {
451         let mut found = false;
452         let parent_manifests = self
453             .packages()
454             .filter_map(|pkg| {
455                 if !found && &self[pkg].manifest == manifest_path {
456                     found = true
457                 }
458                 self[pkg].dependencies.iter().find_map(|dep| {
459                     if &self[dep.pkg].manifest == manifest_path {
460                         return Some(self[pkg].manifest.clone());
461                     }
462                     None
463                 })
464             })
465             .collect::<Vec<ManifestPath>>();
466
467         // some packages has this pkg as dep. return their manifests
468         if parent_manifests.len() > 0 {
469             return Some(parent_manifests);
470         }
471
472         // this pkg is inside this cargo workspace, fallback to workspace root
473         if found {
474             return Some(vec![
475                 ManifestPath::try_from(self.workspace_root().join("Cargo.toml")).ok()?
476             ]);
477         }
478
479         // not in this workspace
480         None
481     }
482
483     fn is_unique(&self, name: &str) -> bool {
484         self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
485     }
486 }
487
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) {
494         Ok(stdout) => {
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())
499             } else {
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);
502                 None
503             }
504         }
505         Err(e) => {
506             tracing::warn!("Failed to discover host platform: {}", e);
507             None
508         }
509     }
510 }
511
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);
515     cargo_config
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) {
522         Ok(stdout) => stdout
523             .strip_prefix("build.target = \"")
524             .and_then(|stdout| stdout.strip_suffix('"'))
525             .map(ToOwned::to_owned),
526         Err(_) => None,
527     }
528 }