Fix invoking cargo without consulting CARGO or standard installation paths

This commit is contained in:
veetaha 2020-05-23 04:58:22 +03:00
parent 5f7225446e
commit 030d78345f
5 changed files with 16 additions and 5 deletions

1
Cargo.lock generated
View file

@ -1364,6 +1364,7 @@ dependencies = [
"ra_syntax", "ra_syntax",
"ra_text_edit", "ra_text_edit",
"ra_tt", "ra_tt",
"ra_toolchain",
"ra_vfs", "ra_vfs",
"rand", "rand",
"relative-path", "relative-path",

View file

@ -48,6 +48,7 @@ hir = { path = "../ra_hir", package = "ra_hir" }
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" } hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
ra_proc_macro_srv = { path = "../ra_proc_macro_srv" } ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
ra_toolchain = { path = "../ra_toolchain" }
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
winapi = "0.3.8" winapi = "0.3.8"

View file

@ -40,6 +40,7 @@ use crate::{
world::WorldSnapshot, world::WorldSnapshot,
LspError, Result, LspError, Result,
}; };
use anyhow::Context;
pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> { pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> {
let _p = profile("handle_analyzer_status"); let _p = profile("handle_analyzer_status");
@ -982,10 +983,15 @@ fn to_lsp_runnable(
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t)) target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
} }
}; };
let cargo_path = ra_toolchain::cargo()
.to_str()
.context("Path to cargo executable contains invalid UTF8 characters")?
.to_owned();
Ok(lsp_ext::Runnable { Ok(lsp_ext::Runnable {
range: to_proto::range(&line_index, runnable.range), range: to_proto::range(&line_index, runnable.range),
label, label,
bin: "cargo".to_string(), bin: cargo_path,
args, args,
extra_args, extra_args,
env: { env: {

View file

@ -126,8 +126,8 @@ export class Cargo {
} }
} }
// Mirrors `ra_env::get_path_for_executable` implementation // Mirrors `ra_toolchain::cargo()` implementation
function getCargoPathOrFail(): string { export function getCargoPathOrFail(): string {
const envVar = process.env.CARGO; const envVar = process.env.CARGO;
const executableName = "cargo"; const executableName = "cargo";

View file

@ -1,4 +1,5 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { getCargoPathOrFail } from "./cargo";
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and // This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
// our configuration should be compatible with it so use the same key. // our configuration should be compatible with it so use the same key.
@ -24,6 +25,8 @@ class CargoTaskProvider implements vscode.TaskProvider {
// set of tasks that always exist. These tasks cannot be removed in // set of tasks that always exist. These tasks cannot be removed in
// tasks.json - only tweaked. // tasks.json - only tweaked.
const cargoPath = getCargoPathOrFail();
return [ return [
{ command: 'build', group: vscode.TaskGroup.Build }, { command: 'build', group: vscode.TaskGroup.Build },
{ command: 'check', group: vscode.TaskGroup.Build }, { command: 'check', group: vscode.TaskGroup.Build },
@ -46,7 +49,7 @@ class CargoTaskProvider implements vscode.TaskProvider {
`cargo ${command}`, `cargo ${command}`,
'rust', 'rust',
// What to do when this command is executed. // What to do when this command is executed.
new vscode.ShellExecution('cargo', [command]), new vscode.ShellExecution(cargoPath, [command]),
// Problem matchers. // Problem matchers.
['$rustc'], ['$rustc'],
); );
@ -80,4 +83,4 @@ class CargoTaskProvider implements vscode.TaskProvider {
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable { export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
const provider = new CargoTaskProvider(target); const provider = new CargoTaskProvider(target);
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider); return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
} }