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

[info] Add support for automatically downloading sources JARs #911

Merged
merged 3 commits into from
Jan 10, 2025

Conversation

alexander-yakushev
Copy link
Member

@alexander-yakushev alexander-yakushev commented Jan 5, 2025

Sister PRs: clojure-emacs/orchard#310, clojure-emacs/cider#3769.

Not a lot of changes were needed here. Given how things are implemented on the Orchard side, cider-nrepl only needs to set up a correct *download-sources-jar-fn* before calling out to Orchard functions that do Java sources discovery.

We maintain a ConcurrentHashMap (logically, a set, but there is no ConcurrentHashSet, and CHM API for this usecase is cleaner than (atom #{})) of Maven coordinates (groupId/artifactId "version") of the dependencies we already tried downloading sources once. If it succeeded, we won't call this function again anyway, if it failed, the CHM will prevent trying for the second time. This debouncer only exists in memory which I suppose makes it easier to retry (by restarting) if the sources failed to download because bad Internet or something, not because the sources JAR does not exist in the repository. If this cache was persistent, retrying will be a bit more involved. Anyway, this may change after user feedback; maybe, there would be frequent annoying cases where certain Java classes wouldn't have sources JAR, and it will be annoying that the first C-c C-d C-d on such class is slow after each REPL restart.

We bind *download-sources-jar-fn* only in info op, not the eldoc op. Eldoc is less intentional and much more common, so I don't want to make slow and download things. Counter to that, info is invoked by M-. or C-c C-d C-d where the user explicitly requested something, so it makes sense to attempt downloading then. Until the JAR is downloaded, eldoc will simply return results obtained via reflection, after that it will pick up the parsed Javadocs.


  • You've added tests to cover your change(s)
  • You've updated the README

@alexander-yakushev alexander-yakushev changed the title Sources jar [info] Add support for automatically downloading sources JARs Jan 5, 2025
@@ -57,11 +57,9 @@
then had to be disabled for test suite reasons in Orchard 0.15.0 to 0.17.0 / cider-nrepl 0.38.0 to 0.41.0, and now it's restored again)
Note that this can only be done cider-nrepl side, unlike before when it was done in Orchard itself."
[]
;; 1.- Warmup the overall cache for core Java and Clojure stuff
@orchard.java/cache-initializer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This initializer was removed in latest Orchard.

*clojure-version* min-clojure-verion)]
(try
(.println System/err msg)
(finally (throw (ex-info msg {}))))))
Copy link
Member Author

@alexander-yakushev alexander-yakushev Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that a library can System/exit, I think it takes too much upon itself. Throwing an exception is better because it can be caught (for whichever reason the user has), it can be properly logged, you get a stacktrace to know who required cider.nrepl namespace (e.g. this might be a surprise to the user), etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me - that System/exit was always controversial. Ideally we'd have at some point a way for the server to notify clients if they are not meeting some runtime requirements, but if cider-nrepl can work to some extent and the users are aware of this, it's probably a good trade-off.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, if user doesn't try to catch the exception around requiring cider.nrepl, then the end result will be the same as in System/exit – the server will fail to start. It's just that throwing an exception gives more options to react to it and properly report.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, yeah - I get this. My point was mainly that the way the code was previously written it would fail on the version check alone, even if it might actually work in practice and might be fine with some users. (working, but not really supported) I think the purpose of the explicit exit here was to prevent such scenarios from happening.

I agree it's better for the clients to decide how to handle an error from a library that they're using.

Copy link
Member

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the failing tests the PR looks good to me.

@bbatsov
Copy link
Member

bbatsov commented Jan 10, 2025

We bind download-sources-jar-fn only in info op, not the eldoc op. Eldoc is less intentional and much more common, so I don't want to make slow and download things. Counter to that, info is invoked by M-. or C-c C-d C-d where the user explicitly requested something, so it makes sense to attempt downloading then. Until the JAR is downloaded, eldoc will simply return results obtained via reflection, after that it will pick up the parsed Javadocs.

You might want to mention this somewhere in the comments and perhaps later in CIDER's docs, so people are aware this is intentional.

@alexander-yakushev alexander-yakushev force-pushed the sources-jar branch 2 times, most recently from 67fff19 to 3c7cb91 Compare January 10, 2025 13:21
@alexander-yakushev
Copy link
Member Author

You might want to mention this somewhere in the comments and perhaps later in CIDER's docs, so people are aware this is intentional.

I'll mention it in the CIDER docs; there's probably not a place to mention it here because it is not controlled on cider-nrepl side. If the client decides to set the :download-sources-jar key for eldoc, they can do that, cider-nrepl will not object.

@alexander-yakushev alexander-yakushev force-pushed the sources-jar branch 2 times, most recently from b83aa07 to 3e17faa Compare January 10, 2025 13:39
@alexander-yakushev alexander-yakushev merged commit 46136be into master Jan 10, 2025
16 checks passed
@alexander-yakushev alexander-yakushev deleted the sources-jar branch January 10, 2025 15:54
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

Successfully merging this pull request may close these issues.

2 participants