rust-lang / rust-lang/rust-mode

Support multiple binary targets in `rust-run`

Open
#471 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Emacs Lisp
Stars
1.3k
Forks
198
PR merge metrics
No merged PRs in 30d

Description

rust-run fails if cargo run needs a --bin argument, which is the case if:

  1. The workspace has multiple binary targets.
  2. No target is listed as the package's default-run

It is probably reasonably easy to support this. A possible interface update to rust-run could simply prompt for a target name iff both conditions above hold (multiple binary target, no default) OR it's called with an argument. The chosen target's name could be saved somewhere so further invocations of rust-run can just reuse it (unless called with an arg)

This is a quick and dirty way to get all binary targets from the current workspace, and the default target:

(let* ((json (with-temp-buffer
               (call-process "cargo" nil (current-buffer) nil
                             "metadata" "--format-version" "1")
               (goto-char (point-min))
               (json-parse-buffer)))
       ;; !! this is a bit dirty. !!
       (members (seq-map (lambda (wm)
                           (car (split-string wm " ")))
                         (gethash "workspace_members" json)))
       (targets
        (-flatten ; from dash.el, but not strictly required.
         (seq-map (lambda (v)
                    (seq-keep
                     (lambda (target)
                       (when (seq-contains-p
                              (gethash "kind" target) "bin" 'string=)
                         (gethash "name" target))) v))
                  (seq-map (lambda (package) (gethash "targets" package))
                           (seq-filter
                            (lambda (pkg) (seq-contains-p members (gethash "name" pkg)))
                            (gethash "packages" json))))))
       (default-run (gethash "default_run"
                             (car
                              (seq-filter
                               (lambda (item) (string= "raoc2021" (gethash "name" item)))
                               (gethash "packages" json))))))
  (message "Workspace members: %s" members)
  (message "Binary targets: %s" targets)
  (message "Default run target is: %s" default-run))

What do you think?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the rust-run entry point and inspect how it invokes cargo run and handles arguments. Implement target selection for workspaces with multiple binaries, including reuse of a chosen target, and verify that rust-run works both with and without an explicit argument.

Written by the indexing model from the issue text.

Assessment

Tech stack
emacs-lisp, rust
Domain
cli, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.