1 //! Handles lowering of build-system specific workspace information (`cargo
2 //! metadata` or `rust-project.json`) into representation stored in the salsa
3 //! database -- `CrateGraph`.
5 use std::{collections::VecDeque, convert::TryFrom, fmt, fs, process::Command};
7 use anyhow::{format_err, Context, Result};
9 CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacro,
11 use cfg::{CfgDiff, CfgOptions};
12 use paths::{AbsPath, AbsPathBuf};
13 use rustc_hash::{FxHashMap, FxHashSet};
17 build_scripts::BuildScriptOutput,
18 cargo_workspace::{DepKind, PackageData, RustcSource},
21 sysroot::SysrootCrate,
22 utf8_stdout, CargoConfig, CargoWorkspace, ManifestPath, ProjectJson, ProjectManifest, Sysroot,
23 TargetKind, WorkspaceBuildScripts,
26 /// A set of cfg-overrides per crate.
28 /// `Wildcard(..)` is useful e.g. disabling `#[cfg(test)]` on all crates,
29 /// without having to first obtain a list of all crates.
30 #[derive(Debug, Clone, Eq, PartialEq)]
31 pub enum CfgOverrides {
32 /// A single global set of overrides matching all crates.
34 /// A set of overrides matching specific crates.
35 Selective(FxHashMap<String, CfgDiff>),
38 impl Default for CfgOverrides {
39 fn default() -> Self {
40 Self::Selective(FxHashMap::default())
45 pub fn len(&self) -> usize {
47 CfgOverrides::Wildcard(_) => 1,
48 CfgOverrides::Selective(hash_map) => hash_map.len(),
53 /// `PackageRoot` describes a package root folder.
54 /// Which may be an external dependency, or a member of
55 /// the current workspace.
56 #[derive(Debug, Clone, Eq, PartialEq, Hash)]
57 pub struct PackageRoot {
58 /// Is from the local filesystem and may be edited
60 pub include: Vec<AbsPathBuf>,
61 pub exclude: Vec<AbsPathBuf>,
64 #[derive(Clone, Eq, PartialEq)]
65 pub enum ProjectWorkspace {
66 /// Project workspace was discovered by running `cargo metadata` and `rustc --print sysroot`.
68 cargo: CargoWorkspace,
69 build_scripts: WorkspaceBuildScripts,
70 sysroot: Option<Sysroot>,
71 rustc: Option<CargoWorkspace>,
72 /// Holds cfg flags for the current target. We get those by running
73 /// `rustc --print cfg`.
75 /// FIXME: make this a per-crate map, as, eg, build.rs might have a
77 rustc_cfg: Vec<CfgFlag>,
78 cfg_overrides: CfgOverrides,
80 /// Project workspace was manually specified using a `rust-project.json` file.
81 Json { project: ProjectJson, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
83 // FIXME: The primary limitation of this approach is that the set of detached files needs to be fixed at the beginning.
84 // That's not the end user experience we should strive for.
85 // Ideally, you should be able to just open a random detached file in existing cargo projects, and get the basic features working.
86 // That needs some changes on the salsa-level though.
87 // In particular, we should split the unified CrateGraph (which currently has maximal durability) into proper crate graph, and a set of ad hoc roots (with minimal durability).
88 // Then, we need to hide the graph behind the queries such that most queries look only at the proper crate graph, and fall back to ad hoc roots only if there's no results.
89 // After this, we should be able to tweak the logic in reload.rs to add newly opened files, which don't belong to any existing crates, to the set of the detached files.
91 /// Project with a set of disjoint files, not belonging to any particular workspace.
92 /// Backed by basic sysroot crates for basic completion and highlighting.
93 DetachedFiles { files: Vec<AbsPathBuf>, sysroot: Sysroot, rustc_cfg: Vec<CfgFlag> },
96 impl fmt::Debug for ProjectWorkspace {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 // Make sure this isn't too verbose.
100 ProjectWorkspace::Cargo {
108 .debug_struct("Cargo")
109 .field("root", &cargo.workspace_root().file_name())
110 .field("n_packages", &cargo.packages().len())
111 .field("sysroot", &sysroot.is_some())
113 "n_rustc_compiler_crates",
114 &rustc.as_ref().map_or(0, |rc| rc.packages().len()),
116 .field("n_rustc_cfg", &rustc_cfg.len())
117 .field("n_cfg_overrides", &cfg_overrides.len())
119 ProjectWorkspace::Json { project, sysroot, rustc_cfg } => {
120 let mut debug_struct = f.debug_struct("Json");
121 debug_struct.field("n_crates", &project.n_crates());
122 if let Some(sysroot) = sysroot {
123 debug_struct.field("n_sysroot_crates", &sysroot.crates().len());
125 debug_struct.field("n_rustc_cfg", &rustc_cfg.len());
126 debug_struct.finish()
128 ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => f
129 .debug_struct("DetachedFiles")
130 .field("n_files", &files.len())
131 .field("n_sysroot_crates", &sysroot.crates().len())
132 .field("n_rustc_cfg", &rustc_cfg.len())
138 impl ProjectWorkspace {
140 manifest: ProjectManifest,
141 config: &CargoConfig,
142 progress: &dyn Fn(String),
143 ) -> Result<ProjectWorkspace> {
144 let res = match manifest {
145 ProjectManifest::ProjectJson(project_json) => {
146 let file = fs::read_to_string(&project_json).with_context(|| {
147 format!("Failed to read json file {}", project_json.display())
149 let data = serde_json::from_str(&file).with_context(|| {
150 format!("Failed to deserialize json file {}", project_json.display())
152 let project_location = project_json.parent().to_path_buf();
153 let project_json = ProjectJson::new(&project_location, data);
154 ProjectWorkspace::load_inline(project_json, config.target.as_deref())?
156 ProjectManifest::CargoToml(cargo_toml) => {
157 let cargo_version = utf8_stdout({
158 let mut cmd = Command::new(toolchain::cargo());
159 cmd.arg("--version");
163 let meta = CargoWorkspace::fetch_metadata(&cargo_toml, config, progress)
166 "Failed to read Cargo metadata from Cargo.toml file {}, {}",
167 cargo_toml.display(),
171 let cargo = CargoWorkspace::new(meta);
173 let sysroot = if config.no_sysroot {
176 Some(Sysroot::discover(cargo_toml.parent()).with_context(|| {
178 "Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?",
184 let rustc_dir = match &config.rustc_source {
185 Some(RustcSource::Path(path)) => ManifestPath::try_from(path.clone()).ok(),
186 Some(RustcSource::Discover) => Sysroot::discover_rustc(&cargo_toml),
190 let rustc = match rustc_dir {
191 Some(rustc_dir) => Some({
192 let meta = CargoWorkspace::fetch_metadata(&rustc_dir, config, progress)
194 "Failed to read Cargo metadata for Rust sources".to_string()
196 CargoWorkspace::new(meta)
201 let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref());
203 let cfg_overrides = config.cfg_overrides();
204 ProjectWorkspace::Cargo {
206 build_scripts: WorkspaceBuildScripts::default(),
219 project_json: ProjectJson,
220 target: Option<&str>,
221 ) -> Result<ProjectWorkspace> {
222 let sysroot = match &project_json.sysroot_src {
223 Some(path) => Some(Sysroot::load(path.clone())?),
226 let rustc_cfg = rustc_cfg::get(None, target);
227 Ok(ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg })
230 pub fn load_detached_files(detached_files: Vec<AbsPathBuf>) -> Result<ProjectWorkspace> {
231 let sysroot = Sysroot::discover(
234 .and_then(|it| it.parent())
235 .ok_or_else(|| format_err!("No detached files to load"))?,
237 let rustc_cfg = rustc_cfg::get(None, None);
238 Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
241 pub fn run_build_scripts(
243 config: &CargoConfig,
244 progress: &dyn Fn(String),
245 ) -> Result<WorkspaceBuildScripts> {
247 ProjectWorkspace::Cargo { cargo, .. } => {
248 WorkspaceBuildScripts::run(config, cargo, progress)
250 ProjectWorkspace::Json { .. } | ProjectWorkspace::DetachedFiles { .. } => {
251 Ok(WorkspaceBuildScripts::default())
256 pub fn set_build_scripts(&mut self, bs: WorkspaceBuildScripts) {
258 ProjectWorkspace::Cargo { build_scripts, .. } => *build_scripts = bs,
260 always!(bs == WorkspaceBuildScripts::default());
265 /// Returns the roots for the current `ProjectWorkspace`
266 /// The return type contains the path and whether or not
267 /// the root is a member of the current workspace
268 pub fn to_roots(&self) -> Vec<PackageRoot> {
270 ProjectWorkspace::Json { project, sysroot, rustc_cfg: _ } => project
272 .map(|(_, krate)| PackageRoot {
273 is_local: krate.is_workspace_member,
274 include: krate.include.clone(),
275 exclude: krate.exclude.clone(),
277 .collect::<FxHashSet<_>>()
279 .chain(sysroot.as_ref().into_iter().flat_map(|sysroot| {
280 sysroot.crates().map(move |krate| PackageRoot {
282 include: vec![sysroot[krate].root.parent().to_path_buf()],
286 .collect::<Vec<_>>(),
287 ProjectWorkspace::Cargo {
298 let is_local = cargo[pkg].is_local;
299 let pkg_root = cargo[pkg].manifest.parent().to_path_buf();
301 let mut include = vec![pkg_root.clone()];
303 build_scripts.outputs.get(pkg).and_then(|it| it.out_dir.clone()),
306 // In case target's path is manually set in Cargo.toml to be
307 // outside the package root, add its parent as an extra include.
308 // An example of this situation would look like this:
312 // path = "../../src/lib.rs"
314 let extra_targets = cargo[pkg]
317 .filter(|&&tgt| cargo[tgt].kind == TargetKind::Lib)
318 .filter_map(|&tgt| cargo[tgt].root.parent())
319 .map(|tgt| tgt.normalize().to_path_buf())
320 .filter(|path| !path.starts_with(&pkg_root));
321 include.extend(extra_targets);
323 let mut exclude = vec![pkg_root.join(".git")];
325 exclude.push(pkg_root.join("target"));
327 exclude.push(pkg_root.join("tests"));
328 exclude.push(pkg_root.join("examples"));
329 exclude.push(pkg_root.join("benches"));
331 PackageRoot { is_local, include, exclude }
333 .chain(sysroot.iter().map(|sysroot| PackageRoot {
335 include: vec![sysroot.root().to_path_buf()],
338 .chain(rustc.iter().flat_map(|rustc| {
339 rustc.packages().map(move |krate| PackageRoot {
341 include: vec![rustc[krate].manifest.parent().to_path_buf()],
347 ProjectWorkspace::DetachedFiles { files, sysroot, .. } => files
349 .map(|detached_file| PackageRoot {
351 include: vec![detached_file.clone()],
354 .chain(sysroot.crates().map(|krate| PackageRoot {
356 include: vec![sysroot[krate].root.parent().to_path_buf()],
363 pub fn n_packages(&self) -> usize {
365 ProjectWorkspace::Json { project, .. } => project.n_crates(),
366 ProjectWorkspace::Cargo { cargo, sysroot, rustc, .. } => {
367 let rustc_package_len = rustc.as_ref().map_or(0, |it| it.packages().len());
368 let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
369 cargo.packages().len() + sysroot_package_len + rustc_package_len
371 ProjectWorkspace::DetachedFiles { sysroot, files, .. } => {
372 sysroot.crates().len() + files.len()
377 pub fn to_crate_graph(
379 load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
380 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
382 let _p = profile::span("ProjectWorkspace::to_crate_graph");
384 let mut crate_graph = match self {
385 ProjectWorkspace::Json { project, sysroot, rustc_cfg } => project_json_to_crate_graph(
392 ProjectWorkspace::Cargo {
399 } => cargo_to_crate_graph(
409 ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => {
410 detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot)
413 if crate_graph.patch_cfg_if() {
414 tracing::debug!("Patched std to depend on cfg-if")
416 tracing::debug!("Did not patch std to depend on cfg-if")
422 fn project_json_to_crate_graph(
423 rustc_cfg: Vec<CfgFlag>,
424 load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
425 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
426 project: &ProjectJson,
427 sysroot: &Option<Sysroot>,
429 let mut crate_graph = CrateGraph::default();
430 let sysroot_deps = sysroot
432 .map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load));
434 let mut cfg_cache: FxHashMap<&str, Vec<CfgFlag>> = FxHashMap::default();
435 let crates: FxHashMap<CrateId, CrateId> = project
437 .filter_map(|(crate_id, krate)| {
438 let file_path = &krate.root_module;
439 let file_id = load(file_path)?;
440 Some((crate_id, krate, file_id))
442 .map(|(crate_id, krate, file_id)| {
443 let env = krate.env.clone().into_iter().collect();
444 let proc_macro = krate.proc_macro_dylib_path.clone().map(|it| load_proc_macro(&it));
446 let target_cfgs = match krate.target.as_deref() {
448 cfg_cache.entry(target).or_insert_with(|| rustc_cfg::get(None, Some(target)))
453 let mut cfg_options = CfgOptions::default();
454 cfg_options.extend(target_cfgs.iter().chain(krate.cfg.iter()).cloned());
457 crate_graph.add_crate_root(
460 krate.display_name.clone(),
464 proc_macro.unwrap_or_default(),
470 for (from, krate) in project.crates() {
471 if let Some(&from) = crates.get(&from) {
472 if let Some((public_deps, libproc_macro)) = &sysroot_deps {
473 for (name, to) in public_deps.iter() {
474 add_dep(&mut crate_graph, from, name.clone(), *to)
476 if krate.is_proc_macro {
477 if let Some(proc_macro) = libproc_macro {
481 CrateName::new("proc_macro").unwrap(),
488 for dep in &krate.deps {
489 if let Some(&to) = crates.get(&dep.crate_id) {
490 add_dep(&mut crate_graph, from, dep.name.clone(), to)
498 fn cargo_to_crate_graph(
499 rustc_cfg: Vec<CfgFlag>,
500 override_cfg: &CfgOverrides,
501 load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
502 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
503 cargo: &CargoWorkspace,
504 build_scripts: &WorkspaceBuildScripts,
505 sysroot: Option<&Sysroot>,
506 rustc: &Option<CargoWorkspace>,
508 let _p = profile::span("cargo_to_crate_graph");
509 let mut crate_graph = CrateGraph::default();
510 let (public_deps, libproc_macro) = match sysroot {
511 Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
512 None => (Vec::new(), None),
515 let mut cfg_options = CfgOptions::default();
516 cfg_options.extend(rustc_cfg);
518 let mut pkg_to_lib_crate = FxHashMap::default();
520 // Add test cfg for non-sysroot crates
521 cfg_options.insert_atom("test".into());
522 cfg_options.insert_atom("debug_assertions".into());
524 let mut pkg_crates = FxHashMap::default();
525 // Does any crate signal to rust-analyzer that they need the rustc_private crates?
526 let mut has_private = false;
527 // Next, create crates for each package, target pair
528 for pkg in cargo.packages() {
529 let mut cfg_options = &cfg_options;
530 let mut replaced_cfg_options;
532 let overrides = match override_cfg {
533 CfgOverrides::Wildcard(cfg_diff) => Some(cfg_diff),
534 CfgOverrides::Selective(cfg_overrides) => cfg_overrides.get(&cargo[pkg].name),
537 if let Some(overrides) = overrides {
538 // FIXME: this is sort of a hack to deal with #![cfg(not(test))] vanishing such as seen
539 // in ed25519_dalek (#7243), and libcore (#9203) (although you only hit that one while
540 // working on rust-lang/rust as that's the only time it appears outside sysroot).
542 // A more ideal solution might be to reanalyze crates based on where the cursor is and
543 // figure out the set of cfgs that would have to apply to make it active.
545 replaced_cfg_options = cfg_options.clone();
546 replaced_cfg_options.apply_diff(overrides.clone());
547 cfg_options = &replaced_cfg_options;
550 has_private |= cargo[pkg].metadata.rustc_private;
551 let mut lib_tgt = None;
552 for &tgt in cargo[pkg].targets.iter() {
553 if let Some(file_id) = load(&cargo[tgt].root) {
554 let crate_id = add_target_crate_root(
557 build_scripts.outputs.get(pkg),
563 if cargo[tgt].kind == TargetKind::Lib {
564 lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
565 pkg_to_lib_crate.insert(pkg, crate_id);
567 if let Some(proc_macro) = libproc_macro {
571 CrateName::new("proc_macro").unwrap(),
576 pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, cargo[tgt].kind));
580 // Set deps to the core, std and to the lib target of the current package
581 for (from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
582 if let Some((to, name)) = lib_tgt.clone() {
583 if to != *from && *kind != TargetKind::BuildScript {
584 // (build script can not depend on its library target)
586 // For root projects with dashes in their name,
587 // cargo metadata does not do any normalization,
588 // so we do it ourselves currently
589 let name = CrateName::normalize_dashes(&name);
590 add_dep(&mut crate_graph, *from, name, to);
593 for (name, krate) in public_deps.iter() {
594 add_dep(&mut crate_graph, *from, name.clone(), *krate);
599 // Now add a dep edge from all targets of upstream to the lib
600 // target of downstream.
601 for pkg in cargo.packages() {
602 for dep in cargo[pkg].dependencies.iter() {
603 let name = CrateName::new(&dep.name).unwrap();
604 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
605 for (from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
606 if dep.kind == DepKind::Build && *kind != TargetKind::BuildScript {
607 // Only build scripts may depend on build dependencies.
610 if dep.kind != DepKind::Build && *kind == TargetKind::BuildScript {
611 // Build scripts may only depend on build dependencies.
615 add_dep(&mut crate_graph, *from, name.clone(), to)
622 // If the user provided a path to rustc sources, we add all the rustc_private crates
623 // and create dependencies on them for the crates which opt-in to that
624 if let Some(rustc_workspace) = rustc {
631 &mut pkg_to_lib_crate,
641 fn detached_files_to_crate_graph(
642 rustc_cfg: Vec<CfgFlag>,
643 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
644 detached_files: &[AbsPathBuf],
647 let _p = profile::span("detached_files_to_crate_graph");
648 let mut crate_graph = CrateGraph::default();
649 let (public_deps, _libproc_macro) =
650 sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load);
652 let mut cfg_options = CfgOptions::default();
653 cfg_options.extend(rustc_cfg);
655 for detached_file in detached_files {
656 let file_id = match load(detached_file) {
657 Some(file_id) => file_id,
659 tracing::error!("Failed to load detached file {:?}", detached_file);
663 let display_name = detached_file
665 .and_then(|os_str| os_str.to_str())
666 .map(|file_stem| CrateDisplayName::from_canonical_name(file_stem.to_string()));
667 let detached_file_crate = crate_graph.add_crate_root(
677 for (name, krate) in public_deps.iter() {
678 add_dep(&mut crate_graph, detached_file_crate, name.clone(), *krate);
684 fn handle_rustc_crates(
685 rustc_workspace: &CargoWorkspace,
686 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
687 crate_graph: &mut CrateGraph,
688 cfg_options: &CfgOptions,
689 load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
690 pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
691 public_deps: &[(CrateName, CrateId)],
692 cargo: &CargoWorkspace,
693 pkg_crates: &FxHashMap<la_arena::Idx<crate::PackageData>, Vec<(CrateId, TargetKind)>>,
695 let mut rustc_pkg_crates = FxHashMap::default();
696 // The root package of the rustc-dev component is rustc_driver, so we match that
698 rustc_workspace.packages().find(|package| rustc_workspace[*package].name == "rustc_driver");
699 // The rustc workspace might be incomplete (such as if rustc-dev is not
700 // installed for the current toolchain) and `rustcSource` is set to discover.
701 if let Some(root_pkg) = root_pkg {
702 // Iterate through every crate in the dependency subtree of rustc_driver using BFS
703 let mut queue = VecDeque::new();
704 queue.push_back(root_pkg);
705 while let Some(pkg) = queue.pop_front() {
706 // Don't duplicate packages if they are dependended on a diamond pattern
707 // N.B. if this line is ommitted, we try to analyse over 4_800_000 crates
708 // which is not ideal
709 if rustc_pkg_crates.contains_key(&pkg) {
712 for dep in &rustc_workspace[pkg].dependencies {
713 queue.push_back(dep.pkg);
715 for &tgt in rustc_workspace[pkg].targets.iter() {
716 if rustc_workspace[tgt].kind != TargetKind::Lib {
719 if let Some(file_id) = load(&rustc_workspace[tgt].root) {
720 let crate_id = add_target_crate_root(
722 &rustc_workspace[pkg],
727 &rustc_workspace[tgt].name,
729 pkg_to_lib_crate.insert(pkg, crate_id);
730 // Add dependencies on core / std / alloc for this crate
731 for (name, krate) in public_deps.iter() {
732 add_dep(crate_graph, crate_id, name.clone(), *krate);
734 rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
739 // Now add a dep edge from all targets of upstream to the lib
740 // target of downstream.
741 for pkg in rustc_pkg_crates.keys().copied() {
742 for dep in rustc_workspace[pkg].dependencies.iter() {
743 let name = CrateName::new(&dep.name).unwrap();
744 if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
745 for &from in rustc_pkg_crates.get(&pkg).into_iter().flatten() {
746 add_dep(crate_graph, from, name.clone(), to);
751 // Add a dependency on the rustc_private crates for all targets of each package
753 for dep in rustc_workspace.packages() {
754 let name = CrateName::normalize_dashes(&rustc_workspace[dep].name);
756 if let Some(&to) = pkg_to_lib_crate.get(&dep) {
757 for pkg in cargo.packages() {
758 let package = &cargo[pkg];
759 if !package.metadata.rustc_private {
762 for (from, _) in pkg_crates.get(&pkg).into_iter().flatten() {
763 // Avoid creating duplicate dependencies
764 // This avoids the situation where `from` depends on e.g. `arrayvec`, but
765 // `rust_analyzer` thinks that it should use the one from the `rustcSource`
766 // instead of the one from `crates.io`
767 if !crate_graph[*from].dependencies.iter().any(|d| d.name == name) {
768 add_dep(crate_graph, *from, name.clone(), to);
776 fn add_target_crate_root(
777 crate_graph: &mut CrateGraph,
779 build_data: Option<&BuildScriptOutput>,
780 cfg_options: &CfgOptions,
781 load_proc_macro: &mut dyn FnMut(&AbsPath) -> Vec<ProcMacro>,
785 let edition = pkg.edition;
787 let mut opts = cfg_options.clone();
788 for feature in pkg.active_features.iter() {
789 opts.insert_key_value("feature".into(), feature.into());
791 if let Some(cfgs) = build_data.as_ref().map(|it| &it.cfgs) {
792 opts.extend(cfgs.iter().cloned());
797 let mut env = Env::default();
798 inject_cargo_env(pkg, &mut env);
800 if let Some(envs) = build_data.map(|it| &it.envs) {
802 env.set(k, v.clone());
806 let proc_macro = build_data
808 .and_then(|it| it.proc_macro_dylib_path.as_ref())
809 .map(|it| load_proc_macro(it))
810 .unwrap_or_default();
812 let display_name = CrateDisplayName::from_canonical_name(cargo_name.to_string());
813 let mut potential_cfg_options = cfg_options.clone();
814 potential_cfg_options.extend(
817 .map(|feat| CfgFlag::KeyValue { key: "feature".into(), value: feat.0.into() }),
820 crate_graph.add_crate_root(
825 potential_cfg_options,
831 fn sysroot_to_crate_graph(
832 crate_graph: &mut CrateGraph,
834 rustc_cfg: Vec<CfgFlag>,
835 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
836 ) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) {
837 let _p = profile::span("sysroot_to_crate_graph");
838 let mut cfg_options = CfgOptions::default();
839 cfg_options.extend(rustc_cfg);
840 let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = sysroot
842 .filter_map(|krate| {
843 let file_id = load(&sysroot[krate].root)?;
845 let env = Env::default();
846 let proc_macro = vec![];
847 let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
848 let crate_id = crate_graph.add_crate_root(
857 Some((krate, crate_id))
861 for from in sysroot.crates() {
862 for &to in sysroot[from].deps.iter() {
863 let name = CrateName::new(&sysroot[to].name).unwrap();
864 if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) {
865 add_dep(crate_graph, from, name, to);
870 let public_deps = sysroot
872 .map(|(name, idx)| (CrateName::new(name).unwrap(), sysroot_crates[&idx]))
873 .collect::<Vec<_>>();
875 let libproc_macro = sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied());
876 (public_deps, libproc_macro)
879 fn add_dep(graph: &mut CrateGraph, from: CrateId, name: CrateName, to: CrateId) {
880 if let Err(err) = graph.add_dep(from, Dependency::new(name, to)) {
881 tracing::error!("{}", err)
885 /// Recreates the compile-time environment variables that Cargo sets.
887 /// Should be synced with
888 /// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
890 /// FIXME: ask Cargo to provide this data instead of re-deriving.
891 fn inject_cargo_env(package: &PackageData, env: &mut Env) {
892 // FIXME: Missing variables:
893 // CARGO_BIN_NAME, CARGO_BIN_EXE_<name>
895 let manifest_dir = package.manifest.parent();
896 env.set("CARGO_MANIFEST_DIR", manifest_dir.as_os_str().to_string_lossy().into_owned());
898 // Not always right, but works for common cases.
899 env.set("CARGO", "cargo".into());
901 env.set("CARGO_PKG_VERSION", package.version.to_string());
902 env.set("CARGO_PKG_VERSION_MAJOR", package.version.major.to_string());
903 env.set("CARGO_PKG_VERSION_MINOR", package.version.minor.to_string());
904 env.set("CARGO_PKG_VERSION_PATCH", package.version.patch.to_string());
905 env.set("CARGO_PKG_VERSION_PRE", package.version.pre.to_string());
907 env.set("CARGO_PKG_AUTHORS", String::new());
909 env.set("CARGO_PKG_NAME", package.name.clone());
910 // FIXME: This isn't really correct (a package can have many crates with different names), but
911 // it's better than leaving the variable unset.
912 env.set("CARGO_CRATE_NAME", CrateName::normalize_dashes(&package.name).to_string());
913 env.set("CARGO_PKG_DESCRIPTION", String::new());
914 env.set("CARGO_PKG_HOMEPAGE", String::new());
915 env.set("CARGO_PKG_REPOSITORY", String::new());
916 env.set("CARGO_PKG_LICENSE", String::new());
918 env.set("CARGO_PKG_LICENSE_FILE", String::new());