Fork me on GitHub
#clojurescript
<
2024-03-29
>
pez14:03:47

Am I supposed to be able to do (require '[something.something :as something]) and then use the alias? I’m doing it here https://github.com/PEZ/guardrails-malli-mini/blob/e5a3a45f92d38eba5bb93a0f410023aaf3b28251/src/main/guarded.cljc#L35-L39 and only the fully qualified access works…

p-himik15:03:23

Thomas will probably correct or expand on my answer once he sees this. :) But IIRC a dynamic require is really only a REPL thing. It can't work in a file because the compiler needs to know what m is, but it doesn't before actually running the code that require generates.

pez15:03:38

This is in a Rich comment form though, and thus in the REPL. I first evaluate the require, then fail to use the alias in the following evaluations.

p-himik15:03:19

Erm. That does surprise me. What also surprises me is that *ns* is nil, at least in a shadow-cljs REPL.

p-himik15:03:43

Disregard the whole previous comment - my env is busted.

p-himik15:03:44

OK, fixed. *ns* is still nil though. Given this code:

(comment
  (require '[clojure.string :as str])
  (str "a" "b")
  )
I attached to a shadow-cljs REPL, sent the first form there and then sent the second form there:
(require '[clojure.string :as str])
=> nil
(str "a" "b")
=> "ab"
So that seems to have worked just fine.

p-himik15:03:12

Yo, sleep deprivation is no joke. facepalm The code above is the dumbest one I've written in a long time. At least it took me only a minute to see that.

😂 1
p-himik15:03:55

Yeah, can reproduce what you see, so neither or us is doing anything wrong (or we both are):

(str/join ["a" "b"])
------ WARNING - :undeclared-ns ------------------------------------------------
 Resource: <eval>:1:2
 No such namespace: str, could not locate str.cljs, str.cljc, or JavaScript source providing "str"
--------------------------------------------------------------------------------

------ WARNING - :undeclared-var -----------------------------------------------
 Resource: <eval>:1:2
 Use of undeclared Var str/join
--------------------------------------------------------------------------------

pez15:03:12

I’m in good company (we would say in Swedish, dunno if it makes sense in English 😃 ).

p-himik15:03:29

There was a similar thread a year ago without any resolution: https://clojurians.slack.com/archives/C03S1L9DN/p1678325483467779

p-himik15:03:02

On my end, I'm attaching to shadow-cljs via nREPL and then run (shadow/repl build-id) to get to the CLJS REPL.

p-himik15:03:23

Using the REPL via npx shadow-cljs cljs-repl build-id works just fine:

shadow-cljs - connected to server
cljs.user=> 1
1
cljs.user=> (require '[clojure.string :as str])
nil
cljs.user=> (str/join ["a" "b"])
"ab"

p-himik15:03:41

@U05224H0W Any clue why aliases work when the REPL is started via cljs-repl but don't work if an nREPL connection is used?

pez15:03:26

Ah, I’m also using nREPL.

thheller15:03:22

works fine for me?

p-himik15:03:59

It worked for me as well with the quickstart-browser project. I'm testing whether switching to deps.edn changes it, because that's what I was using for a test in my own project.

p-himik15:03:57

Nah, that still works. Not sure what the difference is.

thheller16:03:04

I suspect I just broke something. REPL require has been broken so many times I can't even count it anymore 😛

p-himik16:03:14

Worthy of a test? :) No clue how feasible though, of course.

thheller16:03:55

the reason this is so hard is basically the combination of REPL and hot-reload

thheller16:03:12

ie. you do something at the REPL and a triggered hot-reload deletes everything you did

thheller16:03:27

(as in created new aliases)

thheller16:03:51

or you do something at the REPL that then causes a circular relationship between namespaces and breaks hot-reload 😛

thheller16:03:03

this should of course still work, not sure why it doesn't

p-himik16:03:21

Mm, right. But what made you say "hmm or not"?

thheller16:03:28

it worked on the first try, but out of habit I did a load-file and switched to the ns first

thheller16:03:07

the issue appears to be that the alias is created in the wrong ns

p-himik16:03:42

Huh. I just updated to 2.28.2 (was using 2.27.5 before) and it now works for me just fine. Tried a few namespaces in the same REPL session, tried restarting the REPL session - all works. @U0ETXRFEW What version do you have?

p-himik16:03:35

Ah, it works only when run directly via the nREPL window. Doesn't work when I use a comment form.

pez16:03:04

I’m on 2.28.2 with the project in question.

pez16:03:17

> Ah, it works only when run directly via the nREPL window. Doesn’t work when I use a comment form. That’s extra weird. Maybe the nREPL window in Cursive is using user or something as ns and that makes it dodge the problem.

thheller16:03:38

ok so the problem is relatively simple

thheller16:03:18

require uses the ns of the current REPL "session", which if you just eval something and don't set it manually is cljs.user

thheller16:03:25

so the alias is created in cljs.user

thheller16:03:45

BUT regular eval uses the :ns supplied by nrepl OVER the current repl session

thheller16:03:24

so in case of cursive the eval that arrives is

{:eof? false, :ns demo.browser, :form sg/query-ident, :source "sg/query-ident"}

thheller16:03:42

(note that I'm testing in shadow-cljs project itself, doesn't really matter which ns is used)

thheller16:03:58

but as you can see cursive supplied the :ns demo.browser here, although I never actually switched to it

thheller16:03:57

I suspect your nrepl clients do the same

thheller16:03:34

so the alias exists in cljs.user due to the require executing there, but not the ns you actually eval in

thheller16:03:41

seems like that should be more consistent 😛

thheller16:03:14

if you in-ns first it seems to be ok

pez16:03:07

Interesting, Calva used to do in-ns as part of any evaluation. But I changed that because sending the ns parameter along with the nREPL eval op should be quite equivalent. Not equivalent enough then.

thheller16:03:52

no this is a bug

thheller16:03:58

fix is incoming

catjam 1
thheller16:03:53

fixed in 2.28.3

🎉 4
pez17:03:46

I can confirm that it is fixed in my project. Thanks, Thomas, you are the best! 🙏

NucularLemon20:03:53

Hello, i am pretty new, and this question is maybe stupid, but if i want to consume an api with clojurescript do i use a special library or just interop with javascript using fetch, what is the recommended way?

Nundrum20:03:56

It sort of depends. If you are just using clojurescript, then interop is fine. If you are using re-frame, then you'll want to hook into it's capabilities. And that would be similar for any other framework/library you might use.

hifumi12301:03:53

I personally use https://github.com/oliyh/martian since the backend at my job exports OpenAPI 3.0 schemas

NucularLemon17:03:22

thanks all for the answers, i will take a look into martian also