Skip to content

Commit e83488b

Browse files
committed
Handle Windows and remote buffers using default formatters
Various fixes to ensure that the default formatters work over Tramp and on Windows: 1. Replace the apheleia-npx command with npx when the buffer is remote or running on Windows. This uses the built-in npx handling to lookup the binary, so npx never gets called. Since the apheleia-npx script isn't available on remote hosts, it makes sense to remove this. The same is done for Windows to capture the case of using WSL, in which case calling bash will enter WSL and so the script cannot be accessed using the Windows path. 2. Use local file name when returning binary path. 3. Don't use executable-find if the binary is already an absolute path. 4. Pass the -l option to sh to ensure that it loads the user's profile.
1 parent 2c8e822 commit e83488b

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog].
77
### Bugs fixed
88
* A formatter that moves a line to the top of the file would sometimes
99
place it as the second line instead ([#299]).
10+
* Handle remote buffers using default formatters.
1011

1112
### Formatters
1213
* Format Bazel files according to their type

apheleia-formatters.el

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,12 @@ NO-QUERY, and CONNECTION-TYPE."
571571
(ignore name noquery connection-type)
572572
(let* ((run-on-remote (and (eq apheleia-remote-algorithm 'remote)
573573
remote))
574-
;; Resolve the formatter executable's path to ensure it's
575-
;; found
576-
(command (cons (executable-find (car command) run-on-remote) (cdr command)))
574+
;; Resolve the formatter executable's path to ensure it's
575+
;; found
576+
(command (cons (if (file-name-absolute-p (car command))
577+
(car command)
578+
(executable-find (car command) run-on-remote))
579+
(cdr command)))
577580
(stderr-file (apheleia--make-temp-file run-on-remote "apheleia"))
578581
(args
579582
(append
@@ -630,9 +633,9 @@ NO-QUERY, and CONNECTION-TYPE."
630633
(apheleia--log
631634
'process
632635
"Using process-file to create process %s with %S"
633-
name (list shell "-c" shell-command))
636+
name (list shell "-lc" shell-command))
634637
(process-file
635-
shell nil (nth 2 args) nil "-c" shell-command))
638+
shell nil (nth 2 args) nil "-lc" shell-command))
636639
(delete-file remote-stdin))))
637640
(stdin
638641
(apheleia--log
@@ -968,6 +971,13 @@ cmd is to be run."
968971
(setf (apheleia-formatter--name context) name)
969972
(setf (apheleia-formatter--stdin context) stdin)
970973
(setf (apheleia-formatter--remote context) remote)
974+
975+
;; Just use npx when buffer is remote or we're running on Windows,
976+
;; since in these cases bash won't be able to find the script
977+
(when (and (member "apheleia-npx" command)
978+
(or run-on-remote (eq system-type 'windows-nt)))
979+
(setq command (cons 'npx (remove "apheleia-npx" command))))
980+
971981
;; TODO: Support arbitrary package managers, not just NPM.
972982
(when (memq 'npx command)
973983
(setq command (remq 'npx command))
@@ -982,7 +992,7 @@ cmd is to be run."
982992
".bin"
983993
(expand-file-name "node_modules" project-dir)))))
984994
(when (file-executable-p binary)
985-
(setcar command binary))))))
995+
(setcar command (file-local-name binary)))))))
986996

987997
(when (or (memq 'file command) (memq 'filepath command))
988998
;; Fail when using file but not as the first formatter in this
@@ -1110,8 +1120,9 @@ purposes."
11101120
process-environment))
11111121
(ctx
11121122
(apheleia--formatter-context formatter command remote stdin)))
1113-
(if (executable-find (apheleia-formatter--arg1 ctx)
1114-
(eq apheleia-remote-algorithm 'remote))
1123+
(if (or (file-name-absolute-p (apheleia-formatter--arg1 ctx))
1124+
(executable-find (apheleia-formatter--arg1 ctx)
1125+
(eq apheleia-remote-algorithm 'remote)))
11151126
(apheleia--execute-formatter-process
11161127
:ctx ctx
11171128
:callback

0 commit comments

Comments
 (0)