@@ -411,40 +411,64 @@ without interfering with classloaders."
411411 :package-version '(cider . " 1.2.0" )
412412 :safe #'booleanp )
413413
414- (defun cider--get-enrich-classpath-lein-script ()
415- " Returns the location of enrich-classpath's lein.sh wrapper script."
416- (when-let ((cider-location (locate-library " cider.el" t )))
417- (concat (file-name-directory cider-location)
418- " lein.sh" )))
419-
420- (defun cider--get-enrich-classpath-clojure-cli-script ()
421- " Returns the location of enrich-classpath's clojure.sh wrapper script."
422- (when-let ((cider-location (locate-library " cider.el" t )))
423- (concat (file-name-directory cider-location)
424- " clojure.sh" )))
414+ (defvar cider--enrich-classpath-script-names
415+ '((lein . " lein.sh" )
416+ (clojure-cli . " clojure.sh" )))
417+
418+ (defun cider--enriched-cmd-p (cmd )
419+ " Test if the shell-quoted CMD contains the name of an enrich-classpath script.
420+ Returns the local path to the script or nil."
421+ (let* ((script-names (map-values cider--enrich-classpath-script-names))
422+ (temp-prefix cider--temp-name-prefix)
423+ (any-name (rx-to-string
424+ `(or (: (or bos " /" ) (or ,@script-names ) (or eos space))
425+ (: , temp-prefix (or ,@script-names )))))
426+ (script (thread-last
427+ (split-string-shell-command cmd)
428+ (seq-filter (lambda (part ) (string-match any-name part)))
429+ (seq-first))))
430+ (when script
431+ (shell-quote-argument script))))
432+
433+ (defun cider--get-enrich-classpath-script (project-type )
434+ " Get or create an executable enrich-classpath script for PROJECT-TYPE.
435+ If `default-directory' is remote, create a copy at
436+ '<remote-tempdir>/.cider__<script-name>__<random>' that deletes itself after
437+ use. The search for '<remote-tempdir>' is handled by tramp and falls back to
438+ `clojure-project-dir' or `default-directory' . Returns nil if anything goes wrong."
439+ (when-let* ((cider-dir (file-name-directory (locate-library " cider.el" t )))
440+ (name (map-elt cider--enrich-classpath-script-names project-type))
441+ (location (concat cider-dir name))
442+ (script (cider--ensure-executable location)))
443+ (if (file-remote-p default-directory)
444+ (with-demoted-errors
445+ " cider: Failed to initialize enrich-classpath on remote."
446+ (thread-first
447+ (cider--make-nearby-temp-copy script)
448+ (cider--ensure-executable)
449+ (cider--inject-self-delete)))
450+ script)))
451+
452+ (defun cider--jack-in-resolve-command-enrich (project-type )
453+ " Conditionally wrap the command for PROJECT-TYPE with an enrich-classpath script.
454+ Resolves to the non-wrapped `cider-jack-in-command' if `cider-enrich-classpath' is nil or the
455+ wrapper-script can't be initialized."
456+ (when-let ((command (cider--resolve-command (cider-jack-in-command project-type))))
457+ (if-let ((wrapper-script (and cider-enrich-classpath
458+ (not (eq system-type 'windows-nt ))
459+ (cider--get-enrich-classpath-script project-type))))
460+ (concat " bash "
461+ (shell-quote-argument (file-local-name wrapper-script)) " "
462+ command)
463+ command)))
425464
426465(defun cider-jack-in-resolve-command (project-type )
427466 " Determine the resolved file path to `cider-jack-in-command' .
428467Throws an error if PROJECT-TYPE is unknown."
429468 (pcase project-type
430- ('lein (let ((r (cider--resolve-command cider-lein-command)))
431- (if (and cider-enrich-classpath
432- (not (eq system-type 'windows-nt ))
433- (executable-find (cider--get-enrich-classpath-lein-script)))
434- (concat " bash " ; ; don't assume lein.sh is executable - MELPA might change that
435- (cider--get-enrich-classpath-lein-script)
436- " "
437- r)
438- r)))
469+ ('lein (cider--jack-in-resolve-command-enrich 'lein ))
439470 ('boot (cider--resolve-command cider-boot-command))
440- ('clojure-cli (if (and cider-enrich-classpath
441- (not (eq system-type 'windows-nt ))
442- (executable-find (cider--get-enrich-classpath-clojure-cli-script)))
443- (concat " bash " ; ; don't assume clojure.sh is executable - MELPA might change that
444- (cider--get-enrich-classpath-clojure-cli-script)
445- " "
446- (cider--resolve-command cider-clojure-cli-command))
447- (cider--resolve-command cider-clojure-cli-command)))
471+ ('clojure-cli (cider--jack-in-resolve-command-enrich 'clojure-cli ))
448472 ('babashka (cider--resolve-command cider-babashka-command))
449473 ; ; here we have to account for the possibility that the command is either
450474 ; ; "npx shadow-cljs" or just "shadow-cljs"
@@ -1661,7 +1685,11 @@ PARAMS is a plist with the following keys (non-exhaustive list)
16611685 (command-resolved (cider-jack-in-resolve-command project-type))
16621686 ; ; TODO: global-options are deprecated and should be removed in CIDER 2.0
16631687 (command-global-opts (cider-jack-in-global-options project-type))
1664- (command-params (cider-jack-in-params project-type)))
1688+ (command-params (cider-jack-in-params project-type))
1689+ ; ; ignore `cider-enrich-classpath' if the jack-in-command does not include
1690+ ; ; the necessary wrapper script at this point
1691+ (cider-enrich-classpath (and cider-enrich-classpath
1692+ (cider--enriched-cmd-p command-resolved))))
16651693 (if command-resolved
16661694 (with-current-buffer (or (plist-get params :--context-buffer )
16671695 (current-buffer ))
@@ -2114,13 +2142,11 @@ M-2 \\[cider-jack-in-universal]."
21142142 (cider-jack-in-clj arg))))
21152143
21162144
2117- ; ; TODO: Implement a check for command presence over tramp
21182145(defun cider--resolve-command (command )
2119- " Find COMMAND in exec path (see variable `exec-path' ).
2120- Return nil if not found. In case `default-directory' is non-local we
2121- assume the command is available."
2122- (when-let* ((command (or (and (file-remote-p default-directory) command)
2123- (executable-find command)
2146+ " Test if COMMAND exists, is executable and shell-quote it.
2147+ Return nil otherwise. When `default-directory' is remote, the check is
2148+ performed by tramp."
2149+ (when-let* ((command (or (executable-find command :remote )
21242150 (executable-find (concat command " .bat" )))))
21252151 (shell-quote-argument command)))
21262152
0 commit comments