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

Fix #292: babashka compatibility #293

Merged
merged 1 commit into from Feb 2, 2023
Merged

Conversation

borkdude
Copy link
Contributor

@borkdude borkdude commented Jan 30, 2023

Hi @weavejester - this fixes and tests babashka compatibility.

I took the liberty to add a generated deps.edn based off the project.clj. This makes testing this library as a babashka dependency easier but also benefits deps.edn users as this library can be used as a git dep. To re-generate the deps.edn, all you have to do is run bb gen-deps.

Also added: a Github CI config file for testing this library with babashka. I did not know how to work with Travis.
cc @lread

@borkdude
Copy link
Contributor Author

Note that the test runner requires a recent version of babashka (as babashka only recently became compatible with eftest).

@pesterhazy
Copy link

pesterhazy commented Jan 30, 2023

Very cool. Quick benchmark:

numbers_of_files runtime_in_seconds

bb -m cjfmt.main check

1 0.174
2 0.207
4 0.286
8 0.460
16 0.761
32 1.392
64 2.636
128 5.113

clojure -m cjfmt.main check

1 1.629
2 1.680
4 1.779
8 1.890
16 2.066
32 2.421
64 3.109
128 4.473

Due to startup overhead, clojure takes 9.3x time compared to bb for 1 file. At 128 files, clojure overtakes bb and now takes 0.8x of bb's time.

Conclusion: bb is much faster if you're only checking a small number of files (e.g. on save in an editor or in a pre-commit git hook)

Copy link
Owner

