Skip to content

Commit 2576511

Browse files
author
Bruce Hauman
committed
small update for autostarting nrepl functionality
1 parent 2cb14bc commit 2576511

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/clojure_mcp/nrepl_launcher.clj

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@
6767
(when process
6868
;; Add shutdown hook
6969
(try
70-
(.addShutdownHook
70+
(.addShutdownHook
7171
(Runtime/getRuntime)
7272
(Thread. #(destroy-nrepl-process process)))
7373
(log/debug "Added shutdown hook for nREPL process cleanup")
7474
(catch Exception e
75-
(log/warn e "Failed to add shutdown hook for process cleanup")))
75+
(log/warn e "Failed to add shutdown hook for process cleanup")))
7676
process))
7777

7878
(defn wait-for-port
@@ -199,13 +199,13 @@
199199
(do
200200
(log/debug "Port already provided without start command, skipping auto-start")
201201
false)
202-
202+
203203
;; CLI condition: start-nrepl-cmd AND project-dir provided (regardless of port)
204204
(and start-nrepl-cmd project-dir)
205205
(do
206206
(log/info "Auto-start condition met: CLI args provided")
207207
true)
208-
208+
209209
;; Config file condition
210210
:else
211211
(if-let [config (load-config-if-exists project-dir)]
@@ -216,33 +216,39 @@
216216
false)
217217
false))))
218218

219+
(defn add-project-dir [nrepl-args]
220+
(if (:start-nrepl-cmd nrepl-args)
221+
(assoc nrepl-args :project-dir (System/getProperty "user.dir"))
222+
nrepl-args))
223+
219224
(defn maybe-start-nrepl-process
220225
"Main wrapper that conditionally starts an nREPL process.
221226
Returns updated nrepl-args with discovered :port if process was started,
222227
otherwise returns nrepl-args unchanged."
223228
[nrepl-args]
224-
(if (should-start-nrepl? nrepl-args)
225-
(let [;; Load config and merge with CLI args (CLI takes precedence)
226-
config (load-config-if-exists (:project-dir nrepl-args))
227-
merged-args (merge config nrepl-args)
228-
{:keys [start-nrepl-cmd parse-nrepl-port port]
229-
:or {parse-nrepl-port true}} merged-args]
229+
(let [nrepl-args' (add-project-dir nrepl-args)]
230+
(if (should-start-nrepl? nrepl-args')
231+
(let [;; Load config and merge with CLI args (CLI takes precedence)
232+
config (load-config-if-exists (:project-dir nrepl-args'))
233+
merged-args (merge config nrepl-args')
234+
{:keys [start-nrepl-cmd parse-nrepl-port port]
235+
:or {parse-nrepl-port true}} merged-args]
230236
;; Validate: if parse-nrepl-port is false, port must be provided
231-
(when (and (false? parse-nrepl-port) (not port))
232-
(throw
233-
(ex-info "When :parse-nrepl-port is false, :port must be provided"
234-
{:start-nrepl-cmd start-nrepl-cmd
235-
:parse-nrepl-port parse-nrepl-port})))
236-
(log/info "Starting nREPL process automatically")
237-
(let [{:keys [port process]} (start-nrepl-process merged-args)]
238-
(if port
239-
(do
240-
(log/info (str "Using discovered port: " port))
241-
(assoc nrepl-args :port port :nrepl-process process))
237+
(when (and (false? parse-nrepl-port) (not port))
238+
(throw
239+
(ex-info "When :parse-nrepl-port is false, :port must be provided"
240+
{:start-nrepl-cmd start-nrepl-cmd
241+
:parse-nrepl-port parse-nrepl-port})))
242+
(log/info "Starting nREPL process automatically")
243+
(let [{:keys [port process]} (start-nrepl-process merged-args)]
244+
(if port
245+
(do
246+
(log/info (str "Using discovered port: " port))
247+
(assoc nrepl-args :port port :nrepl-process process))
242248
;; If parse-nrepl-port was false, we still started the process
243249
;; but didn't get a port - this is an error for our use case
244-
(throw (ex-info "nREPL process started but no port available"
245-
{:start-nrepl-cmd start-nrepl-cmd})))))
246-
(do
247-
(log/debug "Auto-start conditions not met, using existing behavior")
248-
nrepl-args)))
250+
(throw (ex-info "nREPL process started but no port available"
251+
{:start-nrepl-cmd start-nrepl-cmd})))))
252+
(do
253+
(log/debug "Auto-start conditions not met, using existing behavior")
254+
nrepl-args))))

0 commit comments

Comments
 (0)