Skip to content

Commit 2300c9d

Browse files
committed
Handle sysroot config in detached-files workspaces
1 parent e162d58 commit 2300c9d

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

crates/project-model/src/workspace.rs

+38-20
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub enum ProjectWorkspace {
9393
// //
9494
/// Project with a set of disjoint files, not belonging to any particular workspace.
9595
/// Backed by basic sysroot crates for basic completion and highlighting.
96-
DetachedFiles { files: Vec<AbsPathBuf>, sysroot: Sysroot, rustc_cfg: Vec<CfgFlag> },
96+
DetachedFiles { files: Vec<AbsPathBuf>, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
9797
}
9898

9999
impl fmt::Debug for ProjectWorkspace {
@@ -133,7 +133,7 @@ impl fmt::Debug for ProjectWorkspace {
133133
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => f
134134
.debug_struct("DetachedFiles")
135135
.field("n_files", &files.len())
136-
.field("n_sysroot_crates", &sysroot.crates().len())
136+
.field("sysroot", &sysroot.is_some())
137137
.field("n_rustc_cfg", &rustc_cfg.len())
138138
.finish(),
139139
}
@@ -191,10 +191,7 @@ impl ProjectWorkspace {
191191
let sysroot = match &config.sysroot {
192192
Some(RustcSource::Path(path)) => {
193193
Some(Sysroot::with_sysroot_dir(path.clone()).with_context(|| {
194-
format!(
195-
"Failed to find sysroot for Cargo.toml file {}.",
196-
cargo_toml.display()
197-
)
194+
format!("Failed to find sysroot at {}.", path.display())
198195
})?)
199196
}
200197
Some(RustcSource::Discover) => Some(
@@ -291,14 +288,29 @@ impl ProjectWorkspace {
291288
Ok(ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg })
292289
}
293290

294-
pub fn load_detached_files(detached_files: Vec<AbsPathBuf>) -> Result<ProjectWorkspace> {
295-
let sysroot = Sysroot::discover(
296-
detached_files
297-
.first()
298-
.and_then(|it| it.parent())
299-
.ok_or_else(|| format_err!("No detached files to load"))?,
300-
&Default::default(),
301-
)?;
291+
pub fn load_detached_files(
292+
detached_files: Vec<AbsPathBuf>,
293+
config: &CargoConfig,
294+
) -> Result<ProjectWorkspace> {
295+
let sysroot = match &config.sysroot {
296+
Some(RustcSource::Path(path)) => Some(
297+
Sysroot::with_sysroot_dir(path.clone())
298+
.with_context(|| format!("Failed to find sysroot at {}.", path.display()))?,
299+
),
300+
Some(RustcSource::Discover) => {
301+
let dir = &detached_files
302+
.first()
303+
.and_then(|it| it.parent())
304+
.ok_or_else(|| format_err!("No detached files to load"))?;
305+
Some(Sysroot::discover(dir, &config.extra_env).with_context(|| {
306+
format!("Failed to find sysroot in {}. Is rust-src installed?", dir.display())
307+
})?)
308+
}
309+
None => None,
310+
};
311+
if let Some(sysroot) = &sysroot {
312+
tracing::info!(src_root = %sysroot.src_root().display(), root = %sysroot.root().display(), "Using sysroot");
313+
}
302314
let rustc_cfg = rustc_cfg::get(None, None, &Default::default());
303315
Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg })
304316
}
@@ -464,21 +476,25 @@ impl ProjectWorkspace {
464476
include: vec![detached_file.clone()],
465477
exclude: Vec::new(),
466478
})
467-
.chain(mk_sysroot(Some(sysroot)))
479+
.chain(mk_sysroot(sysroot.as_ref()))
468480
.collect(),
469481
}
470482
}
471483

472484
pub fn n_packages(&self) -> usize {
473485
match self {
474-
ProjectWorkspace::Json { project, .. } => project.n_crates(),
486+
ProjectWorkspace::Json { project, sysroot, .. } => {
487+
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
488+
sysroot_package_len + project.n_crates()
489+
}
475490
ProjectWorkspace::Cargo { cargo, sysroot, rustc, .. } => {
476491
let rustc_package_len = rustc.as_ref().map_or(0, |it| it.packages().len());
477492
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
478493
cargo.packages().len() + sysroot_package_len + rustc_package_len
479494
}
480495
ProjectWorkspace::DetachedFiles { sysroot, files, .. } => {
481-
sysroot.crates().len() + files.len()
496+
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
497+
sysroot_package_len + files.len()
482498
}
483499
}
484500
}
@@ -790,12 +806,14 @@ fn detached_files_to_crate_graph(
790806
rustc_cfg: Vec<CfgFlag>,
791807
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
792808
detached_files: &[AbsPathBuf],
793-
sysroot: &Sysroot,
809+
sysroot: &Option<Sysroot>,
794810
) -> CrateGraph {
795811
let _p = profile::span("detached_files_to_crate_graph");
796812
let mut crate_graph = CrateGraph::default();
797-
let (public_deps, _libproc_macro) =
798-
sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load);
813+
let (public_deps, _libproc_macro) = match sysroot {
814+
Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
815+
None => (SysrootPublicDeps::default(), None),
816+
};
799817

800818
let mut cfg_options = CfgOptions::default();
801819
cfg_options.extend(rustc_cfg);

crates/rust-analyzer/src/reload.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,10 @@ impl GlobalState {
158158
.collect::<Vec<_>>();
159159

160160
if !detached_files.is_empty() {
161-
workspaces
162-
.push(project_model::ProjectWorkspace::load_detached_files(detached_files));
161+
workspaces.push(project_model::ProjectWorkspace::load_detached_files(
162+
detached_files,
163+
&cargo_config,
164+
));
163165
}
164166

165167
tracing::info!("did fetch workspaces {:?}", workspaces);

0 commit comments

Comments
 (0)