Skip to content

Handle Windows and remote buffers using default formatters #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog].
### Bugs fixed
* A formatter that moves a line to the top of the file would sometimes
place it as the second line instead ([#299]).
* Handle remote buffers using default formatters.

### Formatters
* Format Bazel files according to their type
Expand Down
27 changes: 19 additions & 8 deletions apheleia-formatters.el
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,12 @@ NO-QUERY, and CONNECTION-TYPE."
(ignore name noquery connection-type)
(let* ((run-on-remote (and (eq apheleia-remote-algorithm 'remote)
remote))
;; Resolve the formatter executable's path to ensure it's
;; found
(command (cons (executable-find (car command) run-on-remote) (cdr command)))
;; Resolve the formatter executable's path to ensure it's
;; found
(command (cons (if (file-name-absolute-p (car command))
(car command)
(executable-find (car command) run-on-remote))
(cdr command)))
(stderr-file (apheleia--make-temp-file run-on-remote "apheleia"))
(args
(append
Expand Down Expand Up @@ -630,9 +633,9 @@ NO-QUERY, and CONNECTION-TYPE."
(apheleia--log
'process
"Using process-file to create process %s with %S"
name (list shell "-c" shell-command))
name (list shell "-lc" shell-command))
(process-file
shell nil (nth 2 args) nil "-c" shell-command))
shell nil (nth 2 args) nil "-lc" shell-command))
(delete-file remote-stdin))))
(stdin
(apheleia--log
Expand Down Expand Up @@ -968,6 +971,13 @@ cmd is to be run."
(setf (apheleia-formatter--name context) name)
(setf (apheleia-formatter--stdin context) stdin)
(setf (apheleia-formatter--remote context) remote)

;; Just use npx when buffer is remote or we're running on Windows,
;; since in these cases bash won't be able to find the script
(when (and (member "apheleia-npx" command)
(or run-on-remote (eq system-type 'windows-nt)))
(setq command (cons 'npx (remove "apheleia-npx" command))))
Comment on lines +975 to +979
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be an issue for any of the scripts in the Apheleia scripts directory, just just the npx one. What would you think of having Apheleia copy the script to the remote host before first execution, if anything apheleia-prefixed is used in the command?


;; TODO: Support arbitrary package managers, not just NPM.
(when (memq 'npx command)
(setq command (remq 'npx command))
Expand All @@ -982,7 +992,7 @@ cmd is to be run."
".bin"
(expand-file-name "node_modules" project-dir)))))
(when (file-executable-p binary)
(setcar command binary))))))
(setcar command (file-local-name binary)))))))

(when (or (memq 'file command) (memq 'filepath command))
;; Fail when using file but not as the first formatter in this
Expand Down Expand Up @@ -1110,8 +1120,9 @@ purposes."
process-environment))
(ctx
(apheleia--formatter-context formatter command remote stdin)))
(if (executable-find (apheleia-formatter--arg1 ctx)
(eq apheleia-remote-algorithm 'remote))
(if (or (file-name-absolute-p (apheleia-formatter--arg1 ctx))
(executable-find (apheleia-formatter--arg1 ctx)
(eq apheleia-remote-algorithm 'remote)))
(apheleia--execute-formatter-process
:ctx ctx
:callback
Expand Down