@weavejester weavejester left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. I'd rather not manage two different CI solutions, so either we convert everything over to GitHub Actions (or Circle CI) in another PR, or we instead use Travis. Equally, I think we'd want to split out deps.edn into another PR if its absolutely necessary (again, I'd rather maintain one dependency file than two).

Comment on lines 46 to 48
(defmacro if-bb [then else]
(if (System/getProperty "babashka.version")
then else))
Copy link
Owner

Choose a reason for hiding this comment

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

Can you explain why this is a macro, instead of something like:

(def bb? (boolean (System/getProperty "babashka.version")))

Copy link
Contributor Author

@borkdude borkdude Jan 30, 2023

Choose a reason for hiding this comment

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

We can do that if you prefer, but imo it's cleaner to not have any side effects of macro-expansions in conditionals happening at all, which can only be prevented by using another macro. But in this case it's probably ok.

Copy link
Owner

Choose a reason for hiding this comment

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

Does Babashka have a reader conditional in that case? i.e. would #?(:bb ...) work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@weavejester I solved this using a more elegant solution: I added a .bb file (which is always loaded first in bb) with the same namespace and vars so the main namespace doesn't need any changes. A reader conditional would have worked, but then we'd needed to rename the .clj namespace to .cljc.

cljfmt/src/cljfmt/main.clj Outdated Show resolved Hide resolved
@weavejester
Copy link
Owner

My current inclination would be to migrate from Travis to GitHub Actions, I think.

@borkdude
Copy link
Contributor Author

borkdude commented Jan 30, 2023

I can remove the deps.edn and Github actions in this PR. In that case there won't be any verification (beyond manually running it) that the added bb support actually works. I'll have to rely on lein classpath to set the classpath for babashka.

I don't have time (nor desire) to learn Travis.

@weavejester
Copy link
Owner

Keep the GitHub Actions for now; I'll use it for reference as part of another PR. I've been meaning to switch a project over to GitHub Actions for evaluation, and this seems like a good opportunity to do so.

@borkdude
Copy link
Contributor Author

@weavejester The only thing left is if you want to keep this deps.edn:

https://github.com/weavejester/cljfmt/pull/293/files#diff-f65a787e85051fde7ed000568b4cfb9277627049a38f0569c8d9e238e70b622b

I can generate it dynamically in CI to test bb or use lein classpath but if you want to support deps.edn at some point anyway, I think it would be good to leave it in as it simplifies testing with babashka. I would understand if you'd rather leave it out, so please let me know.

@@ -1,5 +1,5 @@
(ns cljfmt.test-util.cljs
(:require [cljs.test :as test]
(:require [clojure.test :as test]
Copy link
Contributor Author

@borkdude borkdude Jan 31, 2023

Choose a reason for hiding this comment

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

This change was needed since bb loads this namespace during tests and cljs.test isn't part of bb. Loading clojure.test also works in ClojureScript so this change doesn't change any behavior.

@borkdude
Copy link
Contributor Author

If you don't like the formatting of the generated deps.edn, of course we can do something about that now cljfmt works in bb ;).

@borkdude
Copy link
Contributor Author

Note that adding a deps.edn has also been requested in #133

@borkdude
Copy link
Contributor Author

borkdude commented Jan 31, 2023

(again, I'd rather maintain one dependency file than two).

The solution I've chosen uses lein2deps which automatically generates deps.edn file from a project.clj so you'd only have to maintain the project.clj one (and call bb gen-deps) until you decide to migrate.

@borkdude
Copy link
Contributor Author

To reduce friction even more, I just published a lein plugin for lein2deps:

https://github.com/borkdude/lein2deps#lein-plugin

@weavejester
Copy link
Owner

The solution I've chosen uses lein2deps which automatically generates deps.edn file from a project.clj so you'd only have to maintain the project.clj one (and call bb gen-deps) until you decide to migrate.

There was also a project that I recall did the opposite: given a deps.edn file, it would fill in the dependencies in Leiningen. The advantage of doing it that way around is that there's no need to put generated files in the repository.

Ultimately I'd like to move over to tools.deps, but Leiningen handles deploying to Clojars or other Maven repositories better than any tool I've discovered for tools.deps. I've been attempting to fix this, but it hasn't been particularly interesting work, and I've kept putting it off.

@borkdude
Copy link
Contributor Author

borkdude commented Feb 1, 2023 via email

@weavejester
Copy link
Owner

weavejester commented Feb 1, 2023

The tools.deps ecosystem isn't mature enough for me to switch, yet, so don't worry about it. Leiningen currently has better pom generation and GPG integration, last I checked, so I'm waiting for tools.deps to get up to par (or eventually, I'll fix it myself).

@borkdude
Copy link
Contributor Author

borkdude commented Feb 1, 2023 via email

@weavejester
Copy link
Owner

Sure. Can you let me know how to finish up this PR?

Ah, sorry for the confusion, I should have been much clearer about my intent. I was planning on converting the repository over to GitHub Actions, then getting back to you. That's now done.

Could you rebase against master and instead of the bb.yml workflow, could you add the Babashka tests to the new test.yml workflow? My thought is something like:

      - name: Install clojure tools
        uses: DeLaGuardo/setup-clojure@10.1
        with:
          lein: 2.9.10
          bb: latest

      - name: Run Leiningen tests
        working-directory: ./cljfmt
        run: lein test

      - name: Run Babashka tests
        working-directory: ./cljfmt
        run: bb test

My understanding is that Babashka requires deps.edn, unless it pulls the classpath from Leiningen. Is that correct? If so, let's keep deps.edn, as anything else feels like a hack. However, could you also remove gen-deps from this pull request, as I'd like to keep it focused on the minimal changes necessary to implement this feature.

Also, for now, I think it might make more sense for the test:bb job to be test, at least until there's different test profiles to run.

Finally, could you squash all the commits and change the final commit message to:

Add support for Babashka (fixes #292)

This is just to make it more consistent with previous commits.

@borkdude
Copy link
Contributor Author

borkdude commented Feb 1, 2023

Absolutely, will do :)

@borkdude borkdude force-pushed the master branch 2 times, most recently from 213a989 to d1e22d6 Compare February 2, 2023 09:14
@borkdude
Copy link
Contributor Author

borkdude commented Feb 2, 2023

@weavejester Should be all good now.

@borkdude
Copy link
Contributor Author

borkdude commented Feb 2, 2023

It seems I forgot to add bb in the github runner, will do it now.

@borkdude
Copy link
Contributor Author

borkdude commented Feb 2, 2023

Done. If you can re-trigger CI once more, that would be great.

@weavejester
Copy link
Owner

Looks good!

@weavejester weavejester merged commit 7dfd55d into weavejester:master Feb 2, 2023
@borkdude
Copy link
Contributor Author

borkdude commented Feb 2, 2023

Thanks!

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.

None yet

3 participants