Fork me on GitHub
#cider
<
2022-03-12
>
David Pham05:03:12

I recently have cider freezing my emacs when I write a namespace and a slash sign (which I have to hit C-g to unfreeze), after cancellation I see an error message in the repl stating it was from nrepl. For example clojure.string/ would freeze emacs. Does anyone saw a similar behavior?

vemv06:03:30

by any chance you have cider and clojure-lsp running at the same time?

vemv06:03:34

Anyway, the functionality you are exercising is called cljr-slash, from clj-refactor.el. Try these two from your emacs' JVM repl:

(refactor-nrepl.ns.libspecs/namespace-aliases-response {:suggest true})
(refactor-nrepl.ns.libspecs/namespace-aliases-response {:suggest false})
Do they fail? Do they time out?

vemv06:03:12

The next thing you can try is https://github.com/clojure-emacs/clj-refactor.el/blob/d7384a4edddc6c875f8562e2acd4e7346a68553f/clj-refactor.el#L1951-L1958 Same questions - does it fail, does it timeout? What if you remove parseedn-read-str (note the surrounding thread-first which is identical to clojure's ->)

David Pham07:03:14

Thanks a lot! I will try this. But yes I use both at the same time, is there any problem?

vemv07:03:30

sometimes 😑 but let's try first with these

Charles Comstock23:03:25

I'm getting a similar error from namespace-aliases because I have a data_readers.cljc with "No ns form".

ERROR: Unhandled REPL handler exception processing message {:op namespace-aliases, :prefix-rewriting false, :insert-newline-after-require true, :debug false, :suggest true, :session 3dafca56-daeb-4b3b-aa19-a64d5d42faff, :id 257}
java.lang.IllegalStateException: No ns form at /src/data_readers.cljc
	at refactor_nrepl.core$read_ns_form_with_meta.invokeStatic(core.clj:383)
	at refactor_nrepl.core$read_ns_form_with_meta.invoke(core.clj:372)
	at clojure.lang.AFn.applyToHelper(AFn.java:156)
I can kinda fix this by adding (ns data-readers) to the file and then ns alias suggestion works, but then I get compile warnings for the data-readers file. I'm happy to adjust my project to include the appropriate ns for data-readers, but I'm not sure what that is supposed to be, but also, maybe namespace-aliases should have error recovery to skip any files it can't parse?

Charles Comstock02:03:37

Followup - on advanced compilation including the ns form in data_reader.cljc results in the following error:

