Skip to content

Commit 81d26e7

Browse files
committed
Auto merge of rust-lang#13667 - Veykril:detached-files-sysroot, r=Veykril
Handle sysroot config in detached-files workspaces
2 parents 2656297 + 2300c9d commit 81d26e7

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
}
@@ -479,21 +491,25 @@ impl ProjectWorkspace {
479491
include: vec![detached_file.clone()],
480492
exclude: Vec::new(),
481493
})
482-
.chain(mk_sysroot(Some(sysroot)))
494+
.chain(mk_sysroot(sysroot.as_ref()))
483495
.collect(),
484496
}
485497
}
486498

487499
pub fn n_packages(&self) -> usize {
488500
match self {
489-
ProjectWorkspace::Json { project, .. } => project.n_crates(),
501+
ProjectWorkspace::Json { project, sysroot, .. } => {
502+
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
503+
sysroot_package_len + project.n_crates()
504+
}
490505
ProjectWorkspace::Cargo { cargo, sysroot, rustc, .. } => {
491506
let rustc_package_len = rustc.as_ref().map_or(0, |it| it.packages().len());
492507
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
493508
cargo.packages().len() + sysroot_package_len + rustc_package_len
494509
}
495510
ProjectWorkspace::DetachedFiles { sysroot, files, .. } => {
496-
sysroot.crates().len() + files.len()
511+
let sysroot_package_len = sysroot.as_ref().map_or(0, |it| it.crates().len());
512+
sysroot_package_len + files.len()
497513
}
498514
}
499515
}
@@ -805,12 +821,14 @@ fn detached_files_to_crate_graph(
805821
rustc_cfg: Vec<CfgFlag>,
806822
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
807823
detached_files: &[AbsPathBuf],
808-
sysroot: &Sysroot,
824+
sysroot: &Option<Sysroot>,
809825
) -> CrateGraph {
810826
let _p = profile::span("detached_files_to_crate_graph");
811827
let mut crate_graph = CrateGraph::default();
812-
let (public_deps, _libproc_macro) =
813-
sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load);
828+
let (public_deps, _libproc_macro) = match sysroot {
829+
Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
830+
None => (SysrootPublicDeps::default(), None),
831+
};
814832

815833
let mut cfg_options = CfgOptions::default();
816834
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)