From 47f52ef11dc644618696639009c884f228c166bf Mon Sep 17 00:00:00 2001 From: Yuu Yin Date: Mon, 13 Feb 2023 13:00:41 -0300 Subject: [PATCH 1/2] fix(rustic-babel): disable toolchain when invalid/unneeded When the user installs Rust tools using a method other than rustup, e.g. using an operating system's package manager, cargo generally has no support for toolchain specification. In such case, the user can then `nil', or `""', so that the respective functions in `rustic-babel' will remove the toolchain from params, i.e. only toolchain has a valid value if Cargo has toolchain support. See also: https://github.com/brotzeit/rustic/pull/279#issuecomment-1336164427 Fixes #498 introduced in 80d05c4152a05c1a1de1e99cf4a9eeb95fb734d3 Co-authored-by: Sibi Prabakaran --- rustic-babel.el | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rustic-babel.el b/rustic-babel.el index f728c31..04b957d 100644 --- a/rustic-babel.el +++ b/rustic-babel.el @@ -58,6 +58,21 @@ considered." (defvar rustic-babel-spinner nil) +(defun rustic-babel-cargo-params (toolchain) + "Return a list of cargo commands for babel evaluation. +If TOOLCHAIN has a value other than `nil' or `+', then add +TOOLCHAIN to the list, else return a list of cargo commands with +no toolchain. Users may need non-toolchain when the computing +environment installed Cargo with a method other than rustup such +as with an operating system's package manager, in which case +Cargo has no support for toolchain specification." + (remove nil (list "cargo" + (unless (or (null toolchain) + (equal "+" toolchain)) + toolchain) + "build" + "--quiet"))) + (defun rustic-babel-eval (dir toolchain-kw-or-string main-p) "Start a rust babel compilation process. Compilation is started in directory DIR with appropriate @@ -69,9 +84,12 @@ should be wrapped in which case we will disable rustfmt." (toolchain (cond ((eq toolchain-kw-or-string 'nightly) "+nightly") ((eq toolchain-kw-or-string 'beta) "+beta") ((eq toolchain-kw-or-string 'stable) "+stable") + ((or (null rustic-babel-default-toolchain) + (eq 0 (string-blank-p rustic-babel-default-toolchain))) + nil) (toolchain-kw-or-string (format "+%s" toolchain-kw-or-string)) (t (format "+%s" rustic-babel-default-toolchain)))) - (params (list "cargo" toolchain "build" "--quiet")) + (params (rustic-babel-cargo-params toolchain)) (inhibit-read-only t)) (rustic-compilation-setup-buffer err-buff dir 'rustic-compilation-mode) (when rustic-babel-display-compilation-buffer @@ -122,7 +140,7 @@ execution with rustfmt." ;; run project (let* ((err-buff (get-buffer-create rustic-babel-compilation-buffer-name)) - (params (list "cargo" toolchain "run" "--quiet")) + (params (rustic-babel-cargo-params toolchain)) (inhibit-read-only t)) (rustic-make-process :name rustic-babel-process-name From f16f5ed92b8c7e33b32fef0ef550388e6650643f Mon Sep 17 00:00:00 2001 From: Troy Hinckley Date: Mon, 8 Jul 2024 10:51:49 -0500 Subject: [PATCH 2/2] Refactor default toolchain for babel blocks We are setting the default to `nil`, which means use the default provided by cargo and don't specify it explicitly. If overridden with a string value, that will be used instead and passed as +. --- rustic-babel.el | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/rustic-babel.el b/rustic-babel.el index 04b957d..9879c2e 100644 --- a/rustic-babel.el +++ b/rustic-babel.el @@ -32,11 +32,12 @@ :type 'boolean :group 'rustic-babel) -(defcustom rustic-babel-default-toolchain "stable" +(defcustom rustic-babel-default-toolchain nil "Active toolchain for babel blocks. When passing a toolchain to a block as argument, this variable won't be considered." - :type 'string + :type '(choice (string :tag "String") + (const :tag "Nil" nil)) :group 'rustic-babel) (defvar rustic-babel-buffer-name '((:default . "*rust-babel*"))) @@ -58,21 +59,6 @@ considered." (defvar rustic-babel-spinner nil) -(defun rustic-babel-cargo-params (toolchain) - "Return a list of cargo commands for babel evaluation. -If TOOLCHAIN has a value other than `nil' or `+', then add -TOOLCHAIN to the list, else return a list of cargo commands with -no toolchain. Users may need non-toolchain when the computing -environment installed Cargo with a method other than rustup such -as with an operating system's package manager, in which case -Cargo has no support for toolchain specification." - (remove nil (list "cargo" - (unless (or (null toolchain) - (equal "+" toolchain)) - toolchain) - "build" - "--quiet"))) - (defun rustic-babel-eval (dir toolchain-kw-or-string main-p) "Start a rust babel compilation process. Compilation is started in directory DIR with appropriate @@ -84,12 +70,10 @@ should be wrapped in which case we will disable rustfmt." (toolchain (cond ((eq toolchain-kw-or-string 'nightly) "+nightly") ((eq toolchain-kw-or-string 'beta) "+beta") ((eq toolchain-kw-or-string 'stable) "+stable") - ((or (null rustic-babel-default-toolchain) - (eq 0 (string-blank-p rustic-babel-default-toolchain))) - nil) (toolchain-kw-or-string (format "+%s" toolchain-kw-or-string)) - (t (format "+%s" rustic-babel-default-toolchain)))) - (params (rustic-babel-cargo-params toolchain)) + (rustic-babel-default-toolchain (format "+%s" rustic-babel-default-toolchain)) + (t nil))) + (params (remove nil (list "cargo" toolchain "build" "--quiet"))) (inhibit-read-only t)) (rustic-compilation-setup-buffer err-buff dir 'rustic-compilation-mode) (when rustic-babel-display-compilation-buffer @@ -140,7 +124,7 @@ execution with rustfmt." ;; run project (let* ((err-buff (get-buffer-create rustic-babel-compilation-buffer-name)) - (params (rustic-babel-cargo-params toolchain)) + (params (remove nil (list "cargo" toolchain "run" "--quiet"))) (inhibit-read-only t)) (rustic-make-process :name rustic-babel-process-name