ClojureScript - How to load user.cljs file for repl sessions - clojurescript

I'm starting a cljs repl with a node.js environment.
=> clj -m cljs.main -re node
Documentation for using the repl says that you can place a user.cljs file at the classpath root for the project and it will be automatically loaded into a repl session in the cljs.user namespace.
I have a src directory with user.cljs at its base. The project's directory structure looks like the following.
xyz / deps.edn
/ src / xyz / core.cljs
/ user.cljs
ClojureScript version 1.10.520 is the only dependency in deps.edn.
Whenever I start the repl I get the following error.
Execution error (IllegalArgumentException) at cljs.closure/compile-file (closure.clj:639).
/var/folders/rk/r65ng3n93l53h95nsvkl265c0000gn/T/out385499094487053918164582825852/cljs/user/user5D28A44.js is not a relative path
When I look in the temporary output folder it is clear that ../cljs/user/user5D28A44.js is not being generated.
Is this a cljs bug? Can someone show me an example of this working correctly?

You have to start the ClojureScript repl explicitly from clojure programmatically, the command line startup short hand doesn't work for loading user.cljs.
=> clj
Clojure 1.10.1
user=> (require '[cljs.repl :as repl])
user=> (require '[cljs.repl.node :as node])
user=> (def env (node/repl-env))
user=> (repl/repl env)
cljs.user=> working?
"Yes."
cljs.user=> ...
With the working? symbol defined in user.cljs it is available as you would expect.

It’s a known issue. See: CLJS-2917
You can work around it by specifying an output directory via -d.

Related

cljs quick start failed to locate namespace

I tries to quick start cljs, but the similar problem occurred.
clojure 1.10.3 on linux openjdk 17.0.1
project folder:
.
|_ deps.edn
|_ src
|_ helloworld
|_ core.cljs
core.cljs:
(ns helloworld.core)
(println "helloworld")
deps.edn
{:deps {org.clojure/clojurescript {:mvn/version "1.10.758"}}}
tried to quick start under the project folder but failed,
$ clj -M -m cljs.main -c helloworld.core
Unexpected error (IllegalArgumentException) compiling at (REPL:1).
Namespace helloworld.core does not exist.
#EDIT However, tried on lein-cljsbuild successfully.
$ lein cljsbuild once
project.clj (instead of deps.edn):
(defproject helloworld "0.1.0"
:dependencies [[org.clojure/clojure "1.10.3"]
[org.clojure/clojurescript "1.10.758"]]
:plugins [[lein-cljsbuild "1.1.8"]]
:cljsbuild {:builds [{:source-paths ["src"]}]})
#EDIT I had to reboot the machine then tried again by chance - it works now. but "why" remains mysterious.
In the tutorial, it wants src/hello_world/core.cljs as the file and hello-world.core for the namespace but you have helloworld.core for the ns and say you have src/helloworld/core.cljs for the file (which ought to work). That's the only difference I can see from the tutorial.
In addition, the tutorial has --repl on the clj command to cause a (ClojureScript) REPL to be started after compiling helloworld.core (using your ns).
That all works as expected for me:
(! 600)-> tree .
.
|____deps.edn
|____src
| |____helloworld
| | |____core.cljs
(! 601)-> cat deps.edn
{:deps {org.clojure/clojurescript {:mvn/version "1.10.758"}}}
(! 602)-> cat src/helloworld/core.cljs
(ns helloworld.core)
(println "helloworld")
(! 603)-> clj -M -m cljs.main -c helloworld.core -r
ClojureScript 1.10.758
cljs.user=> helloworld
(and a browser opens with the ClojureScript welcome page)
That was with Clojure CLI 1.10.3.1029 (the latest) but I also tested it with 1.10.1.561 to make sure recent changes haven't affected the behavior and it also worked as expected.
I would recommend checking that you have the correct Clojure CLI installed:
(! 605)-> clj -version
Clojure CLI version 1.10.3.1029
(! 606)-> clj -Sdescribe
{:version "1.10.3.1029"
:config-files ["/usr/local/Cellar/clojure#1.10.3.1029/1.10.3.1029/deps.edn" "/Users/sean/.clojure/deps.edn" "deps.edn" ]
:config-user "/Users/sean/.clojure/deps.edn"
:config-project "deps.edn"
:install-dir "/usr/local/Cellar/clojure#1.10.3.1029/1.10.3.1029"
:config-dir "/Users/sean/.clojure"
:cache-dir ".cpcache"
:force false
:repro false
:main-aliases ""
:repl-aliases ""}
(the installation instructions are here: https://clojure.org/guides/getting_started if have some rogue clj script instead of the official one)
I suspect you have a non-standard clj script installed that tried to just run helloworld.core as if it were a .clj file.

How to dispatch events in re-frame framework (clojurescript) in the REPL?

I am using figwheel,I want to dispatch events manually from REPL.
e.g after my app is connected to REPL
(in-ns 'my-re-frame.core)
(re-frame/dispatch-sync [::events/initialize-db])
I get following error
"RuntimeException Invalid token: ::events/initialize-db"
How I dispatch those events? and How to see status of app in REPL?
I think it's is because I'm using cider with fighweel in emacs and it tries to find conf with clj extention than clj

Problems with Clojurescript quickstart -- build fails

I'm trying to follow the Clojurescript Quick-Start
I have downloaded the Clojurescript jar as described in the Quick-Start guide. I have verified that it has the appropriate size (about 19M). I have created the files. But when I try to build using the command:
java -cp cljs.jar:src clojure.main build.clj
Java returns the following stacktrace:
Exception in thread "main" java.io.FileNotFoundException: Could not locate cls/build/api__init.class or cls/build/api.clj on classpath., compiling:(/Users/jnedzel/Documents/prj/closurescript/quickstart/hello_world/build.clj:1:1)
at clojure.lang.Compiler.load(Compiler.java:7249)
at clojure.lang.Compiler.loadFile(Compiler.java:7175)
at clojure.main$load_script.invoke(main.clj:275)
at clojure.main$script_opt.invoke(main.clj:337)
at clojure.main$main.doInvoke(main.clj:421)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.Var.invoke(Var.java:379)
at clojure.lang.AFn.applyToHelper(AFn.java:154)
at clojure.lang.Var.applyTo(Var.java:700)
at clojure.main.main(main.java:37)
Caused by: java.io.FileNotFoundException: Could not locate cls/build/api__init.class or cls/build/api.clj on classpath.
Any ideas?
TL;DR: There's a typo in build.clj
Full explanation
As of the Quick-Start, the file build.clj should have the following content:
(require 'cljs.build.api)
(cljs.build.api/build "src" {:output-to "out/main.js"})
The error message says Could not locate cls/build/api__init.class. When the clojure compiler compiles a ns, a loader classfile is generated with the name my/domain/lib__init.class. That's the class that will be loaded given (require 'my.domain.lib).
In the case of cljs.build.api, that would be cljs/build/api__init.class. But it is trying to load cls/build/api__init.class instead, so you have a typo there.

Clojurescript: Error: Could not find or load main class clojure.main

I installed Clojurescript following its quick start guide.
I pulled the git repo to ~/clojurescript.
CLOJURESCRIPT_HOME is set ~/clojurescript and clojurescript/bin is added to system path.
When I try to use commands cljsc I get following error
Could not find or load main class clojure.main
How can I solve it?
Alright coming back after 5 years.
This error surfaces when one runs clj --main cljs.main --compile hello-world.core --repl from directory or path which does not have file deps.edn or cljs.jar.
Considering the directory structure at https://clojurescript.org/guides/quick-start,
hello-world #### Run that command in this directory
├─ src
│ └─ hello_world
│ └─ core.cljs
├─ cljs.jar
└─ deps.edn
i.e.
$ cd hello-world
$ clj --main cljs.main --compile hello-world.core --repl
I was going through this tutorial today.
I checked here for this error. The version of the standalone jar was correct. The problem was caused by a 'typing mistake' in the core.cljs file. The string "Hello world!" was not in double quotes. Initially the string was in single quotes and included a comma.
The other issue I struggled with was getting the java command to work. I was using a Cygwin64 Terminal.
The correct command for the Cygwin64 Terminal is:
$ java -cp "cljs.jar;src" clojure.main build.clj
In my case, I was typing Windows command by mistake which has " in the command + make sure you'll have the cljs.jar in your root folder bes
java -cp cljs.jar:src clojure.main build.clj
I hope it helps someone, one day.:)
If you have created your project using leiningen then your project cli would look something like this
(defproject clojuredemo "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]]
)
After the :dependencies add the following lines
:jar-name "<project-name>.jar"
:jar-path "/target/<project-name>.jar"
Note : you need the replace the project name with your project
After that run the following command
lein jar
Ensure that the JAR file at classpath exists:
java -cp target/missing-file.jar clojure.main your.namespace
will confusingly throw Could not find or load main class clojure.main.

Trouble installing Clojure dev environment on ubuntu

I followed this guide:
http://wiki.unto.net/setting-up-clojure-and-slime
Which walked me through the steps of building Clojure and Clojure-contrib from source along with SLIME and swank emacs scripts. I am a total noob when it comes to emacs.
I am getting this error:
File error: Cannot open load file, swank-clojure-autoload
This is my .emacs:
;; Clojure
(add-to-list 'load-path "~/git/clojure-mode")
(add-to-list 'load-path "~/git/swank-clojure/src/emacs")
(add-to-list 'load-path "~/git/slime")
(setq swank-clojure-jar-path "~/git/clojure/clojure-1.2.0-master-SNAPSHOT.jar")
;; clojure-contrib, when compiled as above, is not working at the moment
;; (setq swank-clojure-extra-classpaths
;; (list "~/git/clojure-contrib/clojure-contrib.jar"))
(require 'clojure-mode)
(require 'swank-clojure-autoload)
(require 'slime)
(eval-after-load "slime" (slime-setup '(slime-repl)))
(slime-setup)
I also encountered this same error while following the steps to this guide on a separate install.
Thanks!
the error is caused because the swank-clojure git repo does not have swank-clojure-autoload or even the directory src/emacs in it.
I would recommend installing swank-clojure and slime using elpa instead.
See here for instructions on getting elpa up and running: http://tromey.com/elpa/install.html
once you've done that then M-x package-install swank-clojure should get you up and running.