PEZ/rn-rf-shadow

cljs.user namespace is not created

Closed this issue · 14 comments

After connecting to the repl, if I run
(def a 3)
An error will occur:
image

Creating the ns with (ns cljs.user) solves the problem

@PEZ Dunno if it is an error here or Calva. But I raised it here.

Note that Calva will default drop into the cljs.user ns.

PEZ commented

Thanks for reporting! Sorry for late reply.

Have you loaded the file before doing this?

I didn't load any file.

PEZ commented

Then that could be the problem. Any Calva session should always start with loading the file.

Looking at the issue again, Calva drops me into the cljs.user namespace, which is the root cause.

I don't know where that namespace comes from, is it Calva or from Shadow (I am using deps.edn+shadow).

Calva seq:

{
    "calva.replConnectSequences": [
        { 
            "name": "app shadow-cljs",
            "projectType": "shadow-cljs",
            "cljsType": "shadow-cljs",
            "menuSelections": {
                "cljsLaunchBuilds": ["mobile"],
                "cljsDefaultBuild": "mobile",
            }
        },
    ]
}
; This is the Calva evaluation results output window.
; TIPS: The keyboard shortcut `ctrl+alt+c o` shows and focuses this window
;   when connected to a REPL session.
; Please see https://calva.io/output/ for more info.
; Happy coding! ♥️

; Connecting ...
; Hooking up nREPL sessions...
; Connected session: clj
; TIPS: 
;   - You can edit the contents here. Use it as a REPL if you like.
;   - `alt+enter` evaluates the current top level form.
;   - `ctrl+enter` evaluates the current form.
;   - `alt+up` and `alt+down` traverse up and down the REPL command history
;      when the cursor is after the last contents at the prompt
;   - Clojure lines in stack traces are peekable and clickable.
clj꞉shadow.user꞉> 
; Creating cljs repl session...
; Connecting cljs repl: app shadow-cljs...
;   The Calva Connection Log might have more connection progress information.
; Connected session: cljs, repl: mobile
; TIPS: You can choose which REPL to use (clj or cljs):
;    *Calva: Toggle REPL connection*
;    (There is a button in the status bar for this)
ns
nil
; ------ WARNING - :undeclared-var -----------------------------------------------
;  Resource: <eval>:1:1
 Use of undeclared Var cljs.user/ns
--------------------------------------------------------------------------------

cljs꞉cljs.user꞉> 
PEZ commented

cljs.user is the default ClojureScript namespace. The Calva REPL starts there, but the REPL doesn't really work until you load a file. And when you load a file the REPL will change to that file's namespace.

You can of course crate a file for the cljs.user namespace. A common pattern is to do this in a dev folder. Like they do in Penpot: https://github.com/tuyulman/penpot/blob/main/frontend/dev/cljs/user.cljs NB: You will still need to load this file though, so only create it if you find use for such a file.

This feels like an upstream bug of cljs. It doesn't make much sense to drop into an invalid namespace.

For example, in clj, even if I do not have the user namespace, that namespace is still valid and I care (def name xxx) in that ns.

PEZ commented

I doubt that is the case if you start a vanilla ClojureScript REPL at the prompt. It is not an invalid namespace as much as an invalid REPL environment. It is an artefact of how Calva is wired. And you'll have it in Clojure as well. (The errors might differ, but it is still the case that you'll need to load a file before the REPL starts behaving.)

Hi @PEZ , I feel we are not on the same page.

My question is, for the SPECIFIC cljs.user namespace, there is no need to load some file to make that ns valid. By valid, I mean being able to perform actions such as

(def a 3)

Consider the case of Clojure, if we run in clj on the command line, it will drop us into the user namespace. There is no need to load the user clj file.

Also consider the clojurescript vanilla example from https://clojurescript.org/guides/quick-start, if we run clj -M --main cljs.main. There is also no need to explicitly include the cljs.user file.

PEZ commented

I also feel we are not on the same page. 😄

I am aware that there is no need to load files in the vanilla cases. Hence:

I doubt that is the case if you start a vanilla ClojureScript REPL at the prompt.

But the way Calva's REPL is wired up, you will need to load a file before it starts behaving. This is mentioned in some places (maybe not enough places, but anyway), one of which is here: https://calva.io/try-first/

If you have the time to spend to try fix this, a PR on Calva (or maybe even on CIDER/Orchard, I actually don't know) is welcome.

Thanks for the quick reply. I would like to fix the issue but my current understanding of how the system works is limited.

For the moment, I would like follow your advice on:

cljs.user is the default ClojureScript namespace. The Calva REPL starts there, but the REPL doesn't really work until you load a file. And when you load a file the REPL will change to that file's namespace.

You can of course crate a file for the cljs.user namespace. A common pattern is to do this in a dev folder. Like they do in Penpot: https://github.com/tuyulman/penpot/blob/main/frontend/dev/cljs/user.cljs NB: You will still need to load this file though, so only create it if you find use for such a file.

PEZ commented

The issue with non-functional repl sessions should be (at least partially) fixed in latest Calva.