mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-13 17:35:21 +00:00
fix: Copy lockfiles into target directory before invoking cargo metadata
This commit is contained in:
parent
b1824c3962
commit
c0f428d55b
6 changed files with 179 additions and 87 deletions
|
|
@ -7,7 +7,7 @@ use anyhow::Context;
|
||||||
use base_db::Env;
|
use base_db::Env;
|
||||||
use cargo_metadata::{CargoOpt, MetadataCommand};
|
use cargo_metadata::{CargoOpt, MetadataCommand};
|
||||||
use la_arena::{Arena, Idx};
|
use la_arena::{Arena, Idx};
|
||||||
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
|
use paths::{AbsPath, AbsPathBuf, Utf8Path, Utf8PathBuf};
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use serde_json::from_value;
|
use serde_json::from_value;
|
||||||
|
|
@ -18,6 +18,14 @@ use toolchain::Tool;
|
||||||
use crate::{CfgOverrides, InvocationStrategy};
|
use crate::{CfgOverrides, InvocationStrategy};
|
||||||
use crate::{ManifestPath, Sysroot};
|
use crate::{ManifestPath, Sysroot};
|
||||||
|
|
||||||
|
const MINIMUM_TOOLCHAIN_VERSION_SUPPORTING_LOCKFILE_PATH: semver::Version = semver::Version {
|
||||||
|
major: 1,
|
||||||
|
minor: 82,
|
||||||
|
patch: 0,
|
||||||
|
pre: semver::Prerelease::EMPTY,
|
||||||
|
build: semver::BuildMetadata::EMPTY,
|
||||||
|
};
|
||||||
|
|
||||||
/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
|
/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
|
||||||
/// workspace. It pretty closely mirrors `cargo metadata` output.
|
/// workspace. It pretty closely mirrors `cargo metadata` output.
|
||||||
///
|
///
|
||||||
|
|
@ -291,6 +299,13 @@ pub struct CargoMetadataConfig {
|
||||||
pub extra_args: Vec<String>,
|
pub extra_args: Vec<String>,
|
||||||
/// Extra env vars to set when invoking the cargo command
|
/// Extra env vars to set when invoking the cargo command
|
||||||
pub extra_env: FxHashMap<String, Option<String>>,
|
pub extra_env: FxHashMap<String, Option<String>>,
|
||||||
|
/// The target dir for this workspace load.
|
||||||
|
pub target_dir: Utf8PathBuf,
|
||||||
|
/// What kind of metadata are we fetching: workspace, rustc, or sysroot.
|
||||||
|
pub kind: &'static str,
|
||||||
|
/// The toolchain version, if known.
|
||||||
|
/// Used to conditionally enable unstable cargo features.
|
||||||
|
pub toolchain_version: Option<semver::Version>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deserialize helper for the cargo metadata
|
// Deserialize helper for the cargo metadata
|
||||||
|
|
@ -383,18 +398,54 @@ impl CargoWorkspace {
|
||||||
config.targets.iter().flat_map(|it| ["--filter-platform".to_owned(), it.clone()]),
|
config.targets.iter().flat_map(|it| ["--filter-platform".to_owned(), it.clone()]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
// The manifest is a rust file, so this means its a script manifest
|
|
||||||
if cargo_toml.is_rust_manifest() {
|
|
||||||
// Deliberately don't set up RUSTC_BOOTSTRAP or a nightly override here, the user should
|
|
||||||
// opt into it themselves.
|
|
||||||
other_options.push("-Zscript".to_owned());
|
|
||||||
}
|
|
||||||
if locked {
|
|
||||||
other_options.push("--locked".to_owned());
|
|
||||||
}
|
|
||||||
if no_deps {
|
if no_deps {
|
||||||
other_options.push("--no-deps".to_owned());
|
other_options.push("--no-deps".to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut using_lockfile_copy = false;
|
||||||
|
// The manifest is a rust file, so this means its a script manifest
|
||||||
|
if cargo_toml.is_rust_manifest() {
|
||||||
|
other_options.push("-Zscript".to_owned());
|
||||||
|
} else if config
|
||||||
|
.toolchain_version
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|v| *v >= MINIMUM_TOOLCHAIN_VERSION_SUPPORTING_LOCKFILE_PATH)
|
||||||
|
{
|
||||||
|
let lockfile = <_ as AsRef<Utf8Path>>::as_ref(cargo_toml).with_extension("lock");
|
||||||
|
let target_lockfile = config
|
||||||
|
.target_dir
|
||||||
|
.join("rust-analyzer")
|
||||||
|
.join("metadata")
|
||||||
|
.join(config.kind)
|
||||||
|
.join("Cargo.lock");
|
||||||
|
match std::fs::copy(&lockfile, &target_lockfile) {
|
||||||
|
Ok(_) => {
|
||||||
|
using_lockfile_copy = true;
|
||||||
|
other_options.push("--lockfile-path".to_owned());
|
||||||
|
other_options.push(target_lockfile.to_string());
|
||||||
|
}
|
||||||
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||||
|
// There exists no lockfile yet
|
||||||
|
using_lockfile_copy = true;
|
||||||
|
other_options.push("--lockfile-path".to_owned());
|
||||||
|
other_options.push(target_lockfile.to_string());
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!(
|
||||||
|
"Failed to copy lock file from `{lockfile}` to `{target_lockfile}`: {e}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if using_lockfile_copy {
|
||||||
|
other_options.push("-Zunstable-options".to_owned());
|
||||||
|
meta.env("RUSTC_BOOTSTRAP", "1");
|
||||||
|
}
|
||||||
|
// No need to lock it if we copied the lockfile, we won't modify the original after all/
|
||||||
|
// This way cargo cannot error out on us if the lockfile requires updating.
|
||||||
|
if !using_lockfile_copy && locked {
|
||||||
|
other_options.push("--locked".to_owned());
|
||||||
|
}
|
||||||
meta.other_options(other_options);
|
meta.other_options(other_options);
|
||||||
|
|
||||||
// FIXME: Fetching metadata is a slow process, as it might require
|
// FIXME: Fetching metadata is a slow process, as it might require
|
||||||
|
|
@ -427,8 +478,8 @@ impl CargoWorkspace {
|
||||||
current_dir,
|
current_dir,
|
||||||
config,
|
config,
|
||||||
sysroot,
|
sysroot,
|
||||||
locked,
|
|
||||||
true,
|
true,
|
||||||
|
locked,
|
||||||
progress,
|
progress,
|
||||||
) {
|
) {
|
||||||
return Ok((metadata, Some(error)));
|
return Ok((metadata, Some(error)));
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
//! See [`ManifestPath`].
|
//! See [`ManifestPath`].
|
||||||
use std::{borrow::Borrow, fmt, ops};
|
use std::{borrow::Borrow, fmt, ops};
|
||||||
|
|
||||||
use paths::{AbsPath, AbsPathBuf};
|
use paths::{AbsPath, AbsPathBuf, Utf8Path};
|
||||||
|
|
||||||
/// More or less [`AbsPathBuf`] with non-None parent.
|
/// More or less [`AbsPathBuf`] with non-None parent.
|
||||||
///
|
///
|
||||||
|
|
@ -78,6 +78,12 @@ impl AsRef<std::ffi::OsStr> for ManifestPath {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<Utf8Path> for ManifestPath {
|
||||||
|
fn as_ref(&self) -> &Utf8Path {
|
||||||
|
self.file.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Borrow<AbsPath> for ManifestPath {
|
impl Borrow<AbsPath> for ManifestPath {
|
||||||
fn borrow(&self) -> &AbsPath {
|
fn borrow(&self) -> &AbsPath {
|
||||||
self.file.borrow()
|
self.file.borrow()
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
//! but we can't process `.rlib` and need source code instead. The source code
|
//! but we can't process `.rlib` and need source code instead. The source code
|
||||||
//! is typically installed with `rustup component add rust-src` command.
|
//! is typically installed with `rustup component add rust-src` command.
|
||||||
|
|
||||||
|
use core::fmt;
|
||||||
use std::{env, fs, ops::Not, path::Path, process::Command};
|
use std::{env, fs, ops::Not, path::Path, process::Command};
|
||||||
|
|
||||||
use anyhow::{Result, format_err};
|
use anyhow::{Result, format_err};
|
||||||
|
|
@ -34,6 +35,19 @@ pub enum RustLibSrcWorkspace {
|
||||||
Empty,
|
Empty,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for RustLibSrcWorkspace {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
RustLibSrcWorkspace::Workspace(ws) => write!(f, "workspace {}", ws.workspace_root()),
|
||||||
|
RustLibSrcWorkspace::Json(json) => write!(f, "json {}", json.manifest_or_root()),
|
||||||
|
RustLibSrcWorkspace::Stitched(stitched) => {
|
||||||
|
write!(f, "stitched with {} crates", stitched.crates.len())
|
||||||
|
}
|
||||||
|
RustLibSrcWorkspace::Empty => write!(f, "empty"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Sysroot {
|
impl Sysroot {
|
||||||
pub const fn empty() -> Sysroot {
|
pub const fn empty() -> Sysroot {
|
||||||
Sysroot {
|
Sysroot {
|
||||||
|
|
@ -195,6 +209,7 @@ impl Sysroot {
|
||||||
pub fn load_workspace(
|
pub fn load_workspace(
|
||||||
&self,
|
&self,
|
||||||
sysroot_source_config: &RustSourceWorkspaceConfig,
|
sysroot_source_config: &RustSourceWorkspaceConfig,
|
||||||
|
current_dir: &AbsPath,
|
||||||
progress: &dyn Fn(String),
|
progress: &dyn Fn(String),
|
||||||
) -> Option<RustLibSrcWorkspace> {
|
) -> Option<RustLibSrcWorkspace> {
|
||||||
assert!(matches!(self.workspace, RustLibSrcWorkspace::Empty), "workspace already loaded");
|
assert!(matches!(self.workspace, RustLibSrcWorkspace::Empty), "workspace already loaded");
|
||||||
|
|
@ -205,10 +220,16 @@ impl Sysroot {
|
||||||
if let RustSourceWorkspaceConfig::CargoMetadata(cargo_config) = sysroot_source_config {
|
if let RustSourceWorkspaceConfig::CargoMetadata(cargo_config) = sysroot_source_config {
|
||||||
let library_manifest = ManifestPath::try_from(src_root.join("Cargo.toml")).unwrap();
|
let library_manifest = ManifestPath::try_from(src_root.join("Cargo.toml")).unwrap();
|
||||||
if fs::metadata(&library_manifest).is_ok() {
|
if fs::metadata(&library_manifest).is_ok() {
|
||||||
if let Some(loaded) =
|
match self.load_library_via_cargo(
|
||||||
self.load_library_via_cargo(library_manifest, src_root, cargo_config, progress)
|
&library_manifest,
|
||||||
{
|
current_dir,
|
||||||
return Some(loaded);
|
cargo_config,
|
||||||
|
progress,
|
||||||
|
) {
|
||||||
|
Ok(loaded) => return Some(loaded),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("`cargo metadata` failed on `{library_manifest}` : {e}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tracing::debug!("Stitching sysroot library: {src_root}");
|
tracing::debug!("Stitching sysroot library: {src_root}");
|
||||||
|
|
@ -294,11 +315,11 @@ impl Sysroot {
|
||||||
|
|
||||||
fn load_library_via_cargo(
|
fn load_library_via_cargo(
|
||||||
&self,
|
&self,
|
||||||
library_manifest: ManifestPath,
|
library_manifest: &ManifestPath,
|
||||||
rust_lib_src_dir: &AbsPathBuf,
|
current_dir: &AbsPath,
|
||||||
cargo_config: &CargoMetadataConfig,
|
cargo_config: &CargoMetadataConfig,
|
||||||
progress: &dyn Fn(String),
|
progress: &dyn Fn(String),
|
||||||
) -> Option<RustLibSrcWorkspace> {
|
) -> Result<RustLibSrcWorkspace> {
|
||||||
tracing::debug!("Loading library metadata: {library_manifest}");
|
tracing::debug!("Loading library metadata: {library_manifest}");
|
||||||
let mut cargo_config = cargo_config.clone();
|
let mut cargo_config = cargo_config.clone();
|
||||||
// the sysroot uses `public-dependency`, so we make cargo think it's a nightly
|
// the sysroot uses `public-dependency`, so we make cargo think it's a nightly
|
||||||
|
|
@ -307,22 +328,16 @@ impl Sysroot {
|
||||||
Some("nightly".to_owned()),
|
Some("nightly".to_owned()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let (mut res, _) = match CargoWorkspace::fetch_metadata(
|
let (mut res, _) = CargoWorkspace::fetch_metadata(
|
||||||
&library_manifest,
|
library_manifest,
|
||||||
rust_lib_src_dir,
|
current_dir,
|
||||||
&cargo_config,
|
&cargo_config,
|
||||||
self,
|
self,
|
||||||
false,
|
false,
|
||||||
// Make sure we never attempt to write to the sysroot
|
// Make sure we never attempt to write to the sysroot
|
||||||
true,
|
true,
|
||||||
progress,
|
progress,
|
||||||
) {
|
)?;
|
||||||
Ok(it) => it,
|
|
||||||
Err(e) => {
|
|
||||||
tracing::error!("`cargo metadata` failed on `{library_manifest}` : {e}");
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Patch out `rustc-std-workspace-*` crates to point to the real crates.
|
// Patch out `rustc-std-workspace-*` crates to point to the real crates.
|
||||||
// This is done prior to `CrateGraph` construction to prevent de-duplication logic from failing.
|
// This is done prior to `CrateGraph` construction to prevent de-duplication logic from failing.
|
||||||
|
|
@ -373,8 +388,9 @@ impl Sysroot {
|
||||||
res.packages.remove(idx);
|
res.packages.remove(idx);
|
||||||
});
|
});
|
||||||
|
|
||||||
let cargo_workspace = CargoWorkspace::new(res, library_manifest, Default::default(), true);
|
let cargo_workspace =
|
||||||
Some(RustLibSrcWorkspace::Workspace(cargo_workspace))
|
CargoWorkspace::new(res, library_manifest.clone(), Default::default(), true);
|
||||||
|
Ok(RustLibSrcWorkspace::Workspace(cargo_workspace))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::env::temp_dir;
|
||||||
|
|
||||||
use base_db::{CrateGraphBuilder, ProcMacroPaths};
|
use base_db::{CrateGraphBuilder, ProcMacroPaths};
|
||||||
use cargo_metadata::Metadata;
|
use cargo_metadata::Metadata;
|
||||||
use cfg::{CfgAtom, CfgDiff};
|
use cfg::{CfgAtom, CfgDiff};
|
||||||
|
|
@ -235,12 +237,18 @@ fn smoke_test_real_sysroot_cargo() {
|
||||||
AbsPath::assert(Utf8Path::new(env!("CARGO_MANIFEST_DIR"))),
|
AbsPath::assert(Utf8Path::new(env!("CARGO_MANIFEST_DIR"))),
|
||||||
&Default::default(),
|
&Default::default(),
|
||||||
);
|
);
|
||||||
|
let cwd = AbsPathBuf::assert_utf8(temp_dir().join("smoke_test_real_sysroot_cargo"));
|
||||||
|
std::fs::create_dir_all(&cwd).unwrap();
|
||||||
let loaded_sysroot =
|
let loaded_sysroot =
|
||||||
sysroot.load_workspace(&RustSourceWorkspaceConfig::default_cargo(), &|_| ());
|
sysroot.load_workspace(&RustSourceWorkspaceConfig::default_cargo(), &cwd, &|_| ());
|
||||||
if let Some(loaded_sysroot) = loaded_sysroot {
|
if let Some(loaded_sysroot) = loaded_sysroot {
|
||||||
sysroot.set_workspace(loaded_sysroot);
|
sysroot.set_workspace(loaded_sysroot);
|
||||||
}
|
}
|
||||||
assert!(matches!(sysroot.workspace(), RustLibSrcWorkspace::Workspace(_)));
|
assert!(
|
||||||
|
matches!(sysroot.workspace(), RustLibSrcWorkspace::Workspace(_)),
|
||||||
|
"got {}",
|
||||||
|
sysroot.workspace()
|
||||||
|
);
|
||||||
let project_workspace = ProjectWorkspace {
|
let project_workspace = ProjectWorkspace {
|
||||||
kind: ProjectWorkspaceKind::Cargo {
|
kind: ProjectWorkspaceKind::Cargo {
|
||||||
cargo: cargo_workspace,
|
cargo: cargo_workspace,
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use base_db::{
|
||||||
};
|
};
|
||||||
use cfg::{CfgAtom, CfgDiff, CfgOptions};
|
use cfg::{CfgAtom, CfgDiff, CfgOptions};
|
||||||
use intern::{Symbol, sym};
|
use intern::{Symbol, sym};
|
||||||
use paths::{AbsPath, AbsPathBuf};
|
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use span::{Edition, FileId};
|
use span::{Edition, FileId};
|
||||||
|
|
@ -209,6 +209,7 @@ impl ProjectWorkspace {
|
||||||
progress: &(dyn Fn(String) + Sync),
|
progress: &(dyn Fn(String) + Sync),
|
||||||
) -> Result<ProjectWorkspace, anyhow::Error> {
|
) -> Result<ProjectWorkspace, anyhow::Error> {
|
||||||
progress("Discovering sysroot".to_owned());
|
progress("Discovering sysroot".to_owned());
|
||||||
|
let workspace_dir = cargo_toml.parent();
|
||||||
let CargoConfig {
|
let CargoConfig {
|
||||||
features,
|
features,
|
||||||
rustc_source,
|
rustc_source,
|
||||||
|
|
@ -224,15 +225,9 @@ impl ProjectWorkspace {
|
||||||
..
|
..
|
||||||
} = config;
|
} = config;
|
||||||
let mut sysroot = match (sysroot, sysroot_src) {
|
let mut sysroot = match (sysroot, sysroot_src) {
|
||||||
(Some(RustLibSource::Discover), None) => {
|
(Some(RustLibSource::Discover), None) => Sysroot::discover(workspace_dir, extra_env),
|
||||||
Sysroot::discover(cargo_toml.parent(), extra_env)
|
|
||||||
}
|
|
||||||
(Some(RustLibSource::Discover), Some(sysroot_src)) => {
|
(Some(RustLibSource::Discover), Some(sysroot_src)) => {
|
||||||
Sysroot::discover_with_src_override(
|
Sysroot::discover_with_src_override(workspace_dir, extra_env, sysroot_src.clone())
|
||||||
cargo_toml.parent(),
|
|
||||||
extra_env,
|
|
||||||
sysroot_src.clone(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
(Some(RustLibSource::Path(path)), None) => {
|
(Some(RustLibSource::Path(path)), None) => {
|
||||||
Sysroot::discover_rust_lib_src_dir(path.clone())
|
Sysroot::discover_rust_lib_src_dir(path.clone())
|
||||||
|
|
@ -248,24 +243,23 @@ impl ProjectWorkspace {
|
||||||
let toolchain_config = QueryConfig::Cargo(&sysroot, cargo_toml);
|
let toolchain_config = QueryConfig::Cargo(&sysroot, cargo_toml);
|
||||||
let targets =
|
let targets =
|
||||||
target_tuple::get(toolchain_config, target.as_deref(), extra_env).unwrap_or_default();
|
target_tuple::get(toolchain_config, target.as_deref(), extra_env).unwrap_or_default();
|
||||||
|
let toolchain = version::get(toolchain_config, extra_env)
|
||||||
// We spawn a bunch of processes to query various information about the workspace's
|
|
||||||
// toolchain and sysroot
|
|
||||||
// We can speed up loading a bit by spawning all of these processes in parallel (especially
|
|
||||||
// on systems were process spawning is delayed)
|
|
||||||
let join = thread::scope(|s| {
|
|
||||||
let workspace_dir = cargo_toml.parent();
|
|
||||||
let toolchain = s.spawn(|| {
|
|
||||||
version::get(toolchain_config, extra_env)
|
|
||||||
.inspect_err(|e| {
|
.inspect_err(|e| {
|
||||||
tracing::error!(%e,
|
tracing::error!(%e,
|
||||||
"failed fetching toolchain version for {cargo_toml:?} workspace"
|
"failed fetching toolchain version for {cargo_toml:?} workspace"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.ok()
|
.ok()
|
||||||
.flatten()
|
.flatten();
|
||||||
});
|
|
||||||
|
|
||||||
|
let target_dir =
|
||||||
|
config.target_dir.clone().unwrap_or_else(|| workspace_dir.join("target").into());
|
||||||
|
|
||||||
|
// We spawn a bunch of processes to query various information about the workspace's
|
||||||
|
// toolchain and sysroot
|
||||||
|
// We can speed up loading a bit by spawning all of these processes in parallel (especially
|
||||||
|
// on systems were process spawning is delayed)
|
||||||
|
let join = thread::scope(|s| {
|
||||||
let rustc_cfg = s.spawn(|| {
|
let rustc_cfg = s.spawn(|| {
|
||||||
rustc_cfg::get(toolchain_config, targets.first().map(Deref::deref), extra_env)
|
rustc_cfg::get(toolchain_config, targets.first().map(Deref::deref), extra_env)
|
||||||
});
|
});
|
||||||
|
|
@ -300,10 +294,13 @@ impl ProjectWorkspace {
|
||||||
targets: targets.clone(),
|
targets: targets.clone(),
|
||||||
extra_args: extra_args.clone(),
|
extra_args: extra_args.clone(),
|
||||||
extra_env: extra_env.clone(),
|
extra_env: extra_env.clone(),
|
||||||
|
target_dir: target_dir.clone(),
|
||||||
|
toolchain_version: toolchain.clone(),
|
||||||
|
kind: "rustc-dev"
|
||||||
},
|
},
|
||||||
&sysroot,
|
&sysroot,
|
||||||
*no_deps,
|
*no_deps,
|
||||||
false,
|
true,
|
||||||
progress,
|
progress,
|
||||||
) {
|
) {
|
||||||
Ok((meta, _error)) => {
|
Ok((meta, _error)) => {
|
||||||
|
|
@ -343,6 +340,9 @@ impl ProjectWorkspace {
|
||||||
targets: targets.clone(),
|
targets: targets.clone(),
|
||||||
extra_args: extra_args.clone(),
|
extra_args: extra_args.clone(),
|
||||||
extra_env: extra_env.clone(),
|
extra_env: extra_env.clone(),
|
||||||
|
target_dir: target_dir.clone(),
|
||||||
|
toolchain_version: toolchain.clone(),
|
||||||
|
kind: "workspace",
|
||||||
},
|
},
|
||||||
&sysroot,
|
&sysroot,
|
||||||
*no_deps,
|
*no_deps,
|
||||||
|
|
@ -353,15 +353,18 @@ impl ProjectWorkspace {
|
||||||
let loaded_sysroot = s.spawn(|| {
|
let loaded_sysroot = s.spawn(|| {
|
||||||
sysroot.load_workspace(
|
sysroot.load_workspace(
|
||||||
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
|
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
|
||||||
extra_env, &targets,
|
config,
|
||||||
|
&targets,
|
||||||
|
toolchain.clone(),
|
||||||
|
target_dir.clone(),
|
||||||
)),
|
)),
|
||||||
|
workspace_dir,
|
||||||
progress,
|
progress,
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
let cargo_config_extra_env =
|
let cargo_config_extra_env =
|
||||||
s.spawn(|| cargo_config_env(cargo_toml, extra_env, &sysroot));
|
s.spawn(|| cargo_config_env(cargo_toml, extra_env, &sysroot));
|
||||||
thread::Result::Ok((
|
thread::Result::Ok((
|
||||||
toolchain.join()?,
|
|
||||||
rustc_cfg.join()?,
|
rustc_cfg.join()?,
|
||||||
data_layout.join()?,
|
data_layout.join()?,
|
||||||
rustc_dir.join()?,
|
rustc_dir.join()?,
|
||||||
|
|
@ -371,15 +374,8 @@ impl ProjectWorkspace {
|
||||||
))
|
))
|
||||||
});
|
});
|
||||||
|
|
||||||
let (
|
let (rustc_cfg, data_layout, rustc, loaded_sysroot, cargo_metadata, cargo_config_extra_env) =
|
||||||
toolchain,
|
match join {
|
||||||
rustc_cfg,
|
|
||||||
data_layout,
|
|
||||||
rustc,
|
|
||||||
loaded_sysroot,
|
|
||||||
cargo_metadata,
|
|
||||||
cargo_config_extra_env,
|
|
||||||
) = match join {
|
|
||||||
Ok(it) => it,
|
Ok(it) => it,
|
||||||
Err(e) => std::panic::resume_unwind(e),
|
Err(e) => std::panic::resume_unwind(e),
|
||||||
};
|
};
|
||||||
|
|
@ -391,6 +387,7 @@ impl ProjectWorkspace {
|
||||||
})?;
|
})?;
|
||||||
let cargo = CargoWorkspace::new(meta, cargo_toml.clone(), cargo_config_extra_env, false);
|
let cargo = CargoWorkspace::new(meta, cargo_toml.clone(), cargo_config_extra_env, false);
|
||||||
if let Some(loaded_sysroot) = loaded_sysroot {
|
if let Some(loaded_sysroot) = loaded_sysroot {
|
||||||
|
tracing::info!(src_root = ?sysroot.rust_lib_src_root(), root = %loaded_sysroot, "Loaded sysroot");
|
||||||
sysroot.set_workspace(loaded_sysroot);
|
sysroot.set_workspace(loaded_sysroot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -426,14 +423,13 @@ impl ProjectWorkspace {
|
||||||
let query_config = QueryConfig::Rustc(&sysroot, project_json.path().as_ref());
|
let query_config = QueryConfig::Rustc(&sysroot, project_json.path().as_ref());
|
||||||
let targets = target_tuple::get(query_config, config.target.as_deref(), &config.extra_env)
|
let targets = target_tuple::get(query_config, config.target.as_deref(), &config.extra_env)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
let toolchain = version::get(query_config, &config.extra_env).ok().flatten();
|
||||||
|
|
||||||
// We spawn a bunch of processes to query various information about the workspace's
|
// We spawn a bunch of processes to query various information about the workspace's
|
||||||
// toolchain and sysroot
|
// toolchain and sysroot
|
||||||
// We can speed up loading a bit by spawning all of these processes in parallel (especially
|
// We can speed up loading a bit by spawning all of these processes in parallel (especially
|
||||||
// on systems were process spawning is delayed)
|
// on systems were process spawning is delayed)
|
||||||
let join = thread::scope(|s| {
|
let join = thread::scope(|s| {
|
||||||
let toolchain =
|
|
||||||
s.spawn(|| version::get(query_config, &config.extra_env).ok().flatten());
|
|
||||||
let rustc_cfg = s.spawn(|| {
|
let rustc_cfg = s.spawn(|| {
|
||||||
rustc_cfg::get(query_config, targets.first().map(Deref::deref), &config.extra_env)
|
rustc_cfg::get(query_config, targets.first().map(Deref::deref), &config.extra_env)
|
||||||
});
|
});
|
||||||
|
|
@ -445,31 +441,35 @@ impl ProjectWorkspace {
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
let loaded_sysroot = s.spawn(|| {
|
let loaded_sysroot = s.spawn(|| {
|
||||||
|
let project_root = project_json.project_root();
|
||||||
if let Some(sysroot_project) = sysroot_project {
|
if let Some(sysroot_project) = sysroot_project {
|
||||||
sysroot.load_workspace(
|
sysroot.load_workspace(
|
||||||
&RustSourceWorkspaceConfig::Json(*sysroot_project),
|
&RustSourceWorkspaceConfig::Json(*sysroot_project),
|
||||||
|
project_root,
|
||||||
progress,
|
progress,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
let target_dir = config
|
||||||
|
.target_dir
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| project_root.join("target").into());
|
||||||
sysroot.load_workspace(
|
sysroot.load_workspace(
|
||||||
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
|
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
|
||||||
&config.extra_env,
|
config,
|
||||||
&targets,
|
&targets,
|
||||||
|
toolchain.clone(),
|
||||||
|
target_dir,
|
||||||
)),
|
)),
|
||||||
|
project_root,
|
||||||
progress,
|
progress,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
thread::Result::Ok((
|
thread::Result::Ok((rustc_cfg.join()?, data_layout.join()?, loaded_sysroot.join()?))
|
||||||
toolchain.join()?,
|
|
||||||
rustc_cfg.join()?,
|
|
||||||
data_layout.join()?,
|
|
||||||
loaded_sysroot.join()?,
|
|
||||||
))
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let (toolchain, rustc_cfg, target_layout, loaded_sysroot) = match join {
|
let (rustc_cfg, target_layout, loaded_sysroot) = match join {
|
||||||
Ok(it) => it,
|
Ok(it) => it,
|
||||||
Err(e) => std::panic::resume_unwind(e),
|
Err(e) => std::panic::resume_unwind(e),
|
||||||
};
|
};
|
||||||
|
|
@ -507,11 +507,15 @@ impl ProjectWorkspace {
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
let rustc_cfg = rustc_cfg::get(query_config, None, &config.extra_env);
|
let rustc_cfg = rustc_cfg::get(query_config, None, &config.extra_env);
|
||||||
let data_layout = target_data_layout::get(query_config, None, &config.extra_env);
|
let data_layout = target_data_layout::get(query_config, None, &config.extra_env);
|
||||||
|
let target_dir = config.target_dir.clone().unwrap_or_else(|| dir.join("target").into());
|
||||||
let loaded_sysroot = sysroot.load_workspace(
|
let loaded_sysroot = sysroot.load_workspace(
|
||||||
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
|
&RustSourceWorkspaceConfig::CargoMetadata(sysroot_metadata_config(
|
||||||
&config.extra_env,
|
config,
|
||||||
&targets,
|
&targets,
|
||||||
|
toolchain.clone(),
|
||||||
|
target_dir.clone(),
|
||||||
)),
|
)),
|
||||||
|
dir,
|
||||||
&|_| (),
|
&|_| (),
|
||||||
);
|
);
|
||||||
if let Some(loaded_sysroot) = loaded_sysroot {
|
if let Some(loaded_sysroot) = loaded_sysroot {
|
||||||
|
|
@ -526,6 +530,9 @@ impl ProjectWorkspace {
|
||||||
targets,
|
targets,
|
||||||
extra_args: config.extra_args.clone(),
|
extra_args: config.extra_args.clone(),
|
||||||
extra_env: config.extra_env.clone(),
|
extra_env: config.extra_env.clone(),
|
||||||
|
target_dir,
|
||||||
|
toolchain_version: toolchain.clone(),
|
||||||
|
kind: "detached-file",
|
||||||
},
|
},
|
||||||
&sysroot,
|
&sysroot,
|
||||||
config.no_deps,
|
config.no_deps,
|
||||||
|
|
@ -1818,13 +1825,18 @@ fn add_dep_inner(graph: &mut CrateGraphBuilder, from: CrateBuilderId, dep: Depen
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sysroot_metadata_config(
|
fn sysroot_metadata_config(
|
||||||
extra_env: &FxHashMap<String, Option<String>>,
|
config: &CargoConfig,
|
||||||
targets: &[String],
|
targets: &[String],
|
||||||
|
toolchain_version: Option<Version>,
|
||||||
|
target_dir: Utf8PathBuf,
|
||||||
) -> CargoMetadataConfig {
|
) -> CargoMetadataConfig {
|
||||||
CargoMetadataConfig {
|
CargoMetadataConfig {
|
||||||
features: Default::default(),
|
features: Default::default(),
|
||||||
targets: targets.to_vec(),
|
targets: targets.to_vec(),
|
||||||
extra_args: Default::default(),
|
extra_args: Default::default(),
|
||||||
extra_env: extra_env.clone(),
|
extra_env: config.extra_env.clone(),
|
||||||
|
target_dir,
|
||||||
|
toolchain_version,
|
||||||
|
kind: "sysroot",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ use hir::{ChangeWithProcMacros, Crate};
|
||||||
use ide::{AnalysisHost, DiagnosticCode, DiagnosticsConfig};
|
use ide::{AnalysisHost, DiagnosticCode, DiagnosticsConfig};
|
||||||
use ide_db::base_db;
|
use ide_db::base_db;
|
||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
use paths::Utf8PathBuf;
|
|
||||||
use profile::StopWatch;
|
use profile::StopWatch;
|
||||||
use project_model::toolchain_info::{QueryConfig, target_data_layout};
|
use project_model::toolchain_info::{QueryConfig, target_data_layout};
|
||||||
use project_model::{
|
use project_model::{
|
||||||
|
|
@ -64,9 +63,9 @@ fn detect_errors_from_rustc_stderr_file(p: PathBuf) -> FxHashMap<DiagnosticCode,
|
||||||
|
|
||||||
impl Tester {
|
impl Tester {
|
||||||
fn new() -> Result<Self> {
|
fn new() -> Result<Self> {
|
||||||
let mut path = std::env::temp_dir();
|
let mut path = AbsPathBuf::assert_utf8(std::env::temp_dir());
|
||||||
path.push("ra-rustc-test.rs");
|
path.push("ra-rustc-test");
|
||||||
let tmp_file = AbsPathBuf::try_from(Utf8PathBuf::from_path_buf(path).unwrap()).unwrap();
|
let tmp_file = path.join("ra-rustc-test.rs");
|
||||||
std::fs::write(&tmp_file, "")?;
|
std::fs::write(&tmp_file, "")?;
|
||||||
let cargo_config = CargoConfig {
|
let cargo_config = CargoConfig {
|
||||||
sysroot: Some(RustLibSource::Discover),
|
sysroot: Some(RustLibSource::Discover),
|
||||||
|
|
@ -77,7 +76,7 @@ impl Tester {
|
||||||
|
|
||||||
let mut sysroot = Sysroot::discover(tmp_file.parent().unwrap(), &cargo_config.extra_env);
|
let mut sysroot = Sysroot::discover(tmp_file.parent().unwrap(), &cargo_config.extra_env);
|
||||||
let loaded_sysroot =
|
let loaded_sysroot =
|
||||||
sysroot.load_workspace(&RustSourceWorkspaceConfig::default_cargo(), &|_| ());
|
sysroot.load_workspace(&RustSourceWorkspaceConfig::default_cargo(), &path, &|_| ());
|
||||||
if let Some(loaded_sysroot) = loaded_sysroot {
|
if let Some(loaded_sysroot) = loaded_sysroot {
|
||||||
sysroot.set_workspace(loaded_sysroot);
|
sysroot.set_workspace(loaded_sysroot);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue