clj-kondo

cjohansen 2025-06-14T21:36:35.842999Z

I'm trying to write a hook for a macro I wrote that is "function like". I'm not succeeding ๐Ÿ˜… Details in ๐Ÿงต

cjohansen 2025-06-15T07:58:14.646839Z

I figured out how to reproduce it at least! If the macro is defined in a cljc file, it stops working consistently for me: https://github.com/cjohansen/kondo-macro-woe

borkdude 2025-06-15T08:03:11.973789Z

what if you add to this:

(:require-macros [mylib.core])
:refer [defthing]?

borkdude 2025-06-15T08:03:36.884139Z

I thought that is how it works normally in CLJS

borkdude 2025-06-15T08:03:53.272879Z

and then wrap the macro in a :clj reader conditional

borkdude 2025-06-15T08:04:04.248839Z

to avoid adding a macro like thing at runtime in CLJS

cjohansen 2025-06-15T08:05:47.232809Z

I'm not an expert in this, but I do have :require-macros in mylib.core, and this all compiles and runs as expected.

borkdude 2025-06-15T08:06:21.018899Z

can you try it though?

borkdude 2025-06-15T08:06:28.049029Z

I think this is what clj-kondo might expect

cjohansen 2025-06-15T08:06:39.747959Z

To put that in the test namespace?

borkdude 2025-06-15T08:06:45.702449Z

no in the macro namespace

cjohansen 2025-06-15T08:07:06.583539Z

I think I'm misunderstanding, because it's already there? https://github.com/cjohansen/kondo-macro-woe/blob/main/src/mylib/core.cljc

borkdude 2025-06-15T08:07:18.789279Z

