Skip to content

Commit d605ec9

Browse files
committed
Change Runnable.bin -> Runnable.kind
As per matklad, we now pass the responsibility for finding the binary to the frontend. Also, added caching for finding the binary path to reduce the amount of filesystem interactions.
1 parent a419ced commit d605ec9

File tree

13 files changed

+123
-83
lines changed

13 files changed

+123
-83
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ 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" }
5251

5352
[target.'cfg(windows)'.dependencies]
5453
winapi = "0.3.8"

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,21 @@ pub struct RunnablesParams {
121121
pub position: Option<Position>,
122122
}
123123

124+
// Must strictly correspond to the executable name
125+
#[derive(Serialize, Deserialize, Debug)]
126+
#[serde(rename_all = "lowercase")]
127+
pub enum RunnableKind {
128+
Cargo,
129+
Rustc,
130+
Rustup,
131+
}
132+
124133
#[derive(Deserialize, Serialize, Debug)]
125134
#[serde(rename_all = "camelCase")]
126135
pub struct Runnable {
127136
pub range: Range,
128137
pub label: String,
129-
pub bin: String,
138+
pub kind: RunnableKind,
130139
pub args: Vec<String>,
131140
pub extra_args: Vec<String>,
132141
pub env: FxHashMap<String, String>,

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

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

4544
pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result<String> {
4645
let _p = profile("handle_analyzer_status");
@@ -427,7 +426,7 @@ pub fn handle_runnables(
427426
res.push(lsp_ext::Runnable {
428427
range: Default::default(),
429428
label: format!("cargo {} -p {}", cmd, spec.package),
430-
bin: cargo_path()?,
429+
kind: lsp_ext::RunnableKind::Cargo,
431430
args: vec![cmd.to_string(), "--package".to_string(), spec.package.clone()],
432431
extra_args: Vec::new(),
433432
env: FxHashMap::default(),
@@ -439,7 +438,7 @@ pub fn handle_runnables(
439438
res.push(lsp_ext::Runnable {
440439
range: Default::default(),
441440
label: "cargo check --workspace".to_string(),
442-
bin: cargo_path()?,
441+
kind: lsp_ext::RunnableKind::Cargo,
443442
args: vec!["check".to_string(), "--workspace".to_string()],
444443
extra_args: Vec::new(),
445444
env: FxHashMap::default(),
@@ -450,13 +449,6 @@ pub fn handle_runnables(
450449
Ok(res)
451450
}
452451

453-
fn cargo_path() -> Result<String> {
454-
Ok(ra_toolchain::cargo()
455-
.to_str()
456-
.context("Path to `cargo` executable contains invalid UTF8 characters")?
457-
.to_owned())
458-
}
459-
460452
pub fn handle_completion(
461453
world: WorldSnapshot,
462454
params: lsp_types::CompletionParams,
@@ -994,7 +986,7 @@ fn to_lsp_runnable(
994986
Ok(lsp_ext::Runnable {
995987
range: to_proto::range(&line_index, runnable.range),
996988
label,
997-
bin: cargo_path()?,
989+
kind: lsp_ext::RunnableKind::Cargo,
998990
args,
999991
extra_args,
1000992
env: {

crates/rust-analyzer/tests/heavy_tests/main.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ use std::collections::Spam;
5858
eprintln!("completion took {:?}", completion_start.elapsed());
5959
}
6060

61-
fn cargo_path() -> String {
62-
ra_toolchain::cargo().to_str().unwrap().to_owned()
63-
}
64-
6561
#[test]
6662
fn test_runnables_no_project() {
6763
if skip_slow_tests() {
@@ -83,7 +79,7 @@ fn foo() {
8379
{
8480
"args": [ "test" ],
8581
"extraArgs": [ "foo", "--nocapture" ],
86-
"bin": cargo_path(),
82+
"kind": "cargo",
8783
"env": { "RUST_BACKTRACE": "short" },
8884
"cwd": null,
8985
"label": "test foo",
@@ -95,7 +91,7 @@ fn foo() {
9591
{
9692
"args": ["check", "--workspace"],
9793
"extraArgs": [],
98-
"bin": cargo_path(),
94+
"kind": "cargo",
9995
"env": {},
10096
"cwd": null,
10197
"label": "cargo check --workspace",
@@ -145,7 +141,7 @@ fn main() {}
145141
{
146142
"args": [ "test", "--package", "foo", "--test", "spam" ],
147143
"extraArgs": [ "test_eggs", "--exact", "--nocapture" ],
148-
"bin": cargo_path(),
144+
"kind": "cargo",
149145
"env": { "RUST_BACKTRACE": "short" },
150146
"label": "test test_eggs",
151147
"range": {
@@ -157,7 +153,7 @@ fn main() {}
157153
{
158154
"args": [ "check", "--package", "foo" ],
159155
"extraArgs": [],
160-
"bin": cargo_path(),
156+
"kind": "cargo",
161157
"env": {},
162158
"label": "cargo check -p foo",
163159
"range": {
@@ -169,7 +165,7 @@ fn main() {}
169165
{
170166
"args": [ "test", "--package", "foo" ],
171167
"extraArgs": [],
172-
"bin": cargo_path(),
168+
"kind": "cargo",
173169
"env": {},
174170
"label": "cargo test -p foo",
175171
"range": {

docs/dev/lsp-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ interface Runnable {
417417
/// The label to show in the UI.
418418
label: string;
419419
/// The following fields describe a process to spawn.
420-
bin: string;
420+
kind: "cargo" | "rustc" | "rustup";
421421
args: string[];
422422
/// Args for cargo after `--`.
423423
extraArgs: string[];

editors/code/src/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from 'vscode';
33
import * as path from 'path';
44
import * as ra from './lsp_ext';
55

6-
import { Cargo } from './cargo';
6+
import { Cargo } from './toolchain';
77
import { Ctx } from "./ctx";
88

99
const debugOutput = vscode.window.createOutputChannel("Debug");

editors/code/src/lsp_ext.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ export interface RunnablesParams {
4545
textDocument: lc.TextDocumentIdentifier;
4646
position: lc.Position | null;
4747
}
48+
49+
export type RunnableKind = "cargo" | "rustc" | "rustup";
50+
4851
export interface Runnable {
4952
range: lc.Range;
5053
label: string;
51-
bin: string;
54+
kind: RunnableKind;
5255
args: string[];
5356
extraArgs: string[];
5457
env: { [key: string]: string };

editors/code/src/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
33
import * as ra from './lsp_ext';
4+
import * as toolchain from "./toolchain";
45

56
import { Ctx, Cmd } from './ctx';
67
import { startDebugSession, getDebugConfiguration } from './debug';
@@ -175,7 +176,7 @@ export function createTask(spec: ra.Runnable): vscode.Task {
175176
const definition: CargoTaskDefinition = {
176177
type: 'cargo',
177178
label: spec.label,
178-
command: spec.bin,
179+
command: toolchain.getPathForExecutable(spec.kind),
179180
args: spec.extraArgs ? [...spec.args, '--', ...spec.extraArgs] : spec.args,
180181
env: spec.env,
181182
};

editors/code/src/tasks.ts

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

44
// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
55
// our configuration should be compatible with it so use the same key.
@@ -25,7 +25,7 @@ class CargoTaskProvider implements vscode.TaskProvider {
2525
// set of tasks that always exist. These tasks cannot be removed in
2626
// tasks.json - only tweaked.
2727

28-
const cargoPath = getCargoPathOrFail();
28+
const cargoPath = toolchain.cargoPath();
2929

3030
return [
3131
{ command: 'build', group: vscode.TaskGroup.Build },

0 commit comments

Comments
 (0)