Fork me on GitHub
#biff
<
2023-05-04
>
sergey.shvets19:05:00

Hey, is there a good way to integrate REPL with an editor. I'm using Calva + connect to remote repl started with bb dev. But the problem is that it doesn't track if something hasn't reloaded. E.g. I forgot an import, and the function won't compile, but I didn't have any warning.

Jacob O'Bryant20:05:40

I'm a little fuzzy on what exactly is happening--do you mean when you try to evaluate the function, you don't get an error message? if you evaluate the whole file, including the ns form at the top, does that fix the problem? also FYI, biff will evaluate files whenever you save them, independent of your editor. so if you're having any editor issues you can always try saving all modified files and see if that fixes the issue.

sergey.shvets20:05:11

Yes, Biff evaluates files on save, but if I make any error in a file, it spits it into a console where I launched bb dev. What I want to have ideally, is to be able to see that error in my REPL or even better highlight the problem in my source file. Have you seen a setup like that? If not, I'll try to figure it out when I have some time and report back with findings.

sergey.shvets20:05:34

I guess if I evaluate files on save in remote REPL it will get me half way there? Will it mess with reload somehow?

Jacob O'Bryant22:05:55

Ah, got it. On the Biff side, you'd need a modified version of https://github.com/jacobobryant/biff/blob/master/src/com/biffweb.clj#L201 that returns an exception if there was one. Looking at https://github.com/jacobobryant/biff/blob/master/src/com/biffweb/impl/util/reload.clj, I see you could modify your project's on-save fn to check for an exception like so:

(defn on-save [ctx]
  (biff/add-libs)
  (biff/eval-files! ctx)
  (when-some [e (:clojure.tools.namespace.reload/error
                 @com.biffweb.impl.util.reload/global-tracker)]
    (prn :got-an-error e))
  ...)
(Accessing com.biffweb.impl.* namespaces in general isn't recommended since they're not part of Biff's public interface and are thus subject to breaking changes at any time, but it's ok for temporary workarounds at least. I should just remove https://github.com/jacobobryant/biff/blob/master/src/com/biffweb.clj#L206, so the contents of global-tracker would be returned by the eval-files! function) So you can try to replace that prn with some code that will send the exception to your editor somehow--I'm not familiar with VS code or Calva myself, so can't give any pointers on that unfortunately. This is similar to https://github.com/jacobobryant/biff/issues/117, which discusses sending the exception to the browser via an htmx websocket. > I guess if I evaluate files on save in remote REPL it will get me half way there? Will it mess with reload somehow? Since files are already evaluated on save by Biff, do you mean modify the on-save function so it somehow tells Calva to evaluate the files (presumably with any output going to the repl output)? If there's a way to do that, that would probably work. You'd probably want to remove the (biff/eval-files! ctx) line so that Biff isn't also trying to evaluate the files.

Jacob O'Bryant23:05:31

Let me know if you do get something working. If it's general enough I could add it/part of it to biff, or if not, at least add some how-to documentation.

sergey.shvets23:05:12

I meant just ask Calva to send the whole namespace I saved to Biff's REPL and don't mess with the on-save function. It already has that ability as a system setting. That will catch errors in the current namespace, but not sure that it will catch the problems in any other namespaces that depend on this one and might have been broken by a change. But the latter happens not too often.

sergey.shvets23:05:38

I'll play with it and report back. Thanks for the pointers!

Jacob O'Bryant23:05:54

oh yes, that makes sense. Good luck!

Jacob O'Bryant23:05:11

FYI if Calva doesn't evaluate the dependent namespaces, you'll need to e.g. at least evaluate your main namespace whenever you add new routes. If possible I'd look to see if Calva can just evaluate the dependent namespaces as well. If not maybe you could get away with leaving the biff/eval-files! call in.

m.q.warnock10:05:02

FWIW, as an emacs/cider user, in addition to doing a ctrl-c-k in a file I've just been working on and want to make sure compiles, I have a little bash script tailing (with inotifywait) a log which includes the eval-files! output, and when it sees 'xception', it changes a little indicator in polybar (the status bar I use with i3 tiling-wm). I have the same kind of thing indicating failures in webpack recompilation, for my front-end. Obviously, you're unlikely to be using the same combination of software, but maybe there's an equivalent that would make sense, and be easy to implement.

👌 2