Skip to content

Commit 303b444

Browse files
committed
pull function out into new crate ra_env; use in ra_flycheck as well
1 parent 5aa1bba commit 303b444

File tree

9 files changed

+31
-16
lines changed

9 files changed

+31
-16
lines changed

Cargo.lock

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_env/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
edition = "2018"
3+
name = "ra_env"
4+
version = "0.1.0"
5+
authors = ["rust-analyzer developers"]
6+
7+
[dependencies]
8+
anyhow = "1.0.26"
9+
dirs = "2.0"

crates/ra_project_model/src/find_executables.rs renamed to crates/ra_env/src/lib.rs

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

crates/ra_flycheck/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ log = "0.4.8"
1414
cargo_metadata = "0.9.1"
1515
serde_json = "1.0.48"
1616
jod-thread = "0.1.1"
17+
ra_env = { path = "../ra_env" }
1718

1819
[dev-dependencies]
1920
insta = "0.16.0"

crates/ra_flycheck/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
mod conv;
55

66
use std::{
7-
env,
87
io::{self, BufRead, BufReader},
98
path::PathBuf,
109
process::{Command, Stdio},
@@ -17,6 +16,7 @@ use lsp_types::{
1716
CodeAction, CodeActionOrCommand, Diagnostic, Url, WorkDoneProgress, WorkDoneProgressBegin,
1817
WorkDoneProgressEnd, WorkDoneProgressReport,
1918
};
19+
use ra_env::get_path_for_executable;
2020

2121
use crate::conv::{map_rust_diagnostic_to_lsp, MappedRustDiagnostic};
2222

@@ -216,7 +216,7 @@ impl FlycheckThread {
216216

217217
let mut cmd = match &self.config {
218218
FlycheckConfig::CargoCommand { command, all_targets, all_features, extra_args } => {
219-
let mut cmd = Command::new(cargo_binary());
219+
let mut cmd = Command::new(get_path_for_executable("cargo").unwrap());
220220
cmd.arg(command);
221221
cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]);
222222
cmd.arg(self.workspace_root.join("Cargo.toml"));
@@ -337,7 +337,3 @@ fn run_cargo(
337337

338338
Ok(())
339339
}
340-
341-
fn cargo_binary() -> String {
342-
env::var("CARGO").unwrap_or_else(|_| "cargo".to_string())
343-
}

crates/ra_project_model/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ rustc-hash = "1.1.0"
1414
cargo_metadata = "0.9.1"
1515

1616
ra_arena = { path = "../ra_arena" }
17-
ra_db = { path = "../ra_db" }
1817
ra_cfg = { path = "../ra_cfg" }
18+
ra_db = { path = "../ra_db" }
19+
ra_env = { path = "../ra_env" }
1920
ra_proc_macro = { path = "../ra_proc_macro" }
2021

2122
serde = { version = "1.0.106", features = ["derive"] }
2223
serde_json = "1.0.48"
2324

2425
anyhow = "1.0.26"
25-
26-
dirs = "2.0"

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::{
77
process::Command,
88
};
99

10-
use super::find_executables::get_path_for_executable;
1110
use anyhow::{Context, Result};
1211
use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
1312
use ra_arena::{Arena, Idx};
1413
use ra_db::Edition;
14+
use ra_env::get_path_for_executable;
1515
use rustc_hash::FxHashMap;
1616

1717
/// `CargoWorkspace` represents the logical structure of, well, a Cargo

crates/ra_project_model/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! FIXME: write short doc here
22
33
mod cargo_workspace;
4-
mod find_executables;
54
mod json_project;
65
mod sysroot;
76

@@ -15,6 +14,7 @@ use std::{
1514
use anyhow::{bail, Context, Result};
1615
use ra_cfg::CfgOptions;
1716
use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSource, ExternSourceId, FileId};
17+
use ra_env::get_path_for_executable;
1818
use rustc_hash::FxHashMap;
1919
use serde_json::from_reader;
2020

@@ -559,7 +559,7 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions {
559559

560560
match (|| -> Result<String> {
561561
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
562-
let mut cmd = Command::new("rustc");
562+
let mut cmd = Command::new(get_path_for_executable("rustc")?);
563563
cmd.args(&["--print", "cfg", "-O"]);
564564
if let Some(target) = target {
565565
cmd.args(&["--target", target.as_str()]);

crates/ra_project_model/src/sysroot.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! FIXME: write short doc here
22
3-
use super::find_executables::get_path_for_executable;
43
use anyhow::{bail, Context, Result};
54
use std::{
65
env, ops,
@@ -9,6 +8,7 @@ use std::{
98
};
109

1110
use ra_arena::{Arena, Idx};
11+
use ra_env::get_path_for_executable;
1212

1313
#[derive(Default, Debug, Clone)]
1414
pub struct Sysroot {
@@ -122,7 +122,8 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
122122
let src_path = sysroot_path.join("lib/rustlib/src/rust/src");
123123

124124
if !src_path.exists() {
125-
run_command_in_cargo_dir(cargo_toml, "rustup", &["component", "add", "rust-src"])?;
125+
let rustup = get_path_for_executable("rustup")?;
126+
run_command_in_cargo_dir(cargo_toml, &rustup, &["component", "add", "rust-src"])?;
126127
}
127128
if !src_path.exists() {
128129
bail!(

0 commit comments

Comments
 (0)