project_model/
sysroot.rs

1//! Loads "sysroot" crate.
2//!
3//! One confusing point here is that normally sysroot is a bunch of `.rlib`s,
4//! but we can't process `.rlib` and need source code instead. The source code
5//! is typically installed with `rustup component add rust-src` command.
6
7use core::fmt;
8use std::{env, fs, ops::Not, path::Path, process::Command};
9
10use anyhow::{Result, format_err};
11use base_db::Env;
12use itertools::Itertools;
13use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
14use rustc_hash::FxHashMap;
15use stdx::format_to;
16use toolchain::{Tool, probe_for_binary};
17
18use crate::{
19    CargoWorkspace, ManifestPath, ProjectJson, RustSourceWorkspaceConfig,
20    cargo_workspace::{CargoMetadataConfig, FetchMetadata},
21    utf8_stdout,
22};
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct Sysroot {
26    root: Option<AbsPathBuf>,
27    rust_lib_src_root: Option<AbsPathBuf>,
28    workspace: RustLibSrcWorkspace,
29    error: Option<String>,
30}
31
32#[derive(Debug, Clone, Eq, PartialEq)]
33pub enum RustLibSrcWorkspace {
34    Workspace { ws: CargoWorkspace, metadata_err: Option<String> },
35    Json(ProjectJson),
36    Stitched(stitched::Stitched),
37    Empty,
38}
39
40impl fmt::Display for RustLibSrcWorkspace {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            RustLibSrcWorkspace::Workspace { ws, .. } => {
44                write!(f, "workspace {}", ws.workspace_root())
45            }
46            RustLibSrcWorkspace::Json(json) => write!(f, "json {}", json.manifest_or_root()),
47            RustLibSrcWorkspace::Stitched(stitched) => {
48                write!(f, "stitched with {} crates", stitched.crates.len())
49            }
50            RustLibSrcWorkspace::Empty => write!(f, "empty"),
51        }
52    }
53}
54
55impl Sysroot {
56    pub const fn empty() -> Sysroot {
57        Sysroot {
58            root: None,
59            rust_lib_src_root: None,
60            workspace: RustLibSrcWorkspace::Empty,
61            error: None,
62        }
63    }
64
65    /// Returns sysroot "root" directory, where `bin/`, `etc/`, `lib/`, `libexec/`
66    /// subfolder live, like:
67    /// `$HOME/.rustup/toolchains/nightly-2022-07-23-x86_64-unknown-linux-gnu`
68    pub fn root(&self) -> Option<&AbsPath> {
69        self.root.as_deref()
70    }
71
72    /// Returns the sysroot "source" directory, where stdlib sources are located, like:
73    /// `$HOME/.rustup/toolchains/nightly-2022-07-23-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library`
74    pub fn rust_lib_src_root(&self) -> Option<&AbsPath> {
75        self.rust_lib_src_root.as_deref()
76    }
77
78    pub fn is_rust_lib_src_empty(&self) -> bool {
79        match &self.workspace {
80            RustLibSrcWorkspace::Workspace { ws, .. } => ws.packages().next().is_none(),
81            RustLibSrcWorkspace::Json(project_json) => project_json.n_crates() == 0,
82            RustLibSrcWorkspace::Stitched(stitched) => stitched.crates.is_empty(),
83            RustLibSrcWorkspace::Empty => true,
84        }
85    }
86
87    pub fn error(&self) -> Option<&str> {
88        self.error.as_deref()
89    }
90
91    pub fn metadata_error(&self) -> Option<&str> {
92        match &self.workspace {
93            RustLibSrcWorkspace::Workspace { metadata_err, .. } => metadata_err.as_deref(),
94            _ => None,
95        }
96    }
97
98    pub fn num_packages(&self) -> usize {
99        match &self.workspace {
100            RustLibSrcWorkspace::Workspace { ws, .. } => ws.packages().count(),
101            RustLibSrcWorkspace::Json(project_json) => project_json.n_crates(),
102            RustLibSrcWorkspace::Stitched(stitched) => stitched.crates.len(),
103            RustLibSrcWorkspace::Empty => 0,
104        }
105    }
106
107    pub(crate) fn workspace(&self) -> &RustLibSrcWorkspace {
108        &self.workspace
109    }
110}
111
112impl Sysroot {
113    /// Attempts to discover the toolchain's sysroot from the given `dir`.
114    pub fn discover(dir: &AbsPath, extra_env: &FxHashMap<String, Option<String>>) -> Sysroot {
115        let sysroot_dir = discover_sysroot_dir(dir, extra_env);
116        let rust_lib_src_dir = sysroot_dir.as_ref().ok().map(|sysroot_dir| {
117            discover_rust_lib_src_dir_or_add_component(sysroot_dir, dir, extra_env)
118        });
119        Sysroot::assemble(Some(sysroot_dir), rust_lib_src_dir)
120    }
121
122    pub fn discover_with_src_override(
123        current_dir: &AbsPath,
124        extra_env: &FxHashMap<String, Option<String>>,
125        rust_lib_src_dir: AbsPathBuf,
126    ) -> Sysroot {
127        let sysroot_dir = discover_sysroot_dir(current_dir, extra_env);
128        Sysroot::assemble(Some(sysroot_dir), Some(Ok(rust_lib_src_dir)))
129    }
130
131    pub fn discover_rust_lib_src_dir(sysroot_dir: AbsPathBuf) -> Sysroot {
132        let rust_lib_src_dir = discover_rust_lib_src_dir(&sysroot_dir)
133            .ok_or_else(|| format_err!("can't find standard library sources in {sysroot_dir}"));
134        Sysroot::assemble(Some(Ok(sysroot_dir)), Some(rust_lib_src_dir))
135    }
136
137    pub fn discover_rustc_src(&self) -> Option<ManifestPath> {
138        get_rustc_src(self.root()?)
139    }
140
141    pub fn new(sysroot_dir: Option<AbsPathBuf>, rust_lib_src_dir: Option<AbsPathBuf>) -> Sysroot {
142        Self::assemble(sysroot_dir.map(Ok), rust_lib_src_dir.map(Ok))
143    }
144
145    /// Returns a command to run a tool preferring the cargo proxies if the sysroot exists.
146    pub fn tool(
147        &self,
148        tool: Tool,
149        current_dir: impl AsRef<Path>,
150        envs: &FxHashMap<String, Option<String>>,
151    ) -> Command {
152        match self.root() {
153            Some(root) => {
154                // special case rustc, we can look that up directly in the sysroot's bin folder
155                // as it should never invoke another cargo binary
156                if let Tool::Rustc = tool
157                    && let Some(path) =
158                        probe_for_binary(root.join("bin").join(Tool::Rustc.name()).into())
159                {
160                    return toolchain::command(path, current_dir, envs);
161                }
162
163                let mut cmd = toolchain::command(tool.prefer_proxy(), current_dir, envs);
164                if !envs.contains_key("RUSTUP_TOOLCHAIN")
165                    && std::env::var_os("RUSTUP_TOOLCHAIN").is_none()
166                {
167                    cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(root));
168                }
169
170                cmd
171            }
172            _ => toolchain::command(tool.path(), current_dir, envs),
173        }
174    }
175
176    pub fn tool_path(&self, tool: Tool, current_dir: impl AsRef<Path>, envs: &Env) -> Utf8PathBuf {
177        match self.root() {
178            Some(root) => {
179                let mut cmd = toolchain::command(
180                    Tool::Rustup.path(),
181                    current_dir,
182                    &envs
183                        .into_iter()
184                        .map(|(k, v)| (k.clone(), Some(v.clone())))
185                        .collect::<FxHashMap<_, _>>(),
186                );
187                if !envs.contains_key("RUSTUP_TOOLCHAIN")
188                    && std::env::var_os("RUSTUP_TOOLCHAIN").is_none()
189                {
190                    cmd.env("RUSTUP_TOOLCHAIN", AsRef::<std::path::Path>::as_ref(root));
191                }
192
193                cmd.arg("which");
194                cmd.arg(tool.name());
195                (|| {
196                    Some(Utf8PathBuf::from(
197                        String::from_utf8(cmd.output().ok()?.stdout).ok()?.trim_end(),
198                    ))
199                })()
200                .unwrap_or_else(|| Utf8PathBuf::from(tool.name()))
201            }
202            _ => tool.path(),
203        }
204    }
205
206    pub fn discover_proc_macro_srv(&self) -> Option<anyhow::Result<AbsPathBuf>> {
207        let root = self.root()?;
208        Some(
209            ["libexec", "lib"]
210                .into_iter()
211                .map(|segment| root.join(segment).join("rust-analyzer-proc-macro-srv"))
212                .find_map(|server_path| probe_for_binary(server_path.into()))
213                .map(AbsPathBuf::assert)
214                .ok_or_else(|| {
215                    anyhow::format_err!("cannot find proc-macro server in sysroot `{}`", root)
216                }),
217        )
218    }
219
220    fn assemble(
221        sysroot_dir: Option<Result<AbsPathBuf, anyhow::Error>>,
222        rust_lib_src_dir: Option<Result<AbsPathBuf, anyhow::Error>>,
223    ) -> Sysroot {
224        let mut errors = String::new();
225        let root = match sysroot_dir {
226            Some(Ok(sysroot_dir)) => Some(sysroot_dir),
227            Some(Err(e)) => {
228                format_to!(errors, "{e}\n");
229                None
230            }
231            None => None,
232        };
233        let rust_lib_src_root = match rust_lib_src_dir {
234            Some(Ok(rust_lib_src_dir)) => Some(rust_lib_src_dir),
235            Some(Err(e)) => {
236                format_to!(errors, "{e}\n");
237                None
238            }
239            None => None,
240        };
241        Sysroot {
242            root,
243            rust_lib_src_root,
244            workspace: RustLibSrcWorkspace::Empty,
245            error: errors.is_empty().not().then_some(errors),
246        }
247    }
248
249    pub fn load_workspace(
250        &self,
251        sysroot_source_config: &RustSourceWorkspaceConfig,
252        no_deps: bool,
253        progress: &dyn Fn(String),
254    ) -> Option<RustLibSrcWorkspace> {
255        assert!(matches!(self.workspace, RustLibSrcWorkspace::Empty), "workspace already loaded");
256        let Self { root: _, rust_lib_src_root: Some(src_root), workspace: _, error: _ } = self
257        else {
258            return None;
259        };
260        if let RustSourceWorkspaceConfig::CargoMetadata(cargo_config) = sysroot_source_config {
261            let library_manifest = ManifestPath::try_from(src_root.join("Cargo.toml")).unwrap();
262            if fs::metadata(&library_manifest).is_ok() {
263                match self.load_library_via_cargo(
264                    &library_manifest,
265                    src_root,
266                    cargo_config,
267                    no_deps,
268                    progress,
269                ) {
270                    Ok(loaded) => return Some(loaded),
271                    Err(e) => {
272                        tracing::error!("`cargo metadata` failed on `{library_manifest}` : {e}")
273                    }
274                }
275            }
276            tracing::debug!("Stitching sysroot library: {src_root}");
277
278            let mut stitched = stitched::Stitched { crates: Default::default() };
279
280            for path in stitched::SYSROOT_CRATES.trim().lines() {
281                let name = path.split('/').next_back().unwrap();
282                let root = [format!("{path}/src/lib.rs"), format!("lib{path}/lib.rs")]
283                    .into_iter()
284                    .map(|it| src_root.join(it))
285                    .filter_map(|it| ManifestPath::try_from(it).ok())
286                    .find(|it| fs::metadata(it).is_ok());
287
288                if let Some(root) = root {
289                    stitched.crates.alloc(stitched::RustLibSrcCrateData {
290                        name: name.into(),
291                        root,
292                        deps: Vec::new(),
293                    });
294                }
295            }
296
297            if let Some(std) = stitched.by_name("std") {
298                for dep in stitched::STD_DEPS.trim().lines() {
299                    if let Some(dep) = stitched.by_name(dep) {
300                        stitched.crates[std].deps.push(dep)
301                    }
302                }
303            }
304
305            if let Some(alloc) = stitched.by_name("alloc") {
306                for dep in stitched::ALLOC_DEPS.trim().lines() {
307                    if let Some(dep) = stitched.by_name(dep) {
308                        stitched.crates[alloc].deps.push(dep)
309                    }
310                }
311            }
312
313            if let Some(proc_macro) = stitched.by_name("proc_macro") {
314                for dep in stitched::PROC_MACRO_DEPS.trim().lines() {
315                    if let Some(dep) = stitched.by_name(dep) {
316                        stitched.crates[proc_macro].deps.push(dep)
317                    }
318                }
319            }
320            return Some(RustLibSrcWorkspace::Stitched(stitched));
321        } else if let RustSourceWorkspaceConfig::Json(project_json) = sysroot_source_config {
322            return Some(RustLibSrcWorkspace::Json(project_json.clone()));
323        }
324
325        None
326    }
327
328    pub fn set_workspace(&mut self, workspace: RustLibSrcWorkspace) {
329        self.workspace = workspace;
330        if self.error.is_none()
331            && let Some(src_root) = &self.rust_lib_src_root
332        {
333            let has_core = match &self.workspace {
334                RustLibSrcWorkspace::Workspace { ws: workspace, .. } => {
335                    workspace.packages().any(|p| workspace[p].name == "core")
336                }
337                RustLibSrcWorkspace::Json(project_json) => project_json
338                    .crates()
339                    .filter_map(|(_, krate)| krate.display_name.clone())
340                    .any(|name| name.canonical_name().as_str() == "core"),
341                RustLibSrcWorkspace::Stitched(stitched) => stitched.by_name("core").is_some(),
342                RustLibSrcWorkspace::Empty => true,
343            };
344            if !has_core {
345                let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
346                    " (env var `RUST_SRC_PATH` is set and may be incorrect, try unsetting it)"
347                } else {
348                    ", try running `rustup component add rust-src` to possibly fix this"
349                };
350                self.error =
351                    Some(format!("sysroot at `{src_root}` is missing a `core` library{var_note}",));
352            }
353        }
354    }
355
356    fn load_library_via_cargo(
357        &self,
358        library_manifest: &ManifestPath,
359        current_dir: &AbsPath,
360        cargo_config: &CargoMetadataConfig,
361        no_deps: bool,
362        progress: &dyn Fn(String),
363    ) -> Result<RustLibSrcWorkspace> {
364        tracing::debug!("Loading library metadata: {library_manifest}");
365        let mut cargo_config = cargo_config.clone();
366        // the sysroot uses `public-dependency`, so we make cargo think it's a nightly
367        cargo_config.extra_env.insert(
368            "__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS".to_owned(),
369            Some("nightly".to_owned()),
370        );
371
372        // Make sure we never attempt to write to the sysroot
373        let locked = true;
374        let (mut res, err) =
375            FetchMetadata::new(library_manifest, current_dir, &cargo_config, self, no_deps)
376                .exec(locked, progress)?;
377
378        // Patch out `rustc-std-workspace-*` crates to point to the real crates.
379        // This is done prior to `CrateGraph` construction to prevent de-duplication logic from failing.
380        let patches = {
381            let mut fake_core = None;
382            let mut fake_alloc = None;
383            let mut fake_std = None;
384            let mut real_core = None;
385            let mut real_alloc = None;
386            let mut real_std = None;
387            res.packages.iter().enumerate().for_each(|(idx, package)| {
388                match package.name.strip_prefix("rustc-std-workspace-") {
389                    Some("core") => fake_core = Some((idx, package.id.clone())),
390                    Some("alloc") => fake_alloc = Some((idx, package.id.clone())),
391                    Some("std") => fake_std = Some((idx, package.id.clone())),
392                    Some(_) => {
393                        tracing::warn!("unknown rustc-std-workspace-* crate: {}", package.name)
394                    }
395                    None => match &**package.name {
396                        "core" => real_core = Some(package.id.clone()),
397                        "alloc" => real_alloc = Some(package.id.clone()),
398                        "std" => real_std = Some(package.id.clone()),
399                        _ => (),
400                    },
401                }
402            });
403
404            [fake_core.zip(real_core), fake_alloc.zip(real_alloc), fake_std.zip(real_std)]
405                .into_iter()
406                .flatten()
407        };
408
409        if let Some(resolve) = res.resolve.as_mut() {
410            resolve.nodes.retain_mut(|node| {
411                // Replace `rustc-std-workspace` crate with the actual one in the dependency list
412                node.deps.iter_mut().for_each(|dep| {
413                    let real_pkg = patches.clone().find(|((_, fake_id), _)| *fake_id == dep.pkg);
414                    if let Some((_, real)) = real_pkg {
415                        dep.pkg = real;
416                    }
417                });
418                // Remove this node if it's a fake one
419                !patches.clone().any(|((_, fake), _)| fake == node.id)
420            });
421        }
422        // Remove the fake ones from the package list
423        patches.map(|((idx, _), _)| idx).sorted().rev().for_each(|idx| {
424            res.packages.remove(idx);
425        });
426
427        let cargo_workspace =
428            CargoWorkspace::new(res, library_manifest.clone(), Default::default(), true);
429        Ok(RustLibSrcWorkspace::Workspace {
430            ws: cargo_workspace,
431            metadata_err: err.map(|e| format!("{e:#}")),
432        })
433    }
434}
435
436fn discover_sysroot_dir(
437    current_dir: &AbsPath,
438    extra_env: &FxHashMap<String, Option<String>>,
439) -> Result<AbsPathBuf> {
440    let mut rustc = toolchain::command(Tool::Rustc.path(), current_dir, extra_env);
441    rustc.current_dir(current_dir).args(["--print", "sysroot"]);
442    tracing::debug!("Discovering sysroot by {:?}", rustc);
443    let stdout = utf8_stdout(&mut rustc)?;
444    Ok(AbsPathBuf::assert(Utf8PathBuf::from(stdout)))
445}
446
447fn discover_rust_lib_src_dir(sysroot_path: &AbsPathBuf) -> Option<AbsPathBuf> {
448    if let Ok(path) = env::var("RUST_SRC_PATH") {
449        if let Ok(path) = AbsPathBuf::try_from(path.as_str()) {
450            let core = path.join("core");
451            if fs::metadata(&core).is_ok() {
452                tracing::debug!("Discovered sysroot by RUST_SRC_PATH: {path}");
453                return Some(path);
454            }
455            tracing::debug!("RUST_SRC_PATH is set, but is invalid (no core: {core:?}), ignoring");
456        } else {
457            tracing::debug!("RUST_SRC_PATH is set, but is invalid, ignoring");
458        }
459    }
460
461    get_rust_lib_src(sysroot_path)
462}
463
464fn discover_rust_lib_src_dir_or_add_component(
465    sysroot_path: &AbsPathBuf,
466    current_dir: &AbsPath,
467    extra_env: &FxHashMap<String, Option<String>>,
468) -> Result<AbsPathBuf> {
469    discover_rust_lib_src_dir(sysroot_path)
470        .or_else(|| {
471            let mut rustup = toolchain::command(Tool::Rustup.prefer_proxy(), current_dir, extra_env);
472            rustup.args(["component", "add", "rust-src"]);
473            tracing::info!("adding rust-src component by {:?}", rustup);
474            utf8_stdout(&mut rustup).ok()?;
475            get_rust_lib_src(sysroot_path)
476        })
477        .ok_or_else(|| {
478            tracing::error!(%sysroot_path, "can't load standard library, try installing `rust-src`");
479            format_err!(
480                "\
481can't load standard library from sysroot
482{sysroot_path}
483(discovered via `rustc --print sysroot`)
484try installing `rust-src` the same way you installed `rustc`"
485            )
486        })
487}
488
489fn get_rustc_src(sysroot_path: &AbsPath) -> Option<ManifestPath> {
490    let rustc_src = sysroot_path.join("lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml");
491    let rustc_src = ManifestPath::try_from(rustc_src).ok()?;
492    tracing::debug!("checking for rustc source code: {rustc_src}");
493    if fs::metadata(&rustc_src).is_ok() { Some(rustc_src) } else { None }
494}
495
496fn get_rust_lib_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
497    let rust_lib_src = sysroot_path.join("lib/rustlib/src/rust/library");
498    tracing::debug!("checking sysroot library: {rust_lib_src}");
499    if fs::metadata(&rust_lib_src).is_ok() { Some(rust_lib_src) } else { None }
500}
501
502// FIXME: Remove this, that will bump our project MSRV to 1.82
503pub(crate) mod stitched {
504    use std::ops;
505
506    use base_db::CrateName;
507    use la_arena::{Arena, Idx};
508
509    use crate::ManifestPath;
510
511    #[derive(Debug, Clone, Eq, PartialEq)]
512    pub struct Stitched {
513        pub(super) crates: Arena<RustLibSrcCrateData>,
514    }
515
516    impl ops::Index<RustLibSrcCrate> for Stitched {
517        type Output = RustLibSrcCrateData;
518        fn index(&self, index: RustLibSrcCrate) -> &RustLibSrcCrateData {
519            &self.crates[index]
520        }
521    }
522
523    impl Stitched {
524        pub(crate) fn public_deps(
525            &self,
526        ) -> impl Iterator<Item = (CrateName, RustLibSrcCrate, bool)> + '_ {
527            // core is added as a dependency before std in order to
528            // mimic rustcs dependency order
529            [("core", true), ("alloc", false), ("std", true), ("test", false)]
530                .into_iter()
531                .filter_map(move |(name, prelude)| {
532                    Some((CrateName::new(name).unwrap(), self.by_name(name)?, prelude))
533                })
534        }
535
536        pub(crate) fn proc_macro(&self) -> Option<RustLibSrcCrate> {
537            self.by_name("proc_macro")
538        }
539
540        pub(crate) fn crates(&self) -> impl ExactSizeIterator<Item = RustLibSrcCrate> + '_ {
541            self.crates.iter().map(|(id, _data)| id)
542        }
543
544        pub(super) fn by_name(&self, name: &str) -> Option<RustLibSrcCrate> {
545            let (id, _data) = self.crates.iter().find(|(_id, data)| data.name == name)?;
546            Some(id)
547        }
548    }
549
550    pub(crate) type RustLibSrcCrate = Idx<RustLibSrcCrateData>;
551
552    #[derive(Debug, Clone, Eq, PartialEq)]
553    pub(crate) struct RustLibSrcCrateData {
554        pub(crate) name: String,
555        pub(crate) root: ManifestPath,
556        pub(crate) deps: Vec<RustLibSrcCrate>,
557    }
558
559    pub(super) const SYSROOT_CRATES: &str = "
560alloc
561backtrace
562core
563panic_abort
564panic_unwind
565proc_macro
566profiler_builtins
567std
568stdarch/crates/std_detect
569test
570unwind";
571
572    pub(super) const ALLOC_DEPS: &str = "core";
573
574    pub(super) const STD_DEPS: &str = "
575alloc
576panic_unwind
577panic_abort
578core
579profiler_builtins
580unwind
581std_detect
582test";
583
584    // core is required for our builtin derives to work in the proc_macro lib currently
585    pub(super) const PROC_MACRO_DEPS: &str = "
586std
587core";
588}