Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/ra_project_model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ ra_cfg = { path = "../ra_cfg" }

serde = { version = "1.0.89", features = ["derive"] }
serde_json = "1.0.39"

anyhow = "1.0.26"
11 changes: 7 additions & 4 deletions crates/ra_project_model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use cargo_metadata::{CargoOpt, MetadataCommand};
use ra_arena::{impl_arena_id, Arena, RawId};
use ra_db::Edition;
use rustc_hash::FxHashMap;
use serde::Deserialize;

use crate::Result;

/// `CargoWorkspace` represents the logical structure of, well, a Cargo
/// workspace. It pretty closely mirrors `cargo metadata` output.
///
Expand Down Expand Up @@ -171,7 +170,9 @@ impl CargoWorkspace {
if let Some(parent) = cargo_toml.parent() {
meta.current_dir(parent);
}
let meta = meta.exec().map_err(|e| format!("cargo metadata failed: {}", e))?;
let meta = meta.exec().with_context(|| {
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
})?;
let mut pkg_by_id = FxHashMap::default();
let mut packages = Arena::default();
let mut targets = Arena::default();
Expand All @@ -181,7 +182,9 @@ impl CargoWorkspace {
for meta_pkg in meta.packages {
let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg;
let is_member = ws_members.contains(&id);
let edition = edition.parse::<Edition>()?;
let edition = edition
.parse::<Edition>()
.with_context(|| format!("Failed to parse edition {}", edition))?;
let pkg = packages.alloc(PackageData {
name,
manifest: manifest_path,
Expand Down
51 changes: 40 additions & 11 deletions crates/ra_project_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
process::Command,
};

use anyhow::{bail, Context, Result};
use ra_cfg::CfgOptions;
use ra_db::{CrateGraph, CrateId, CrateName, Edition, Env, FileId};
use rustc_hash::FxHashMap;
Expand All @@ -23,8 +24,6 @@ pub use crate::{
sysroot::Sysroot,
};

pub type Result<T> = ::std::result::Result<T, Box<dyn Error + Send + Sync>>;

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct CargoTomlNotFoundError(pub PathBuf);

Expand Down Expand Up @@ -81,15 +80,36 @@ impl ProjectWorkspace {
) -> Result<ProjectWorkspace> {
match find_rust_project_json(path) {
Some(json_path) => {
let file = File::open(json_path)?;
let file = File::open(&json_path)
.with_context(|| format!("Failed to open json file {}", json_path.display()))?;
let reader = BufReader::new(file);
Ok(ProjectWorkspace::Json { project: from_reader(reader)? })
Ok(ProjectWorkspace::Json {
project: from_reader(reader).with_context(|| {
format!("Failed to deserialize json file {}", json_path.display())
})?,
})
}
None => {
let cargo_toml = find_cargo_toml(path)?;
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features)?;
let sysroot =
if with_sysroot { Sysroot::discover(&cargo_toml)? } else { Sysroot::default() };
let cargo_toml = find_cargo_toml(path).with_context(|| {
format!("Failed to find Cargo.toml for path {}", path.display())
})?;
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, cargo_features)
.with_context(|| {
format!(
"Failed to read Cargo metadata from Cargo.toml file {}",
cargo_toml.display()
)
})?;
let sysroot = if with_sysroot {
Sysroot::discover(&cargo_toml).with_context(|| {
format!(
"Failed to find sysroot for Cargo.toml file {}",
cargo_toml.display()
)
})?
} else {
Sysroot::default()
};
Ok(ProjectWorkspace::Cargo { cargo, sysroot })
}
}
Expand Down Expand Up @@ -403,11 +423,20 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
}
}

match (|| -> Result<_> {
match (|| -> Result<String> {
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?;
let output = Command::new("rustc")
.args(&["--print", "cfg", "-O"])
.output()
.context("Failed to get output from rustc --print cfg -O")?;
if !output.status.success() {
Err("failed to get rustc cfgs")?;
bail!(
"rustc --print cfg -O exited with exit code ({})",
output
.status
.code()
.map_or(String::from("no exit code"), |code| format!("{}", code))
);
}
Ok(String::from_utf8(output.stdout)?)
})() {
Expand Down
14 changes: 9 additions & 5 deletions crates/ra_project_model/src/sysroot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! FIXME: write short doc here

use anyhow::{anyhow, bail, Context, Result};
use std::{
env,
path::{Path, PathBuf},
Expand All @@ -8,8 +9,6 @@ use std::{

use ra_arena::{impl_arena_id, Arena, RawId};

use crate::Result;

#[derive(Default, Debug, Clone)]
pub struct Sysroot {
crates: Arena<SysrootCrate, SysrootCrateData>,
Expand Down Expand Up @@ -51,7 +50,7 @@ impl Sysroot {
let src = try_find_src_path(cargo_toml)?;

if !src.exists() {
Err(format!(
Err(anyhow!(
"can't load standard library from sysroot\n\
{}\n\
(discovered via `rustc --print sysroot`)\n\
Expand Down Expand Up @@ -100,9 +99,14 @@ fn try_find_src_path(cargo_toml: &Path) -> Result<PathBuf> {
.current_dir(cargo_toml.parent().unwrap())
.args(&["--print", "sysroot"])
.output()
.map_err(|e| format!("rustc --print sysroot failed: {}", e))?;
.context("rustc --print sysroot failed")?;
if !rustc_output.status.success() {
Err("failed to locate sysroot")?;
match rustc_output.status.code() {
Some(code) => {
bail!("failed to locate sysroot: rustc --print sysroot exited with code {}", code)
}
None => bail!("failed to locate sysroot: rustc --print sysroot terminated by signal"),
};
}
let stdout = String::from_utf8(rustc_output.stdout)?;
let sysroot_path = Path::new(stdout.trim());
Expand Down