From 032fca9572ee8d1dca368a224c09379a851afb9c Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Thu, 28 Mar 2024 20:39:21 -0700 Subject: [PATCH 1/2] Make `cargo run` always available for binaries Previously, items for `cargo test` and `cargo check` would appear as in the `Select Runnable` quick pick that appears when running `rust-analyzer: Run`, but `run` would only appear as a runnable if a `main`` function was selected in the editor. This change adds `cargo run` as an always available runnable command for binary packages. This makes it easier to develop cli / tui applications, as now users can run application from anywhere in their codebase. --- crates/rust-analyzer/src/handlers/request.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 4d4103a7ad..c5796ac071 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -836,10 +836,13 @@ pub(crate) fn handle_runnables( let config = snap.config.runnables(); match cargo_spec { Some(spec) => { - let all_targets = !snap.analysis.is_crate_no_std(spec.crate_id)?; - for cmd in ["check", "test"] { + for cmd in ["check", "run", "test"] { + if cmd == "run" && spec.target_kind != TargetKind::Bin { + continue; + } let mut cargo_args = vec![cmd.to_owned(), "--package".to_owned(), spec.package.clone()]; + let all_targets = cmd != "run" && !snap.analysis.is_crate_no_std(spec.crate_id)?; if all_targets { cargo_args.push("--all-targets".to_owned()); } From aa499266ad1481d2258345616b765dd15983a456 Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 24 Apr 2024 01:29:31 -0700 Subject: [PATCH 2/2] fix: move no_std check out of loop --- crates/rust-analyzer/src/handlers/request.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index c5796ac071..004e69dd32 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -836,13 +836,14 @@ pub(crate) fn handle_runnables( let config = snap.config.runnables(); match cargo_spec { Some(spec) => { + let is_crate_no_std = snap.analysis.is_crate_no_std(spec.crate_id)?; for cmd in ["check", "run", "test"] { if cmd == "run" && spec.target_kind != TargetKind::Bin { continue; } let mut cargo_args = vec![cmd.to_owned(), "--package".to_owned(), spec.package.clone()]; - let all_targets = cmd != "run" && !snap.analysis.is_crate_no_std(spec.crate_id)?; + let all_targets = cmd != "run" && !is_crate_no_std; if all_targets { cargo_args.push("--all-targets".to_owned()); }