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 {
279                crates: Default::default(),
280                edition: span::Edition::Edition2024,
281            };
282
283            for path in stitched::SYSROOT_CRATES.trim().lines() {
284                let name = path.split('/').next_back().unwrap();
285                let root = [format!("{path}/src/lib.rs"), format!("lib{path}/lib.rs")]
286                    .into_iter()
287                    .map(|it| src_root.join(it))
288                    .filter_map(|it| ManifestPath::try_from(it).ok())
289                    .find(|it| fs::metadata(it).is_ok());
290
291                if let Some(root) = root {
292                    stitched.crates.alloc(stitched::RustLibSrcCrateData {
293                        name: name.into(),
294                        root,
295                        deps: Vec::new(),
296                    });
297                }
298            }
299
300            if let Some(std) = stitched.by_name("std") {
301                for dep in stitched::STD_DEPS.trim().lines() {
302                    if let Some(dep) = stitched.by_name(dep) {
303                        stitched.crates[std].deps.push(dep)
304                    }
305                }
306            }
307
308            if let Some(alloc) = stitched.by_name("alloc") {
309                for dep in stitched::ALLOC_DEPS.trim().lines() {
310                    if let Some(dep) = stitched.by_name(dep) {
311                        stitched.crates[alloc].deps.push(dep)
312                    }
313                }
314            }
315
316            if let Some(proc_macro) = stitched.by_name("proc_macro") {
317                for dep in stitched::PROC_MACRO_DEPS.trim().lines() {
318                    if let Some(dep) = stitched.by_name(dep) {
319                        stitched.crates[proc_macro].deps.push(dep)
320                    }
321                }
322            }
323            return Some(RustLibSrcWorkspace::Stitched(stitched));
324        } else if let RustSourceWorkspaceConfig::Json(project_json) = sysroot_source_config {
325            return Some(RustLibSrcWorkspace::Json(project_json.clone()));
326        }
327
328        None
329    }
330
331    pub fn set_workspace(&mut self, workspace: RustLibSrcWorkspace) {
332        self.workspace = workspace;
333        if self.error.is_none()
334            && let Some(src_root) = &self.rust_lib_src_root
335        {
336            let has_core = match &self.workspace {
337                RustLibSrcWorkspace::Workspace { ws: workspace, .. } => {
338                    workspace.packages().any(|p| workspace[p].name == "core")
339                }
340                RustLibSrcWorkspace::Json(project_json) => project_json
341                    .crates()
342                    .filter_map(|(_, krate)| krate.display_name.clone())
343                    .any(|name| name.canonical_name().as_str() == "core"),
344                RustLibSrcWorkspace::Stitched(stitched) => stitched.by_name("core").is_some(),
345                RustLibSrcWorkspace::Empty => true,
346            };
347            if !has_core {
348                let var_note = if env::var_os("RUST_SRC_PATH").is_some() {
349                    " (env var `RUST_SRC_PATH` is set and may be incorrect, try unsetting it)"
350                } else {
351                    ", try running `rustup component add rust-src` to possibly fix this"
352                };
353                self.error =
354                    Some(format!("sysroot at `{src_root}` is missing a `core` library{var_note}",));
355            }
356        }
357    }
358
359    fn load_library_via_cargo(
360        &self,
361        library_manifest: &ManifestPath,
362        current_dir: &AbsPath,
363        cargo_config: &CargoMetadataConfig,
364        no_deps: bool,
365        progress: &dyn Fn(String),
366    ) -> Result<RustLibSrcWorkspace> {
367        tracing::debug!("Loading library metadata: {library_manifest}");
368        let mut cargo_config = cargo_config.clone();
369        // the sysroot uses `public-dependency`, so we make cargo think it's a nightly
370        cargo_config.extra_env.insert(
371            "__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS".to_owned(),
372            Some("nightly".to_owned()),
373        );
374
375        // Make sure we never attempt to write to the sysroot
376        let locked = true;
377        let (mut res, err) =
378            FetchMetadata::new(library_manifest, current_dir, &cargo_config, self, no_deps)
379                .exec(locked, progress)?;
380
381        // Patch out `rustc-std-workspace-*` crates to point to the real crates.
382        // This is done prior to `CrateGraph` construction to prevent de-duplication logic from failing.
383        let patches = {
384            let mut fake_core = None;
385            let mut fake_alloc = None;
386            let mut fake_std = None;
387            let mut real_core = None;
388            let mut real_alloc = None;
389            let mut real_std = None;
390            res.packages.iter().enumerate().for_each(|(idx, package)| {
391                match package.name.strip_prefix("rustc-std-workspace-") {
392                    Some("core") => fake_core = Some((idx, package.id.clone())),
393                    Some("alloc") => fake_alloc = Some((idx, package.id.clone())),
394                    Some("std") => fake_std = Some((idx, package.id.clone())),
395                    Some(_) => {
396                        tracing::warn!("unknown rustc-std-workspace-* crate: {}", package.name)
397                    }
398                    None => match &**package.name {
399                        "core" => real_core = Some(package.id.clone()),
400                        "alloc" => real_alloc = Some(package.id.clone()),
401                        "std" => real_std = Some(package.id.clone()),
402                        _ => (),
403                    },
404                }
405            });
406
407            [fake_core.zip(real_core), fake_alloc.zip(real_alloc), fake_std.zip(real_std)]
408                .into_iter()
409                .flatten()
410        };
411
412        if let Some(resolve) = res.resolve.as_mut() {
413            resolve.nodes.retain_mut(|node| {
414                // Replace `rustc-std-workspace` crate with the actual one in the dependency list
415                node.deps.iter_mut().for_each(|dep| {
416                    let real_pkg = patches.clone().find(|((_, fake_id), _)| *fake_id == dep.pkg);
417                    if let Some((_, real)) = real_pkg {
418                        dep.pkg = real;
419                    }
420                });
421                // Remove this node if it's a fake one
422                !patches.clone().any(|((_, fake), _)| fake == node.id)
423            });
424        }
425        // Remove the fake ones from the package list
426        patches.map(|((idx, _), _)| idx).sorted().rev().for_each(|idx| {
427            res.packages.remove(idx);
428        });
429
430        let cargo_workspace =
431            CargoWorkspace::new(res, library_manifest.clone(), Default::default(), true);
432        Ok(RustLibSrcWorkspace::Workspace {
433            ws: cargo_workspace,
434            metadata_err: err.map(|e| format!("{e:#}")),
435        })
436    }
437}
438
439fn discover_sysroot_dir(
440    current_dir: &AbsPath,
441    extra_env: &FxHashMap<String, Option<String>>,
442) -> Result<AbsPathBuf> {
443    let mut rustc = toolchain::command(Tool::Rustc.path(), current_dir, extra_env);
444    rustc.current_dir(current_dir).args(["--print", "sysroot"]);
445    tracing::debug!("Discovering sysroot by {:?}", rustc);
446    let stdout = utf8_stdout(&mut rustc)?;
447    Ok(AbsPathBuf::assert(Utf8PathBuf::from(stdout)))
448}
449
450fn discover_rust_lib_src_dir(sysroot_path: &AbsPathBuf) -> Option<AbsPathBuf> {
451    if let Ok(path) = env::var("RUST_SRC_PATH") {
452        if let Ok(path) = AbsPathBuf::try_from(path.as_str()) {
453            let core = path.join("core");
454            if fs::metadata(&core).is_ok() {
455                tracing::debug!("Discovered sysroot by RUST_SRC_PATH: {path}");
456                return Some(path);
457            }
458            tracing::debug!("RUST_SRC_PATH is set, but is invalid (no core: {core:?}), ignoring");
459        } else {
460            tracing::debug!("RUST_SRC_PATH is set, but is invalid, ignoring");
461        }
462    }
463
464    get_rust_lib_src(sysroot_path)
465}
466
467fn discover_rust_lib_src_dir_or_add_component(
468    sysroot_path: &AbsPathBuf,
469    current_dir: &AbsPath,
470    extra_env: &FxHashMap<String, Option<String>>,
471) -> Result<AbsPathBuf> {
472    discover_rust_lib_src_dir(sysroot_path)
473        .or_else(|| {
474            let mut rustup = toolchain::command(Tool::Rustup.prefer_proxy(), current_dir, extra_env);
475            rustup.args(["component", "add", "rust-src"]);
476            tracing::info!("adding rust-src component by {:?}", rustup);
477            utf8_stdout(&mut rustup).ok()?;
478            get_rust_lib_src(sysroot_path)
479        })
480        .ok_or_else(|| {
481            tracing::error!(%sysroot_path, "can't load standard library, try installing `rust-src`");
482            format_err!(
483                "\
484can't load standard library from sysroot
485{sysroot_path}
486(discovered via `rustc --print sysroot`)
487try installing `rust-src` the same way you installed `rustc`"
488            )
489        })
490}
491
492fn get_rustc_src(sysroot_path: &AbsPath) -> Option<ManifestPath> {
493    let rustc_src = sysroot_path.join("lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml");
494    let rustc_src = ManifestPath::try_from(rustc_src).ok()?;
495    tracing::debug!("checking for rustc source code: {rustc_src}");
496    if fs::metadata(&rustc_src).is_ok() { Some(rustc_src) } else { None }
497}
498
499fn get_rust_lib_src(sysroot_path: &AbsPath) -> Option<AbsPathBuf> {
500    let rust_lib_src = sysroot_path.join("lib/rustlib/src/rust/library");
501    tracing::debug!("checking sysroot library: {rust_lib_src}");
502    if fs::metadata(&rust_lib_src).is_ok() { Some(rust_lib_src) } else { None }
503}
504
505// FIXME: Remove this, that will bump our project MSRV to 1.82
506pub(crate) mod stitched {
507    use std::ops;
508
509    use base_db::CrateName;
510    use la_arena::{Arena, Idx};
511
512    use crate::ManifestPath;
513
514    #[derive(Debug, Clone, Eq, PartialEq)]
515    pub struct Stitched {
516        pub(super) crates: Arena<RustLibSrcCrateData>,
517        pub(crate) edition: span::Edition,
518    }
519
520    impl ops::Index<RustLibSrcCrate> for Stitched {
521        type Output = RustLibSrcCrateData;
522        fn index(&self, index: RustLibSrcCrate) -> &RustLibSrcCrateData {
523            &self.crates[index]
524        }
525    }
526
527    impl Stitched {
528        pub(crate) fn public_deps(
529            &self,
530        ) -> impl Iterator<Item = (CrateName, RustLibSrcCrate, bool)> + '_ {
531            // core is added as a dependency before std in order to
532            // mimic rustcs dependency order
533            [("core", true), ("alloc", false), ("std", true), ("test", false)]
534                .into_iter()
535                .filter_map(move |(name, prelude)| {
536                    Some((CrateName::new(name).unwrap(), self.by_name(name)?, prelude))
537                })
538        }
539
540        pub(crate) fn proc_macro(&self) -> Option<RustLibSrcCrate> {
541            self.by_name("proc_macro")
542        }
543
544        pub(crate) fn crates(&self) -> impl ExactSizeIterator<Item = RustLibSrcCrate> + '_ {
545            self.crates.iter().map(|(id, _data)| id)
546        }
547
548        pub(super) fn by_name(&self, name: &str) -> Option<RustLibSrcCrate> {
549            let (id, _data) = self.crates.iter().find(|(_id, data)| data.name == name)?;
550            Some(id)
551        }
552    }
553
554    pub(crate) type RustLibSrcCrate = Idx<RustLibSrcCrateData>;
555
556    #[derive(Debug, Clone, Eq, PartialEq)]
557    pub(crate) struct RustLibSrcCrateData {
558        pub(crate) name: String,
559        pub(crate) root: ManifestPath,
560        pub(crate) deps: Vec<RustLibSrcCrate>,
561    }
562
563    pub(super) const SYSROOT_CRATES: &str = "
564alloc
565backtrace
566core
567panic_abort
568panic_unwind
569proc_macro
570profiler_builtins
571std
572stdarch/crates/std_detect
573test
574unwind";
575
576    pub(super) const ALLOC_DEPS: &str = "core";
577
578    pub(super) const STD_DEPS: &str = "
579alloc
580panic_unwind
581panic_abort
582core
583profiler_builtins
584unwind
585std_detect
586test";
587
588    // core is required for our builtin derives to work in the proc_macro lib currently
589    pub(super) const PROC_MACRO_DEPS: &str = "
590std
591core";
592}