aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrédéric Giquel <frederic.giquel@laposte.net>2025-12-23 20:24:34 +0100
committerGitHub <noreply@github.com>2025-12-23 11:24:34 -0800
commit426616cf1799dbce89800ae2fe9f155311436503 (patch)
tree897ca1ec66b9d8d1ffbbea33aaf9ca3e2d1a9142
parent8a6b75008d0cb5c5dabb08281ad2393ba4341d1c (diff)
apheleia-npx: return silently when formatter is missing (#383)
Behavior of `aphaleia-npx` currently differs from other formatters by triggering an error when formatter is missing (causing a message to be displayed in minibuffer and an error buffer to be created). This PR adds a test to check if command is on user's PATH before executing it in order to make `aphaleia-npx` silently ends when the formatter command is not found at project and system level.
-rw-r--r--CHANGELOG.md1
-rw-r--r--apheleia-formatters.el28
-rwxr-xr-xscripts/formatters/apheleia-npx7
3 files changed, 32 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 09e3bd4..3cda2e9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog].
### Bugs fixed
* `shfmt` did not work with `apheleia-formatters-respect-indent-level`
+* `apheleia-npx` did return an error when formatter was missing
[#380]: https://github.com/radian-software/apheleia/pull/380
[#382]: https://github.com/radian-software/apheleia/pull/382
diff --git a/apheleia-formatters.el b/apheleia-formatters.el
index 9bc39f8..53e7387 100644
--- a/apheleia-formatters.el
+++ b/apheleia-formatters.el
@@ -467,6 +467,23 @@ in the buffer."
'mhtml-submode)
#'mhtml-mode))
+;; Expected exit code and stderr output for script when real
+;; formatter is not available
+(defvar apheleia-script--formatter-not-available
+ '(:exit-code 100 :stderr "formatter_not_available"))
+
+(defun apheleia-script--formatter-not-available-p (ctx stderr)
+ "Check if script reports that no formatter has been found.
+CTX is a formatter process context.
+STDERR is the stderr output of process."
+ (and (eq (apheleia-formatter--exit-status ctx)
+ (plist-get apheleia-script--formatter-not-available :exit-code))
+ (string= stderr
+ (concat
+ (apheleia-formatter--arg1 ctx) ":"
+ (plist-get apheleia-script--formatter-not-available
+ :stderr)))))
+
;;;###autoload
(defcustom apheleia-mode-predicates '(apheleia-mhtml-mode-predicate)
"List of predicates that check for sneaky major modes.
@@ -747,9 +764,14 @@ its exit status is 0."
proc-exit-status)
(let ((exit-ok (and
(not proc-interrupted)
- (funcall
- (or exit-status #'zerop)
- (apheleia-formatter--exit-status ctx)))))
+ (or
+ (funcall
+ (or exit-status #'zerop)
+ (apheleia-formatter--exit-status ctx))
+ (apheleia-script--formatter-not-available-p
+ ctx
+ (with-current-buffer stderr
+ (string-trim (buffer-string))))))))
;; Append standard-error from current formatter
;; to log buffer when
;; `apheleia-log-only-errors' is nil or the
diff --git a/scripts/formatters/apheleia-npx b/scripts/formatters/apheleia-npx
index a540f9b..72cafb2 100755
--- a/scripts/formatters/apheleia-npx
+++ b/scripts/formatters/apheleia-npx
@@ -70,4 +70,9 @@ if [[ -d $dir ]]; then
fi
# Fall back to executing the command if it's installed and on the user's $PATH
-exec "$@"
+if command -v "$1" >/dev/null; then
+ exec "$@"
+fi
+
+echo "apheleia-npx:formatter_not_available" >&2
+exit 100