Skip to content

Commit

Permalink
Allow flymake-ruff to use Ruff config files from remote TRAMP hosts
Browse files Browse the repository at this point in the history
Don't pollute /tmp

Fix error on directory existing

Cache config and use tramp-handle-* funcitons to stop TRAMP reentrant errors
  • Loading branch information
struan-robertson committed Dec 28, 2024
1 parent b877f47 commit e880aa6
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions flymake-ruff.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@
'(".ruff.toml" "ruff.toml" "pyproject.toml")
"Default configuration files supported by Ruff.")

(defun flymake-ruff--get-config ()
"Look for configuration files supported by Ruff in project root.
When project is on remote host, cache config using `default-directory' as key."
(if (file-remote-p default-directory)
(let ((cache-dir (expand-file-name
(concat "ruff-config-" (sha1 default-directory))
"/tmp")))
(if (file-directory-p cache-dir)
;; Return cached config if exists
(seq-find #'file-readable-p
(mapcar (lambda (f)
(expand-file-name f cache-dir))
flymake-ruff--default-configs))
;; Create cache and return config path
(when-let* ((project-current (project-current))
(config (seq-find
#'tramp-handle-file-readable-p
(mapcar (lambda (f)
(expand-file-name f (project-root project-current)))
flymake-ruff--default-configs)))
(temp-file (expand-file-name (file-name-nondirectory config) cache-dir))
(temp-copy (tramp-handle-file-local-copy config)))
(message "first")
(make-directory cache-dir)
(copy-file temp-copy temp-file t)
(delete-file temp-copy)
temp-file)))

;; Local project path
(when-let ((project-current (project-current)))
(seq-find #'file-readable-p
(mapcar (lambda (f)
(expand-file-name f (project-root project-current)))
flymake-ruff--default-configs)))))

(defun flymake-ruff--check-buffer ()
"Generate a list of diagnostics for the current buffer."
(let ((code-buffer (current-buffer))
Expand All @@ -52,14 +87,8 @@
;; check if the current buffer belongs to a project before
;; trying to build a path using `project-current' otherwise it
;; will fail silently
(let* ((config (and (project-current)
(seq-find
#'file-readable-p
(mapcar
(lambda (f)
(expand-file-name f (project-root (project-current))))
flymake-ruff--default-configs))))
(args (if config
(let* ((config (flymake-ruff--get-config))
(args (if config
;; for version > 0.5 the work "check" is
;; included so we need to extract it and put it
;; before --config argument
Expand Down Expand Up @@ -94,6 +123,14 @@
"Load hook for the current buffer to tell flymake to run checker."
(interactive)
(when (derived-mode-p 'python-mode 'python-ts-mode)
;; If cached TRAMP config file exists for current buffer, remove it so that cache can be refreshed on reverting buffer.
(when-let* ((_remote (file-remote-p default-directory))
(cache-dir (expand-file-name
(concat "ruff-config-" (sha1 default-directory))
"/tmp"))
(_cache_exist (file-directory-p cache-dir)))
(delete-directory cache-dir t))

(add-hook 'flymake-diagnostic-functions #'flymake-ruff--run-checker nil t)))

(defun flymake-ruff--run-checker (report-fn &rest _args)
Expand Down

0 comments on commit e880aa6

Please sign in to comment.