Hi guys! I am writing a script to search Clojars for a dep and insert the result at the cursor position. Technically it works, but the following function errors with "Maximum call stack size exceeded" and I just cannot figure out why - could you give me a hint? I just don't get where there could possibly occur a recursion.
(defn insert-at-cursor-position
[text]
(let [editor (.-activeTextEditor vscode/window)]
(when editor
(let [position (-> editor
(.-selection)
(.-active))]
(when position
(.edit editor (fn [builder]
(.insert builder position text))))))))
It errors both in REPL and when executed as a user script via keybindings
sometimes you can get a stackoverflow in the REPL due to printing of circular objects
e.g. js/globalThis
Maybe try returning nil from the function to exclude that possibility?
Well, it really was that easy š Thank you so much! But honestly I don't quite understand it. What happens there?
.edit returns a promise right? I don't remember exactly but perhaps joyride prints the result of the promise and it contains a large object which causes the overflow
It returns a JS Thenable<boolean>, which seems to be a promise, yes
hmm, just a boolean. nothing harmful I'd say
I don't have vscode + joyride running at the moment, perhaps @pez could help debug this further
I thought there might be something obvious I was missing. I will try to find out what happens there. Anyway: thanks a lot!
All I can say is that this happens to me a lot.
Lovely script idea. Iād have use for that. Please feel encouraged to add as an example to the Joyride repo.
Sure, I will add it
All I can say is that this happens to me a lot.My hunch is this is related to printing something. When invoking a script outside the REPL (via a keybindig), does it also happen and does joyride print anything in that case?
No not really. All it prints is
clojars.cljs evaluated.
=>
I mean when you didn't return nil
That is for the case without returning nil. With returning nil it is
clojars.cljs evaluated.
=> niland without returning nil, did you get the stackoverflow? this would explain not seeing a value after =>
Yes without nil it overflows
if you make a small function that just returns a promise, e.g.:
(defn foo [] (js/Promise.resolve true))
and call this in the REPL, do you get the same problem?No that's no probem
what if you print the value that .edit returns yourself? what happens in that case? you could try directly printing it or in a .then callback
(-> (.edit editor (fn [builder]
(.insert builder position text)))
(p/then #(println %))))))))
Prints
#<Promise[pending:27]>that doesn't make sense to me since the println should print the "contents" of the promise?
could you try just .then instead of p/then?
gonna call it a night
Thanks for helping debugging šŖ
The output with .then is
; true
#<js/Promise[~]>I use inline def to capture the unwrapped promise.
true, but that wasn't the point here. in this case, printing forces the result to be nil (thus avoiding the SO) while still seeing the value. anyway, it would be nice if that SO could be fixed. But going to get some sleep now
I will also finish for today. I will create a PR for the Joyride example for the script tomorrow. Thank you guys
I would love if we can solve the SO. One of those things that I have grown used to, but that obviously is not good Ux/Dx.
I added issue and PR for the script
@pez I suggest logging the exception + stacktrace in the console or so and just displaying the message, so we can find out more details about the SO?