• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! See [`CargoWorkspace`].
2 
3 use std::path::PathBuf;
4 use std::str::from_utf8;
5 use std::{ops, process::Command};
6 
7 use anyhow::{Context, Result};
8 use base_db::Edition;
9 use cargo_metadata::{CargoOpt, MetadataCommand};
10 use la_arena::{Arena, Idx};
11 use paths::{AbsPath, AbsPathBuf};
12 use rustc_hash::{FxHashMap, FxHashSet};
13 use serde::Deserialize;
14 use serde_json::from_value;
15 
16 use crate::{utf8_stdout, InvocationLocation, ManifestPath};
17 use crate::{CfgOverrides, InvocationStrategy};
18 
19 /// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
20 /// workspace. It pretty closely mirrors `cargo metadata` output.
21 ///
22 /// Note that internally, rust-analyzer uses a different structure:
23 /// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates,
24 /// while this knows about `Packages` & `Targets`: purely cargo-related
25 /// concepts.
26 ///
27 /// We use absolute paths here, `cargo metadata` guarantees to always produce
28 /// abs paths.
29 #[derive(Debug, Clone, Eq, PartialEq)]
30 pub struct CargoWorkspace {
31     packages: Arena<PackageData>,
32     targets: Arena<TargetData>,
33     workspace_root: AbsPathBuf,
34     target_directory: AbsPathBuf,
35 }
36 
37 impl ops::Index<Package> for CargoWorkspace {
38     type Output = PackageData;
index(&self, index: Package) -> &PackageData39     fn index(&self, index: Package) -> &PackageData {
40         &self.packages[index]
41     }
42 }
43 
44 impl ops::Index<Target> for CargoWorkspace {
45     type Output = TargetData;
index(&self, index: Target) -> &TargetData46     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 RustLibSource {
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 #[derive(Clone, Debug, PartialEq, Eq)]
61 pub enum CargoFeatures {
62     All,
63     Selected {
64         /// List of features to activate.
65         features: Vec<String>,
66         /// Do not activate the `default` feature.
67         no_default_features: bool,
68     },
69 }
70 
71 impl Default for CargoFeatures {
default() -> Self72     fn default() -> Self {
73         CargoFeatures::Selected { features: vec![], no_default_features: false }
74     }
75 }
76 
77 #[derive(Default, Clone, Debug, PartialEq, Eq)]
78 pub struct CargoConfig {
79     /// List of features to activate.
80     pub features: CargoFeatures,
81     /// rustc target
82     pub target: Option<String>,
83     /// Sysroot loading behavior
84     pub sysroot: Option<RustLibSource>,
85     pub sysroot_src: Option<AbsPathBuf>,
86     /// rustc private crate source
87     pub rustc_source: Option<RustLibSource>,
88     pub cfg_overrides: CfgOverrides,
89     /// Invoke `cargo check` through the RUSTC_WRAPPER.
90     pub wrap_rustc_in_build_scripts: bool,
91     /// The command to run instead of `cargo check` for building build scripts.
92     pub run_build_script_command: Option<Vec<String>>,
93     /// Extra args to pass to the cargo command.
94     pub extra_args: Vec<String>,
95     /// Extra env vars to set when invoking the cargo command
96     pub extra_env: FxHashMap<String, String>,
97     pub invocation_strategy: InvocationStrategy,
98     pub invocation_location: InvocationLocation,
99 }
100 
101 pub type Package = Idx<PackageData>;
102 
103 pub type Target = Idx<TargetData>;
104 
105 /// Information associated with a cargo crate
106 #[derive(Debug, Clone, Eq, PartialEq)]
107 pub struct PackageData {
108     /// Version given in the `Cargo.toml`
109     pub version: semver::Version,
110     /// Name as given in the `Cargo.toml`
111     pub name: String,
112     /// Repository as given in the `Cargo.toml`
113     pub repository: Option<String>,
114     /// Path containing the `Cargo.toml`
115     pub manifest: ManifestPath,
116     /// Targets provided by the crate (lib, bin, example, test, ...)
117     pub targets: Vec<Target>,
118     /// Does this package come from the local filesystem (and is editable)?
119     pub is_local: bool,
120     /// Whether this package is a member of the workspace
121     pub is_member: bool,
122     /// List of packages this package depends on
123     pub dependencies: Vec<PackageDependency>,
124     /// Rust edition for this package
125     pub edition: Edition,
126     /// Features provided by the crate, mapped to the features required by that feature.
127     pub features: FxHashMap<String, Vec<String>>,
128     /// List of features enabled on this package
129     pub active_features: Vec<String>,
130     /// String representation of package id
131     pub id: String,
132     /// The contents of [package.metadata.rust-analyzer]
133     pub metadata: RustAnalyzerPackageMetaData,
134 }
135 
136 #[derive(Deserialize, Default, Debug, Clone, Eq, PartialEq)]
137 pub struct RustAnalyzerPackageMetaData {
138     pub rustc_private: bool,
139 }
140 
141 #[derive(Debug, Clone, Eq, PartialEq)]
142 pub struct PackageDependency {
143     pub pkg: Package,
144     pub name: String,
145     pub kind: DepKind,
146 }
147 
148 #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
149 pub enum DepKind {
150     /// Available to the library, binary, and dev targets in the package (but not the build script).
151     Normal,
152     /// Available only to test and bench targets (and the library target, when built with `cfg(test)`).
153     Dev,
154     /// Available only to the build script target.
155     Build,
156 }
157 
158 impl DepKind {
iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator<Item = Self> + '_159     fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator<Item = Self> + '_ {
160         let mut dep_kinds = Vec::new();
161         if list.is_empty() {
162             dep_kinds.push(Self::Normal);
163         }
164         for info in list {
165             let kind = match info.kind {
166                 cargo_metadata::DependencyKind::Normal => Self::Normal,
167                 cargo_metadata::DependencyKind::Development => Self::Dev,
168                 cargo_metadata::DependencyKind::Build => Self::Build,
169                 cargo_metadata::DependencyKind::Unknown => continue,
170             };
171             dep_kinds.push(kind);
172         }
173         dep_kinds.sort_unstable();
174         dep_kinds.dedup();
175         dep_kinds.into_iter()
176     }
177 }
178 
179 /// Information associated with a package's target
180 #[derive(Debug, Clone, Eq, PartialEq)]
181 pub struct TargetData {
182     /// Package that provided this target
183     pub package: Package,
184     /// Name as given in the `Cargo.toml` or generated from the file name
185     pub name: String,
186     /// Path to the main source file of the target
187     pub root: AbsPathBuf,
188     /// Kind of target
189     pub kind: TargetKind,
190     /// Is this target a proc-macro
191     pub is_proc_macro: bool,
192     /// Required features of the target without which it won't build
193     pub required_features: Vec<String>,
194 }
195 
196 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
197 pub enum TargetKind {
198     Bin,
199     /// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
200     Lib,
201     Example,
202     Test,
203     Bench,
204     BuildScript,
205     Other,
206 }
207 
208 impl TargetKind {
new(kinds: &[String]) -> TargetKind209     fn new(kinds: &[String]) -> TargetKind {
210         for kind in kinds {
211             return match kind.as_str() {
212                 "bin" => TargetKind::Bin,
213                 "test" => TargetKind::Test,
214                 "bench" => TargetKind::Bench,
215                 "example" => TargetKind::Example,
216                 "custom-build" => TargetKind::BuildScript,
217                 "proc-macro" => TargetKind::Lib,
218                 _ if kind.contains("lib") => TargetKind::Lib,
219                 _ => continue,
220             };
221         }
222         TargetKind::Other
223     }
224 }
225 
226 // Deserialize helper for the cargo metadata
227 #[derive(Deserialize, Default)]
228 struct PackageMetadata {
229     #[serde(rename = "rust-analyzer")]
230     rust_analyzer: Option<RustAnalyzerPackageMetaData>,
231 }
232 
233 impl CargoWorkspace {
fetch_metadata( cargo_toml: &ManifestPath, current_dir: &AbsPath, config: &CargoConfig, progress: &dyn Fn(String), ) -> Result<cargo_metadata::Metadata>234     pub fn fetch_metadata(
235         cargo_toml: &ManifestPath,
236         current_dir: &AbsPath,
237         config: &CargoConfig,
238         progress: &dyn Fn(String),
239     ) -> Result<cargo_metadata::Metadata> {
240         let targets = find_list_of_build_targets(config, cargo_toml);
241 
242         let mut meta = MetadataCommand::new();
243         meta.cargo_path(toolchain::cargo());
244         meta.manifest_path(cargo_toml.to_path_buf());
245         match &config.features {
246             CargoFeatures::All => {
247                 meta.features(CargoOpt::AllFeatures);
248             }
249             CargoFeatures::Selected { features, no_default_features } => {
250                 if *no_default_features {
251                     meta.features(CargoOpt::NoDefaultFeatures);
252                 }
253                 if !features.is_empty() {
254                     meta.features(CargoOpt::SomeFeatures(features.clone()));
255                 }
256             }
257         }
258         meta.current_dir(current_dir.as_os_str());
259 
260         let mut other_options = vec![];
261         // cargo metadata only supports a subset of flags of what cargo usually accepts, and usually
262         // the only relevant flags for metadata here are unstable ones, so we pass those along
263         // but nothing else
264         let mut extra_args = config.extra_args.iter();
265         while let Some(arg) = extra_args.next() {
266             if arg == "-Z" {
267                 if let Some(arg) = extra_args.next() {
268                     other_options.push("-Z".to_owned());
269                     other_options.push(arg.to_owned());
270                 }
271             }
272         }
273 
274         if !targets.is_empty() {
275             other_options.append(
276                 &mut targets
277                     .into_iter()
278                     .flat_map(|target| ["--filter-platform".to_owned().to_string(), target])
279                     .collect(),
280             );
281         }
282         meta.other_options(other_options);
283 
284         // FIXME: Fetching metadata is a slow process, as it might require
285         // calling crates.io. We should be reporting progress here, but it's
286         // unclear whether cargo itself supports it.
287         progress("metadata".to_string());
288 
289         (|| -> Result<cargo_metadata::Metadata, cargo_metadata::Error> {
290             let mut command = meta.cargo_command();
291             command.envs(&config.extra_env);
292             let output = command.output()?;
293             if !output.status.success() {
294                 return Err(cargo_metadata::Error::CargoMetadata {
295                     stderr: String::from_utf8(output.stderr)?,
296                 });
297             }
298             let stdout = from_utf8(&output.stdout)?
299                 .lines()
300                 .find(|line| line.starts_with('{'))
301                 .ok_or(cargo_metadata::Error::NoJson)?;
302             cargo_metadata::MetadataCommand::parse(stdout)
303         })()
304         .with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))
305     }
306 
new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace307     pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace {
308         let mut pkg_by_id = FxHashMap::default();
309         let mut packages = Arena::default();
310         let mut targets = Arena::default();
311 
312         let ws_members = &meta.workspace_members;
313 
314         meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
315         for meta_pkg in meta.packages {
316             let cargo_metadata::Package {
317                 name,
318                 version,
319                 id,
320                 source,
321                 targets: meta_targets,
322                 features,
323                 manifest_path,
324                 repository,
325                 edition,
326                 metadata,
327                 ..
328             } = meta_pkg;
329             let meta = from_value::<PackageMetadata>(metadata).unwrap_or_default();
330             let edition = match edition {
331                 cargo_metadata::Edition::E2015 => Edition::Edition2015,
332                 cargo_metadata::Edition::E2018 => Edition::Edition2018,
333                 cargo_metadata::Edition::E2021 => Edition::Edition2021,
334                 _ => {
335                     tracing::error!("Unsupported edition `{:?}`", edition);
336                     Edition::CURRENT
337                 }
338             };
339             // We treat packages without source as "local" packages. That includes all members of
340             // the current workspace, as well as any path dependency outside the workspace.
341             let is_local = source.is_none();
342             let is_member = ws_members.contains(&id);
343 
344             let pkg = packages.alloc(PackageData {
345                 id: id.repr.clone(),
346                 name,
347                 version,
348                 manifest: AbsPathBuf::assert(manifest_path.into()).try_into().unwrap(),
349                 targets: Vec::new(),
350                 is_local,
351                 is_member,
352                 edition,
353                 repository,
354                 dependencies: Vec::new(),
355                 features: features.into_iter().collect(),
356                 active_features: Vec::new(),
357                 metadata: meta.rust_analyzer.unwrap_or_default(),
358             });
359             let pkg_data = &mut packages[pkg];
360             pkg_by_id.insert(id, pkg);
361             for meta_tgt in meta_targets {
362                 let cargo_metadata::Target { name, kind, required_features, src_path, .. } =
363                     meta_tgt;
364                 let tgt = targets.alloc(TargetData {
365                     package: pkg,
366                     name,
367                     root: AbsPathBuf::assert(src_path.into()),
368                     kind: TargetKind::new(&kind),
369                     is_proc_macro: &*kind == ["proc-macro"],
370                     required_features,
371                 });
372                 pkg_data.targets.push(tgt);
373             }
374         }
375         let resolve = meta.resolve.expect("metadata executed with deps");
376         for mut node in resolve.nodes {
377             let &source = pkg_by_id.get(&node.id).unwrap();
378             node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
379             let dependencies = node
380                 .deps
381                 .iter()
382                 .flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind)));
383             for (dep_node, kind) in dependencies {
384                 let &pkg = pkg_by_id.get(&dep_node.pkg).unwrap();
385                 let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind };
386                 packages[source].dependencies.push(dep);
387             }
388             packages[source].active_features.extend(node.features);
389         }
390 
391         let workspace_root =
392             AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
393 
394         let target_directory =
395             AbsPathBuf::assert(PathBuf::from(meta.target_directory.into_os_string()));
396 
397         CargoWorkspace { packages, targets, workspace_root, target_directory }
398     }
399 
packages(&self) -> impl Iterator<Item = Package> + ExactSizeIterator + '_400     pub fn packages(&self) -> impl Iterator<Item = Package> + ExactSizeIterator + '_ {
401         self.packages.iter().map(|(id, _pkg)| id)
402     }
403 
target_by_root(&self, root: &AbsPath) -> Option<Target>404     pub fn target_by_root(&self, root: &AbsPath) -> Option<Target> {
405         self.packages()
406             .filter(|&pkg| self[pkg].is_member)
407             .find_map(|pkg| self[pkg].targets.iter().find(|&&it| &self[it].root == root))
408             .copied()
409     }
410 
workspace_root(&self) -> &AbsPath411     pub fn workspace_root(&self) -> &AbsPath {
412         &self.workspace_root
413     }
414 
target_directory(&self) -> &AbsPath415     pub fn target_directory(&self) -> &AbsPath {
416         &self.target_directory
417     }
418 
package_flag(&self, package: &PackageData) -> String419     pub fn package_flag(&self, package: &PackageData) -> String {
420         if self.is_unique(&package.name) {
421             package.name.clone()
422         } else {
423             format!("{}:{}", package.name, package.version)
424         }
425     }
426 
parent_manifests(&self, manifest_path: &ManifestPath) -> Option<Vec<ManifestPath>>427     pub fn parent_manifests(&self, manifest_path: &ManifestPath) -> Option<Vec<ManifestPath>> {
428         let mut found = false;
429         let parent_manifests = self
430             .packages()
431             .filter_map(|pkg| {
432                 if !found && &self[pkg].manifest == manifest_path {
433                     found = true
434                 }
435                 self[pkg].dependencies.iter().find_map(|dep| {
436                     (&self[dep.pkg].manifest == manifest_path).then(|| self[pkg].manifest.clone())
437                 })
438             })
439             .collect::<Vec<ManifestPath>>();
440 
441         // some packages has this pkg as dep. return their manifests
442         if parent_manifests.len() > 0 {
443             return Some(parent_manifests);
444         }
445 
446         // this pkg is inside this cargo workspace, fallback to workspace root
447         if found {
448             return Some(vec![
449                 ManifestPath::try_from(self.workspace_root().join("Cargo.toml")).ok()?
450             ]);
451         }
452 
453         // not in this workspace
454         None
455     }
456 
457     /// Returns the union of the features of all member crates in this workspace.
workspace_features(&self) -> FxHashSet<String>458     pub fn workspace_features(&self) -> FxHashSet<String> {
459         self.packages()
460             .filter_map(|package| {
461                 let package = &self[package];
462                 if package.is_member {
463                     Some(package.features.keys().cloned())
464                 } else {
465                     None
466                 }
467             })
468             .flatten()
469             .collect()
470     }
471 
is_unique(&self, name: &str) -> bool472     fn is_unique(&self, name: &str) -> bool {
473         self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
474     }
475 }
476 
find_list_of_build_targets(config: &CargoConfig, cargo_toml: &ManifestPath) -> Vec<String>477 fn find_list_of_build_targets(config: &CargoConfig, cargo_toml: &ManifestPath) -> Vec<String> {
478     if let Some(target) = &config.target {
479         return [target.into()].to_vec();
480     }
481 
482     let build_targets = cargo_config_build_target(cargo_toml, &config.extra_env);
483     if !build_targets.is_empty() {
484         return build_targets;
485     }
486 
487     rustc_discover_host_triple(cargo_toml, &config.extra_env).into_iter().collect()
488 }
489 
rustc_discover_host_triple( cargo_toml: &ManifestPath, extra_env: &FxHashMap<String, String>, ) -> Option<String>490 fn rustc_discover_host_triple(
491     cargo_toml: &ManifestPath,
492     extra_env: &FxHashMap<String, String>,
493 ) -> Option<String> {
494     let mut rustc = Command::new(toolchain::rustc());
495     rustc.envs(extra_env);
496     rustc.current_dir(cargo_toml.parent()).arg("-vV");
497     tracing::debug!("Discovering host platform by {:?}", rustc);
498     match utf8_stdout(rustc) {
499         Ok(stdout) => {
500             let field = "host: ";
501             let target = stdout.lines().find_map(|l| l.strip_prefix(field));
502             if let Some(target) = target {
503                 Some(target.to_string())
504             } else {
505                 // If we fail to resolve the host platform, it's not the end of the world.
506                 tracing::info!("rustc -vV did not report host platform, got:\n{}", stdout);
507                 None
508             }
509         }
510         Err(e) => {
511             tracing::warn!("Failed to discover host platform: {}", e);
512             None
513         }
514     }
515 }
516 
cargo_config_build_target( cargo_toml: &ManifestPath, extra_env: &FxHashMap<String, String>, ) -> Vec<String>517 fn cargo_config_build_target(
518     cargo_toml: &ManifestPath,
519     extra_env: &FxHashMap<String, String>,
520 ) -> Vec<String> {
521     let mut cargo_config = Command::new(toolchain::cargo());
522     cargo_config.envs(extra_env);
523     cargo_config
524         .current_dir(cargo_toml.parent())
525         .args(["-Z", "unstable-options", "config", "get", "build.target"])
526         .env("RUSTC_BOOTSTRAP", "1");
527     // if successful we receive `build.target = "target-triple"`
528     // or `build.target = ["<target 1>", ..]`
529     tracing::debug!("Discovering cargo config target by {:?}", cargo_config);
530     utf8_stdout(cargo_config).map(parse_output_cargo_config_build_target).unwrap_or_default()
531 }
532 
parse_output_cargo_config_build_target(stdout: String) -> Vec<String>533 fn parse_output_cargo_config_build_target(stdout: String) -> Vec<String> {
534     let trimmed = stdout.trim_start_matches("build.target = ").trim_matches('"');
535 
536     if !trimmed.starts_with('[') {
537         return [trimmed.to_string()].to_vec();
538     }
539 
540     let res = serde_json::from_str(trimmed);
541     if let Err(e) = &res {
542         tracing::warn!("Failed to parse `build.target` as an array of target: {}`", e);
543     }
544     res.unwrap_or_default()
545 }
546