Skip to content

Commit 030d783

Browse files
committed
Fix invoking cargo without consulting CARGO or standard installation paths
1 parent 5f72254 commit 030d783

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ hir = { path = "../ra_hir", package = "ra_hir" }
4848
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
4949
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
5050
ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
51+
ra_toolchain = { path = "../ra_toolchain" }
5152

5253
[target.'cfg(windows)'.dependencies]
5354
winapi = "0.3.8"

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::{
4040
world::WorldSnapshot,
4141
LspError, Result,
4242
};
43+
use anyhow::Context;
4344

4445
pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> {
4546
let _p = profile("handle_analyzer_status");
@@ -982,10 +983,15 @@ fn to_lsp_runnable(
982983
target.map_or_else(|| "run binary".to_string(), |t| format!("run {}", t))
983984
}
984985
};
986+
let cargo_path = ra_toolchain::cargo()
987+
.to_str()
988+
.context("Path to cargo executable contains invalid UTF8 characters")?
989+
.to_owned();
990+
985991
Ok(lsp_ext::Runnable {
986992
range: to_proto::range(&line_index, runnable.range),
987993
label,
988-
bin: "cargo".to_string(),
994+
bin: cargo_path,
989995
args,
990996
extra_args,
991997
env: {

editors/code/src/cargo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ export class Cargo {
126126
}
127127
}
128128

129-
// Mirrors `ra_env::get_path_for_executable` implementation
130-
function getCargoPathOrFail(): string {
129+
// Mirrors `ra_toolchain::cargo()` implementation
130+
export function getCargoPathOrFail(): string {
131131
const envVar = process.env.CARGO;
132132
const executableName = "cargo";
133133

editors/code/src/tasks.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode';
2+
import { getCargoPathOrFail } from "./cargo";
23

34
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
45
// our configuration should be compatible with it so use the same key.
@@ -24,6 +25,8 @@ class CargoTaskProvider implements vscode.TaskProvider {
2425
// set of tasks that always exist. These tasks cannot be removed in
2526
// tasks.json - only tweaked.
2627

28+
const cargoPath = getCargoPathOrFail();
29+
2730
return [
2831
{ command: 'build', group: vscode.TaskGroup.Build },
2932
{ command: 'check', group: vscode.TaskGroup.Build },
@@ -46,7 +49,7 @@ class CargoTaskProvider implements vscode.TaskProvider {
4649
`cargo ${command}`,
4750
'rust',
4851
// What to do when this command is executed.
49-
new vscode.ShellExecution('cargo', [command]),
52+
new vscode.ShellExecution(cargoPath, [command]),
5053
// Problem matchers.
5154
['$rustc'],
5255
);
@@ -80,4 +83,4 @@ class CargoTaskProvider implements vscode.TaskProvider {
8083
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
8184
const provider = new CargoTaskProvider(target);
8285
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
83-
}
86+
}

0 commit comments

Comments
 (0)