clojure.lang.ExceptionInfo: Not a valid data-reader map {:url #object[java.net.URL 0x6caf0677 "file:/home/clgc/code/clojure/shimmers/src/data_readers.cljc"]}
        at clojure.core$load_data_reader_file.invokeStatic(core.clj:7815)
So my guess is that there exists a set of clj/cljc/cljs files that should not have a namespace header but still live inside of the main src tree of a project, so I think that supports the argument that the namesapce-alias suggestions should be resilient to parsing files without a ns header. Does analysis make sense or am I missing something about how this works?

vemv02:03:13

Sounds like something that can be easily fixed. refactor-nrepl upgrade to come very soon. Does your experienced issue freeze your Emacs, as David shared? Also, @UEQGQ6XH7, your feedback for the greatly valued. No rush as it's weekend it would be priceless to have it 🙏

Charles Comstock02:03:22

Yea, it similarly hangs whenever I use a unknown ns and hit slash. If I temporarily "fix" the data_readers.cljc so it has a ns form then the method completes as it has in the past.

👍 1
Charles Comstock02:03:48

(I didn't try any of the workarounds suggested above, though is there any there that I should try?)

vemv02:03:11

@UMGAMGWF8 I think it's a good idea (regardless of refactor-nrepl!) to move data_readers.cljc from src to resources. It's semantically more accurate and I'm pretty sure it will fix it (while the content themselves can remain unchanged)

Charles Comstock02:03:33

Cool thanks I will try that, wasn't aware that was a viable place to put it

vemv02:03:27

Cheers 🍻 > Yea, it similarly hangs whenever I use a unknown ns and hit slash. This one seems pretty concerning, wasn't aware of it. Could you report it in clj-refactor.el? I assume you're using a reasonably recent stack

Charles Comstock02:03:50

Sorry to clarify, it hung because of the error above, and on Ctrl-g would report the error about the unhandled repl handler exception. Fixing the ns header issue in data_readers by moving it to resources results fixes it so there is no hang. It does pause for a second on first invoke but it doesn't hang. Is that still new behavior that would be good to report an issue on or no?

Charles Comstock02:03:01

I think the "hanging" behavior is actually just anything that triggers an exception inside of that stack?

Charles Comstock02:03:38

Anyway I have to take off this evening, but happy to submit a bug report when I return if that seems necessary.

vemv05:03:00

> Is that still new behavior that would be good to report an issue on or no? No, cljr-slash can take a little (proportionally to your project's size) on first invocation, later on it's cached except for new/changed files.

vemv05:03:29

> I think the "hanging" behavior is actually just anything that triggers an exception inside of that stack? Could be. This is worth reporting about if it can be clearly articulated and/or reproduced. The problem wouldn't entirely surprise me since the middleware is called synchronously. Error detection depends on this defun, which maybe can be refined https://github.com/clojure-emacs/clj-refactor.el/blob/d7384a4edddc6c875f8562e2acd4e7346a68553f/clj-refactor.el#L2953-L2970

vemv05:03:36

btw, using a recent-enough version of refactor-nrepl is crucial for avoiding the issues both of you have described. the 3.x series has improved error handling (and performance), if you're still on 2.x all hope is lost :)

Charles Comstock06:03:15

I'm using refactor-nrepl 3.4.1 with cider-nrepl 0.28.3 so I believe that's all cutting edge. I'll see if I can summarize it more succintly than a project that includes a cljs/cljc/clj file in src that doesn't have a namespace header?

David Pham07:03:15

So I used eMacs for a smal clj project yesterday and the behavior disappeared. I retried quickly on one of my cljs project and it was smooth again without changing anything. I might retry next week, but I will change computer for a month so I hope the bug persist in that other environment.

👍 1
Charles Comstock07:03:08

I documented a minimal way to trigger the hanging problem from an exception: https://github.com/clojure-emacs/refactor-nrepl/issues/373#issuecomment-1066041519

👍 1
vemv07:03:02

Thank you both cider all info is always useful. Will fix what I can very soon :)

🎉 2
Charles Comstock17:03:41

Thanks again for all your hard work @U45T93RA6, really appreciate it.

vemv22:03:14

I could repro the freezing issue. refactor-nrepl 3.4.2 and clj-refactor 3.4.3 (these are different numbers) fix it for all of us three. clj-refactor 3.4.3 will be on MELPA within a couple hours cider

vemv04:03:28

I've worked on Clojure 1.11's :as-alias which is now supported. So while you're there, you can bump clj-refactor.el/refactor-nrepl to 3.5.1 and it will work with various features.

lassemaatta09:03:30

What sort of performance should I expect from cider-format-region or cider-format-buffer? I've been testing both and formatting a simple region of maybe 5 lines or a full buffer of <100 lines both seem to take 3-4 seconds. Is this as expected?

vemv09:03:28

I don't think it's expected Note, per https://docs.cider.mx/cider/usage/misc_features.html#formatting-code-with-cljfmt those use cljfmt (over nrepl, so fortunately no JVM spawning is involved) You could try profiling what the bottleneck is. If you don't particularly want to use the cljfmt backend, a pure clojure-mode formatting should be pretty quick

lassemaatta09:03:16

yeah, indent-region which I believe uses clojure-mode is lightning fast

lassemaatta09:03:24

I'm in the process of considering whether to ditch parinfer-rust-mode (for various reasons) and move over to parinfer/lispy/smartparens/etc and trying to understand what tools I need to a) keep my code nice and pretty and b) don't slow down too much when editing huge buffers/projects

vemv09:03:10

yeah many options out there :) I think cider-format is kind of a fringe feature, although supported. If you could give it a go to profiling that would be much welcome and can easily mean that a fix will be provided. One of the simplest things you can do is jumping to the JVM code in cider-nrepl, add a time somewhere, re-eval that defn and see how much the formatting itself is taking

lassemaatta10:03:26

I added some keybindings to manage the profiler and run cider-format commands and naturally the delay no longer occurs 🙂

clojure-spin 1
vemv10:03:32

Note that individual cider-nrepl middleware is lazily-loaded. So the first time you'll use it it will load that ns and cljfmt

lassemaatta10:03:16

This is funny. Running M-x cider-format-buffer takes a few seconds, regardless of how many times I call it. But when I define (global-set-key (kbd "<f8>") 'cider-format-buffer) then both <f8> and M-x cider-format-buffer are really fast.

lassemaatta10:03:07

similarly if I add that keybinding to my (use-package cider ... it's fast. Well, I guess this is one way to solve this issue 🙂

😂 1
vemv10:03:25

> Running M-x cider-format-buffer takes a few seconds, regardless of how many times I call it. unless you profile it, I assume?

lassemaatta10:03:09

I really don't understand how to interpret that. From htop I can see that emacs is eating up cpu while running cider-format-region, but the profiler says most of the time is spent on that completion stuff.

vemv10:03:41

It could be useful to determine which lib is scheduling that timer-event-handler . Disabling it would demonstrate if it has to do

lassemaatta10:03:58

oh my, the delay seems to be caused by suggest-key-bindings 😄

vemv05:03:05

Let us know if something could be improved somewhere :)