Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document alternative Clojure CLI usage #48

Open
vemv opened this issue Sep 2, 2023 · 22 comments
Open

Document alternative Clojure CLI usage #48

vemv opened this issue Sep 2, 2023 · 22 comments

Comments

@vemv
Copy link
Member

vemv commented Sep 2, 2023

Besides from the suggested makefile, one could use clojure.sh directly (at the cost of losing some caching).

This is worth documenting, maybe with a suggested accompanying bash/zsh helper to use it concisely.

@anonimitoraf
Copy link

Yes please! I use the clojure CLI and would be keen to try this feature out

@vemv
Copy link
Member Author

vemv commented Nov 9, 2023

Thanks for the interest!

This repo bundles clojure.sh.

If you use CIDER, it's also always bundled with each release and can be found in e.g. ~/.emacs.d./elpa/cider-*/clojure.sh (or elsewhere if you use Git for managing packages).

So you can download it, or reference it (the latter is recommended for always having the latest version)

Now, asuming clojure.sh is in your PATH, you can,

clojure.sh clojure foo bar baz # pass any argument you'd normally pass to `clojure`

Where clojure is the name of your usual clojure command (it's clojure for 99% of users, but maybe you have multiple versions around or some other custom setup)

The usage is simple: it has the same arguments as clojure.

What the script does is:

  • dynamically creating a new clojure invocation that will use a -Scp calculated by enrich-classpath; and then
  • evaluate that invocation.

You can create a utility function that wraps the pattern above, adding it to your bash/zsh init file:

enrich(){
  ~/.emacs.d/elpa/cider*/clojure.sh clojure "$@"
}

# Usage:
# enrich foo bar baz # pass any argument you'd normally pass to `clojure`

That's about it - please let me know how it works for you, and I'll add it all to the README.

Cheers - V

@anonimitoraf
Copy link

anonimitoraf commented Nov 10, 2023

Thanks @vemv - I tried it out and successfully started my program as usual. However when I use cider-connect to connect via SSH, I don't see javadocs (like what you posted here)

  • I've got cider-enrich-classpath set to t
  • CIDER 1.12.0
  • nrepl/1.1.0
  • cider-nrepl/0.43.1
  • I can also see the file ~/.cache/enrich-classpath-cache filled up with my classpaths

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

Thanks!

For discarding possibilities, it would be useful to try a minimal local project first?

@anonimitoraf
Copy link

Sure, I've created https://github.com/anonimitoraf/barebones-clojure and did the same steps as in your recording (I think)

Screen.Recording.2023-11-10.at.9.38.11.pm.mov

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

Nice, thank you!

Could you share your JVM process classpath?

As found with ps aux | grep java

@anonimitoraf
Copy link

No worries

ubuntu     77325  8.0 26.1 3145468 494556 pts/0  Sl+  10:30   1:00 /usr/bin/java -XX:-OmitStackTraceInFastThrow -XX:-OmitStackTraceInFastThrow -Dclojure.main.report=stderr --add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED -Dclojure.basis=.cpcache/676205888.basis -classpath src:resources:/home/ubuntu/.m2/repository/org/clojure/clojure/1.11.1/clojure-1.11.1.jar:/home/ubuntu/.m2/repository/org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.jar:/home/ubuntu/.m2/repository/org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.jar:/home/ubuntu/.m2/repository/cider/cider-nrepl/0.43.1/cider-nrepl-0.43.1.jar:/home/ubuntu/.m2/repository/nrepl/nrepl/1.1.0/nrepl-1.1.0.jar:/home/ubuntu/.m2/repository/cider/orchard/0.19.0/orchard-0.19.0.jar:/home/ubuntu/.m2/repository/mx/cider/logjam/0.1.1/logjam-0.1.1.jar:/home/ubuntu/.cache/mx.cider/enrich-classpath/17081/815684688/1900108823.jar clojure.main -m com.anonimito.barebones-clojure

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

Got it, you need to install the JDK sources in Ubuntu

https://docs.cider.mx/cider/troubleshooting.html#navigation-to-jdk-sources-doesnt-work

(as you can see those instructions are for JDK17; if you're using something newer, I'd appreciate a docs update)

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

...At this point, given that Enrich wraps both Lein and tools.deps, we could proactively ensure that those sources are there e.g. fail-fast when not found.

(Users can always disable Enrich if that requirement cannot be immediately satisfied)

@anonimitoraf
Copy link

Great, installing openjdk-17-source openjdk-17-doc worked! Thanks so much

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

Cheers, enjoy!

Any kind of feedback always welcome. There's a small refinement coming clojure-emacs/cider#3576

@anonimitoraf
Copy link

Oh yea, regarding docs, how can I do the same for third-party libs? For example: https://stripe.dev/stripe-java ?

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

Yes, if you depend on a given Maven library, they always include (per the Maven rules) a "sources" jar.

enrich-classpath fetches those.

@anonimitoraf
Copy link

I see. I can see that Stripe, for example indeed do have docs and sources:

~/.m2/repository/com/stripe/stripe-java/22.12.0$ ls -al
total 28236
drwxr-xr-x 2 ubuntu ubuntu     4096 Nov 10 07:13 .
drwxr-xr-x 3 ubuntu ubuntu     4096 Nov  7 12:39 ..
-rw-rw-r-- 1 ubuntu ubuntu      288 Nov 10 07:13 _remote.repositories
-rw-rw-r-- 1 ubuntu ubuntu 19695356 Nov 10 07:13 stripe-java-22.12.0-javadoc.jar
-rw-rw-r-- 1 ubuntu ubuntu       40 Nov 10 07:13 stripe-java-22.12.0-javadoc.jar.sha1
-rw-rw-r-- 1 ubuntu ubuntu  1775886 Nov 10 07:13 stripe-java-22.12.0-sources.jar
-rw-rw-r-- 1 ubuntu ubuntu       40 Nov 10 07:13 stripe-java-22.12.0-sources.jar.sha1
-rw-r--r-- 1 ubuntu ubuntu  7402879 Nov  7 12:42 stripe-java-22.12.0.jar
-rw-r--r-- 1 ubuntu ubuntu       40 Nov  7 12:42 stripe-java-22.12.0.jar.sha1
-rw-r--r-- 1 ubuntu ubuntu     1767 Nov  7 12:39 stripe-java-22.12.0.pom
-rw-r--r-- 1 ubuntu ubuntu       40 Nov  7 12:39 stripe-java-22.12.0.pom.sha1

but I'm not seeing docs in CIDER inspector 🤔

@vemv
Copy link
Member Author

vemv commented Nov 10, 2023

Will try soon!

Sometimes it happens that there's some issue in the .java files that makes them unparseable (example)

@anonimitoraf
Copy link

anonimitoraf commented Nov 10, 2023

I see, no worries/rush and thanks.

Unrelated, I've just noticed that when using the clojure.sh script, my CPU usage is constantly at 100% as opposed to ~1% when using just vanilla Clojure CLI. Mem usage is much higher too.

For context, I'm using systemd:

ExecStart=/bin/bash -c './clojure.sh clojure -M:prod'

Screenshot 2023-11-11 at 8 45 13 am

vs

ExecStart=/bin/bash -c 'clojure -M:prod'

Screenshot 2023-11-11 at 8 42 57 am

(If it helps, here's the rest of my systemd service config)

[Unit]
Description=API service
After=network.target
StartLimitIntervalSec=0
# Always restart service
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=ubuntu
ExecStart=/bin/bash -c './clojure.sh clojure -M:prod'
# ExecStart=/bin/bash -c 'clojure -M:prod'

[Install]
WantedBy=multi-user.target

@anonimitoraf
Copy link

anonimitoraf commented Nov 10, 2023

Actually, my bad, I should've checked if this is the case without systemd. Checking this now


Yep same issue with just invoking ./clojure.sh clojure -M:prod


I don't get this issue with my minimal project https://github.com/anonimitoraf/barebones-clojure so it might be something specific with my proper project

@vemv
Copy link
Member Author

vemv commented Nov 11, 2023

Interesting, hadn't experienced it (I would have noticed since I use the infamous Intel MBP 🛫)

There are a few ways in which you can determine what your CPU is doing

  • The one I know best is opening YourKit (there's a free trial), going to the Threads tab, searching for threads with a 🔥 icon, and clicking on one for checking its stacktrace.

Example:

image
  • The other one is, within the terminal repl started by clojure.sh hitting Ctrl + \ so that the JVM will dump stacktraces from every single thread

Example:

image

You could share the entire output with me over here or privately.

  • Lastly, there are other profilers that may easily pinpoint the busiest threads.

@vemv
Copy link
Member Author

vemv commented Nov 11, 2023

The simplest explanation is that it's performing this one-off job. On my machine (which as mentioned can be very noisy) it never hogs the CPU and it completes reasonably fast.

It also completes quickly on cider-nrepl's CI matrix.

Are you sure that the 100% lasts more than a few minutes?

@vemv
Copy link
Member Author

vemv commented Nov 11, 2023

Regarding Stripe, the underlying parsing errors come from the fact that is uses Lombok at build time, but it's not a Maven dependency. So without it in the classpath, the Java parser will complain.

They even had some historical issues related to Lombok. It's a plugin which has a mixed fame. https://github.com/stripe/stripe-java/issues?q=is%3Aissue+lombok

Note that, however, jump-to-definition works.

@anonimitoraf
Copy link

Cool thanks for the info. I'll send through peofiling results tonight. From my observation, CPU usage hangs around at 100% for a few minutes after the program has finished startup, I'll check later if this goes on for longer

Re: Stripe - I see, no worries!

@vemv
Copy link
Member Author

vemv commented Nov 12, 2023

Cheers. clojure-emacs/orchard#211 would solve all issues (Stripe parsing and CPU hogging - assuming we've guessed the cause correctly).

It might become a reality before the year wraps up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants