diff --git a/src/babashka/impl/nrepl_server.clj b/src/babashka/impl/nrepl_server.clj index 89a46ec7..2f65dfce 100644 --- a/src/babashka/impl/nrepl_server.clj +++ b/src/babashka/impl/nrepl_server.clj @@ -41,11 +41,10 @@ (defn eval-msg [ctx o msg #_threads] (try (let [ns-str (get msg :ns) + sci-ns (when ns-str (sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false)) sw (StringWriter.)] (sci/with-bindings (cond-> {sci/out sw} - ns-str - (assoc vars/current-ns - (sci-utils/namespace-object (:env ctx) (symbol ns-str) nil false))) + sci-ns (assoc vars/current-ns sci-ns)) (when @dev? (println "current ns" (vars/current-ns-name))) (let [session (get msg :session "none") id (get msg :id "unknown")] diff --git a/test/babashka/impl/nrepl_server_test.clj b/test/babashka/impl/nrepl_server_test.clj index 4175de2b..42f3bca1 100644 --- a/test/babashka/impl/nrepl_server_test.clj +++ b/test/babashka/impl/nrepl_server_test.clj @@ -68,7 +68,14 @@ "session" session "id" (new-id!) "ns" "ns0"}) - (is (= ":foo0" (:value (read-reply in session @id))))))) + (is (= ":foo0" (:value (read-reply in session @id))))) + (testing "providing an ns value of a non-existing namespace falls back the last defined namespace" + (bencode/write-bencode os {"op" "eval" + "code" "(foo)" + "session" session + "id" (new-id!) + "ns" "unicorn"}) + (is (= ":foo1" (:value (read-reply in session @id))))))) (testing "load-file" (bencode/write-bencode os {"op" "load-file" "file" "(ns foo) (defn foo [] :foo)" "session" session "id" (new-id!)}) (read-reply in session @id)