(ns mylib.core
  #?(:cljs (:require-macros [mylib.core :refer [defthing]])))

cjohansen 2025-06-15T08:07:27.356229Z

ah, add refer

cjohansen 2025-06-15T08:08:29.669399Z

Still fails

borkdude 2025-06-15T08:08:43.424089Z

ok, I'm afk most of today, I'll take a look when I can

cjohansen 2025-06-15T08:08:50.303699Z

Sure, no hurry

cjohansen 2025-06-15T08:09:47.835679Z

What's interesting to me is that it even fails usage/linting in a clj file. It seems to be related to the macro coming from a cljc file. If I rename the core file to core.clj, test.clj lints as expected.

borkdude 2025-06-15T08:10:19.638009Z

yeah it might be a cljc + inline config related bug

cjohansen 2025-06-14T21:38:49.053999Z

I have a macro in src/myproject/api.clj that, in use, looks like this:

(defthing some-name [args]
  ,,,)
I want clj-kondo to recognize this as defining a var some-name in the current namespace, and to evaluate the body with the argument bindings available. There is src/no.cjohansen/myproject/config.edn with:
{:linters {:myproject/api {:level :warning}}
 :hooks {:analyze-call {myproject.api/defthing myproject.defs/defthing}}}
There is also src/no.cjohansen/myproject/defs.clj with:
(ns nexus.defs
    (:require [clj-kondo.hooks-api :as api]))

(defn- extract-docstr
       [[docstr? & forms :as remaining-forms]]
       (if (api/string-node? docstr?)
           [docstr? forms]
         [(api/string-node "no docs") remaining-forms]))

(defn defthing [{:keys [node]}]
  (let [[fname & forms] (rest (:children node))
        [docstr [attr-map & body]] (extract-docstr forms)]
    {:node
    (api/list-node
     (list*
      (api/token-node 'defn)
      fname
      docstr
      attr-map
      body))}))
I then have this test code:
(require '[clj-kondo.core :as clj-kondo])

(defn get-findings [code]
  (:findings
   (with-in-str
     (str
      '(require '[myproject.api :as myp])
      code)
     (clj-kondo.core/run! {:lint ["-"]}))))

(def code
  '(myp/defthing doit [a b]
     ))

(get-findings code)
And it returns all the warnings - unused binding doit etc. What am I missing? I copied everything from replicant, where things are working ๐Ÿค”

borkdude 2025-06-14T21:42:53.084209Z

is the syntax the same as clojure.core/defn?

cjohansen 2025-06-14T21:43:11.916779Z

Yes

borkdude 2025-06-14T21:43:27.688119Z

you could just do this:

{:lint-as {your.macro-ns/macro clojure.core/defn}}

cjohansen 2025-06-14T21:43:45.651969Z

hah

cjohansen 2025-06-14T21:44:20.287989Z

Can I put that as meta data on the macro itself? This is for a library.

borkdude 2025-06-14T21:44:55.346519Z

yeah, in your lib:

(defmacro defthing
  {:clj-kondo/lint-as 'clojure.core/defn}
  [...] ...)

borkdude 2025-06-14T21:45:11.081659Z

(let me double check if that is correct)

cjohansen 2025-06-14T21:45:38.860979Z

That seems way easier. Wondering why I've opted for this complicated solution in Replicant ๐Ÿ˜…

borkdude 2025-06-14T21:45:55.380409Z

Perhaps there are subtle syntax changes between your macro and defn

borkdude 2025-06-14T21:46:13.452889Z

else why would you first extract docstring etc and then pass it on to defn

borkdude 2025-06-14T21:46:33.617759Z

you might as well could pass the whole body to clojure.core/defn straightaway

cjohansen 2025-06-14T21:47:06.073979Z

I couldn't tell you - I don't remember why I wrote it the way I did ๐Ÿ˜„

borkdude 2025-06-14T21:47:38.743289Z

if your test suite lints correctly using this config, I'd say you should be fine ;)

borkdude 2025-06-14T21:48:01.646569Z

you do need to lint the macro ns first before the config takes effect

borkdude 2025-06-14T21:48:31.293919Z

but in a normal clj-kondo workflow you first lint your dependencies, which is where this configuration is detected

cjohansen 2025-06-14T21:49:06.789339Z

I mainly use clj-kondo via lsp, not sure what it does

borkdude 2025-06-14T21:49:59.287359Z

via lsp that's the same

borkdude 2025-06-14T21:50:04.148709Z

it automatically does it

cjohansen 2025-06-14T21:50:05.652919Z

So now I have it partially working. Code evaluates, and go-to definition works, but lsp is still throwing squigglies ๐Ÿค”

borkdude 2025-06-14T21:50:37.539699Z

can you restart the lsp server to double check and then make a screenshot?

cjohansen 2025-06-14T21:54:22.424659Z

cjohansen 2025-06-14T21:54:33.648299Z

cjohansen 2025-06-14T21:54:40.842139Z

Squigglies are unresolved symbols

borkdude 2025-06-14T21:55:55.992179Z

could it be that you have another clj-kondo thing running in your emacs, e.g. flycheck-clj-kondo with an old clj-kondo version?

borkdude 2025-06-14T21:56:08.866949Z

best to try it on the command line and see what it spits out

borkdude 2025-06-14T21:56:33.182199Z

along with clj-kondo --version

cjohansen 2025-06-14T21:57:39.680589Z

clj-kondo --version clj-kondo v2025.01.16 Ancient, by your tempo, I would assume ๐Ÿ˜…

borkdude 2025-06-14T21:58:11.919509Z

that sounds ok, do you see the same on the command line, warnings?

cjohansen 2025-06-14T22:00:41.145379Z

Yeah, same warnings

cjohansen 2025-06-14T22:01:22.970299Z

Same with 2025.06.05

borkdude 2025-06-14T22:02:24.025629Z

do you have a .clj-kondo directory in the root of your project?

cjohansen 2025-06-14T22:02:35.450419Z

I do

borkdude 2025-06-14T22:02:40.938669Z

good

borkdude 2025-06-14T22:02:59.970299Z

would you mind makeing a github repro of this that I could clone locally? I'll take a look either tomorrow or monday

cjohansen 2025-06-14T22:03:25.331279Z

Sure!

borkdude 2025-06-14T22:04:00.466179Z

do you perhaps still have the old hook activated?

cjohansen 2025-06-14T22:04:09.805479Z

Could be?

cjohansen 2025-06-14T22:04:21.645969Z

Or not really, I made a new macro to test now

cjohansen 2025-06-14T22:04:42.626409Z

I'm trying it in a fresh repo, that'll answer it

borkdude 2025-06-14T22:06:14.455929Z

I'm trying in a scratch buffer. Before:

cjohansen 2025-06-14T22:06:14.840459Z

Well, that didn't work ๐Ÿ˜‚

borkdude 2025-06-14T22:06:27.747899Z

After:

cjohansen 2025-06-14T22:06:41.424559Z

yeah, I also got it working in a blank project

cjohansen 2025-06-14T22:07:03.933549Z

I'll try to clean up my other project a little then

cjohansen 2025-06-14T22:11:59.025609Z

WTF. I cleared out both .clj-kondo, .lsp and src/no.cjohansen and it's still failing ๐Ÿคฏ

borkdude 2025-06-14T22:12:35.118039Z

you could also zip your project and give it to me, so I can see it happening here perhaps

borkdude 2025-06-14T22:12:51.531439Z

if it's open source

cjohansen 2025-06-14T22:13:02.097699Z

It's about to be ๐Ÿ™‚

cjohansen 2025-06-14T22:14:30.873079Z

Thanks for your kind support! I will have one more look at this with a fresh eye tomorrow, and if I still can't figure it out I'll take you up on your offer and send you the code ๐Ÿ˜Š

borkdude 2025-06-14T22:14:47.596639Z

sure, I'm going to ๐Ÿ’ค now too!

borkdude 2025-06-14T22:15:48.851759Z

if all else fails you could always not use the inline config but a {:lint-as {...}} thing in a config file

cjohansen 2025-06-14T22:16:26.543019Z

The inline config seems to me to be the ideal solution. And it worked in a fresh directory, so I must be doing something stupid.

borkdude 2025-06-14T22:16:49.326449Z

you can check your .clj-kondo directory for inline configs

borkdude 2025-06-14T22:17:00.089059Z

either directly in it or inside imports (not sure)

cjohansen 2025-06-14T22:17:29.044419Z

there are some in imports, but not for the afflicted namespace

borkdude 2025-06-14T22:17:53.456759Z

there should be, else it's not going to work

borkdude 2025-06-14T22:18:16.818939Z

$ ls .clj-kondo/inline-configs
scratch.clj

borkdude 2025-06-14T22:18:34.614489Z

$ cat .clj-kondo/inline-configs/scratch.clj/config.edn
{:lint-as #:scratch{my-defn clojure.core/defn}}%

borkdude 2025-06-14T22:18:56.753119Z

something like that should appear but for your macro file

borkdude 2025-06-14T22:19:02.457609Z

afk now

๐Ÿ’ค 1
cjohansen 2025-06-14T22:19:32.005329Z

Thanks for the help so far! Sleep well ๐Ÿ™‚

borkdude 2025-06-17T09:24:11.351249Z

Sorry, I forgot all about this due to other stuff coming up. Would you mind filing a github issue and meanwhile probably it's best to use a config file rather than an inline config

borkdude 2025-06-17T09:24:15.252459Z

until I find out what's the issue

cjohansen 2025-06-17T09:43:25.616359Z

No worries ๐Ÿ™‚ I'll file an issue, but the good news is that I've ditched the idea of this specific macro ๐Ÿ˜„

borkdude 2025-06-17T09:44:33.469669Z

heheh

cjohansen 2025-06-17T11:05:48.662499Z

https://github.com/clj-kondo/clj-kondo/issues/2554

๐Ÿ™ 1