pull function out into new crate ra_env; use in ra_flycheck as well

This commit is contained in:
Craig Disselkoen 2020-05-05 14:07:10 -07:00
parent 5aa1bba107
commit 303b444dbb
9 changed files with 31 additions and 16 deletions

11
Cargo.lock generated
View file

@ -999,6 +999,14 @@ dependencies = [
"test_utils",
]
[[package]]
name = "ra_env"
version = "0.1.0"
dependencies = [
"anyhow",
"dirs",
]
[[package]]
name = "ra_flycheck"
version = "0.1.0"
@ -1009,6 +1017,7 @@ dependencies = [
"jod-thread",
"log",
"lsp-types",
"ra_env",
"serde_json",
]
@ -1209,11 +1218,11 @@ version = "0.1.0"
dependencies = [
"anyhow",
"cargo_metadata",
"dirs",
"log",
"ra_arena",
"ra_cfg",
"ra_db",
"ra_env",
"ra_proc_macro",
"rustc-hash",
"serde",

9
crates/ra_env/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
edition = "2018"
name = "ra_env"
version = "0.1.0"
authors = ["rust-analyzer developers"]
[dependencies]
anyhow = "1.0.26"
dirs = "2.0"

View file

@ -13,7 +13,7 @@ pub fn get_path_for_executable(executable_name: impl AsRef<str>) -> Result<Strin
// 1) Appropriate environment variable (erroring if this is set but not a usable executable)
// example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc
// 2) `<executable_name>`
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` in on the $PATH
// example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH
// 3) `~/.cargo/bin/<executable_name>`
// example: for cargo, this tries ~/.cargo/bin/cargo
// It seems that this is a reasonable place to try for cargo, rustc, and rustup

View file

@ -14,6 +14,7 @@ log = "0.4.8"
cargo_metadata = "0.9.1"
serde_json = "1.0.48"
jod-thread = "0.1.1"
ra_env = { path = "../ra_env" }
[dev-dependencies]
insta = "0.16.0"

View file

@ -4,7 +4,6 @@
mod conv;
use std::{
env,
io::{self, BufRead, BufReader},
path::PathBuf,
process::{Command, Stdio},
@ -17,6 +16,7 @@ use lsp_types::{
CodeAction, CodeActionOrCommand, Diagnostic, Url, WorkDoneProgress, WorkDoneProgressBegin,
WorkDoneProgressEnd, WorkDoneProgressReport,
};
use ra_env::get_path_for_executable;
use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic};
@ -216,7 +216,7 @@ impl FlycheckThread {
let mut cmd = match &self.config {
FlycheckConfig::CargoCommand { command, all_targets, all_features, extra_args } => {
let mut cmd = Command::new(cargo_binary());
let mut cmd = Command::new(get_path_for_executable("cargo").unwrap());
cmd.arg(command);
cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]);
cmd.arg(self.workspace_root.join("Cargo.toml"));
@ -337,7 +337,3 @@ fn run_cargo(
Ok(())
}
fn cargo_binary() -> String {
env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
}

View file

@ -14,13 +14,12 @@ rustc-hash = "1.1.0"
cargo_metadata = "0.9.1"
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }
ra_cfg = { path = "../ra_cfg" }
ra_db = { path = "../ra_db" }
ra_env = { path = "../ra_env" }
ra_proc_macro = { path = "../ra_proc_macro" }
serde = { version = "1.0.106", features = ["derive"] }
serde_json = "1.0.48"
anyhow = "1.0.26"
dirs = "2.0"

View file

@ -7,11 +7,11 @@ use std::{
process::Command,
};
use super::find_executables::get_path_for_executable;
use anyhow::{Context, Result};
use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
use ra_arena::{Arena, Idx};
use ra_db::Edition;
use ra_env::get_path_for_executable;
use rustc_hash::FxHashMap;
/// `CargoWorkspace` represents the logical structure of, well, a Cargo

View file

@ -1,7 +1,6 @@
//! FIXME: write short doc here
mod cargo_workspace;
mod find_executables;
mod json_project;
mod sysroot;
@ -15,6 +14,7 @@ use std::{
use anyhow::{bail, Context, Result};
use ra_cfg::CfgOptions;
use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
use ra_env::get_path_for_executable;
use rustc_hash::FxHashMap;
use serde_json::from_reader;
@ -559,7 +559,7 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
match (|| -> Result<String> {
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
let mut cmd = Command::new("rustc");
let mut cmd = Command::new(get_path_for_executable("rustc")?);
cmd.args(&["--print", "cfg", "-O"]);
if let Some(target) = target {
cmd.args(&["--target", target.as_str()]);

View file

@ -1,6 +1,5 @@
//! FIXME: write short doc here
use super::find_executables::get_path_for_executable;
use anyhow::{bail, Context, Result};
use std::{
env, ops,
@ -9,6 +8,7 @@ use std::{
};
use ra_arena::{Arena, Idx};
use ra_env::get_path_for_executable;
#[derive(Default, Debug, Clone)]
pub struct Sysroot {
@ -122,7 +122,8 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
if !src_path.exists() {
run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?;
let rustup = get_path_for_executable("rustup")?;
run_command_in_cargo_dir(cargo_toml, &rustup, &["component", "add", "rust-src"])?;
}
if !src_path.exists() {
bail!(