clj-kondo

Stig Brautaset 2024-10-11T11:14:25.354899Z

👋 is there a way to disable all info level diagnostics from the commandline? I'd like to do so in CI, as all the info lines are obscuring the warnings we want folks to fix. Edit: currently I'm doing something like

clj-kondo --lint src:test --config '{:linters {:shadowed-var {:level :off} :unused-binding {:level :off}}}'
to address some very noisy ones, but I've got a handful more and it's getting a bit long.

borkdude 2024-10-11T11:20:22.405249Z

Perhaps also:

--fail-level <level>: minimum severity for exit with error code.  Supported values:
    warning, error.  The default level if unspecified is warning.
but this probably won't affect reporting...

borkdude 2024-10-11T11:20:47.251319Z

yeah you can simply use a file

Stig Brautaset 2024-10-11T11:56:09.080719Z

It would be great to have a flag to only show things that contribute towards a failed exit status. I can't imagine it's very useful to show info level notices in CI.

borkdude 2024-10-11T11:57:32.475589Z

right, we could introduce a new flag for this: --report-level: minimum level which is reported similar to the other one

❤️ 1
borkdude 2024-10-11T11:57:47.628569Z

issue / PR welcome

borkdude 2024-10-11T11:58:01.613419Z

(PR optional of course)

Stig Brautaset 2024-10-11T12:03:28.506799Z

I've set a reminder to write an issue at least. I'll take a look at the code (I've got it checked out...) but no promises about a PR 🙂

borkdude 2024-10-11T12:04:02.229579Z

sure, np. perhaps it's not much more difficult than what the other flag does

Stig Brautaset 2024-10-14T10:51:59.607619Z

I added a ticket: https://github.com/clj-kondo/clj-kondo/issues/2410 I wonder how you feel about making it implicit when you specify the --fail-level flag? That's how I phrased the request. I put in the notice that an explicit --report-level flag would also work for us, but I'm struggling to think of any time I would want to fail at a certain level but still see notices below that. (Other folks might have other use cases, of course.)

Stig Brautaset 2024-10-14T10:52:34.712509Z

I'm happy to rephrase my ticket if that is seen as an unwelcome backwards incompatible change.

borkdude 2024-10-14T10:54:33.116329Z

I think fail-level should only influence what it says it does, so let's not change that behavior

👍 1
Stig Brautaset 2024-10-14T11:09:38.062519Z

Thanks for the steer. Issue updated 👍

Stig Brautaset 2024-10-14T11:21:30.761079Z

I'm taking a look to see if I could contribute this option myself. Could it be as simple as adding a filter somewhere near here? https://github.com/stig/clj-kondo/blob/master/src/clj_kondo/core.clj#L262 - maybe in the filter-findings function?

borkdude 2024-10-14T11:22:52.118799Z

That's probably too early. I'd do it near where the stuff is printed (reported)

Stig Brautaset 2024-10-14T11:25:55.594619Z

Right, so about here: https://github.com/stig/clj-kondo/blob/master/src/clj_kondo/core.clj#L34

Stig Brautaset 2024-10-14T11:26:15.585849Z

I can take a stab at that.

borkdude 2024-10-14T11:29:01.570709Z

yeah

Stig Brautaset 2024-10-14T11:44:36.978679Z

Hmm, doing the filter at the reporting time means we still get the "warnings: N" part in the summary, but that's probably desired anyway?

borkdude 2024-10-14T11:45:55.435139Z

we could omit that perhaps according to the report level

👍 1
Stig Brautaset 2024-10-14T11:59:04.329689Z

Ok, I have an implementation that appears to work:

stig@cci-stig-9c7j1:~/src/clj-kondo/ > clojure -M:clj-kondo/dev --lint - <<< "(defn foo [x] :foo)" 
:1:12: warning: unused binding x
linting took 55ms, errors: 0

stig@cci-stig-9c7j1:~/src/clj-kondo/ > clojure -M:clj-kondo/dev --lint - <<< "(defn foo [x] :foo)" --report-level info 
:1:12: warning: unused binding x
linting took 49ms, errors: 0

stig@cci-stig-9c7j1:~/src/clj-kondo/ > clojure -M:clj-kondo/dev --lint - <<< "(defn foo [x] :foo)" --report-level warning
:1:12: warning: unused binding x
linting took 51ms, errors: 0

stig@cci-stig-9c7j1:~/src/clj-kondo/ > clojure -M:clj-kondo/dev --lint - <<< "(defn foo [x] :foo)" --report-level error
linting took 50ms, errors: 0

stig@cci-stig-9c7j1:~/src/clj-kondo/ > clojure -M:clj-kondo/dev --lint - <<< "(defn foo [x] :foo)" --report-level orrery
[ prints help ]
I don't find any existing tests for the clojure.core/print! function. Does that mean none are required? 😅

Stig Brautaset 2024-10-14T12:14:21.710639Z

https://github.com/clj-kondo/clj-kondo/pull/2411 is a draft PR while I look at adding a test

Stig Brautaset 2024-10-14T13:22:54.970779Z

I've added a test (and moved it in a separate commit, as I found a better place to put it 😂 )

Stig Brautaset 2024-10-14T13:23:54.865199Z

There are some test failures in CI that I can't connect to my change, apparently all from this function: https://github.com/stig/clj-kondo/blob/report-level/test/clj_kondo/main_test.clj#L38-L62

borkdude 2024-10-14T13:26:22.765899Z

(set (drop-while #(not= report-level %)
                                       [:info :warning :error])
isn't this the same as (disj #{:info :warning :error} report-level)?

borkdude 2024-10-14T13:27:46.909749Z

I'll take a look at the CI error later

borkdude 2024-10-14T13:30:03.518569Z

oh I see drop-while , yeah that's not the same

Stig Brautaset 2024-10-14T13:31:08.916189Z

Absolutely open to better ways to write that! It was the second option that came to me. (The first one didn't work 😅 )

borkdude 2024-10-14T13:32:09.144649Z

one way to look into the CI issue would be to see if you get this error locally as well vs locally with the master branch

Stig Brautaset 2024-10-14T13:38:31.002609Z

will do. I get it locally, but I haven't checked with the master branch. I'll do that now.

borkdude 2024-10-14T13:39:16.215349Z

if you don't get it on the master branch, it's probably something with the code

👍 1
Stig Brautaset 2024-10-14T13:40:50.553469Z

Ooh, interesting. I don't see it locally on master, so it is my code. I just can't see how/why (yet)

borkdude 2024-10-14T13:41:14.517259Z

interesting :)

Stig Brautaset 2024-10-14T13:45:48.246419Z

I've narrowed it down to the test failures going away if I remove the when I introduced here: https://github.com/stig/clj-kondo/blob/report-level/src/clj_kondo/core.clj#L37

borkdude 2024-10-14T13:47:18.866009Z

maybe there's something weird with the level there. perhaps you can print the level to see if it's nil or something

borkdude 2024-10-14T13:47:20.935429Z

brb

Stig Brautaset 2024-10-14T13:49:17.769899Z

haha! Yes, the level is :warn in that test, I'll fix that... it's supposed to be :warning I assume?

borkdude 2024-10-14T13:49:42.610829Z

guess so :)

borkdude 2024-10-14T13:50:05.021459Z

or is it warn everywhere else except in your code? don't remember ;)

Stig Brautaset 2024-10-14T13:51:03.491199Z

it seems to be :warning everywhere else

borkdude 2024-10-14T13:53:08.682949Z

ok change it then :)

👍 1
borkdude 2024-10-14T14:37:18.849749Z

made one small comment, rest looks good

Stig Brautaset 2024-10-14T15:22:45.468099Z

Thanks. (Hopefully) addressed your comments!

borkdude 2024-10-14T15:23:58.240389Z

almost. (or (set x) y) will always return (set x) so you need to do something like this:

(if report-level ... (constantly true))

borkdude 2024-10-14T15:24:45.631179Z

and I think the report-level keyword is only ever used in the context of that function so you can just wrap the two expressions into one let binding

👍 1
borkdude 2024-10-14T15:25:51.401139Z

so instead of this:

report-level (some-> report-level keyword)
        report-level? (or (set (drop-while #(not= report-level %)
                                           [:info :warning :error]))
                          (constantly true))
I would write:
report-level? (if report-level (let [k (keyword report-level)] ...) (constantly true))

👍 1
Stig Brautaset 2024-10-14T15:26:13.053529Z

Doh! thanks

Stig Brautaset 2024-10-14T15:39:21.350789Z

I'm adding a slight tweak to that to cater for :report-level "piffle"

borkdude 2024-10-14T16:01:57.066659Z

if the duplication bothers you, you can also write:

(or (when report-level (let .... (when-let [levels ...])) (constantly true))

Stig Brautaset 2024-10-14T16:04:49.459609Z

I thought of that, but I don't feel strongly about it. Happy to make the change. Whatever you prefer.

borkdude 2024-10-14T16:06:03.662739Z

I actually don't care about making report-level :piffle work so we could simplify that code a bit. I only care about when not setting report-level the stuff works as before (hence the fallback to constantly true)

Stig Brautaset 2024-10-14T16:06:55.140199Z

aha! yes, that makes sense. I misunderstood your feedback here then: https://github.com/clj-kondo/clj-kondo/pull/2411#discussion_r1799631456

borkdude 2024-10-14T16:07:55.615009Z

ah yeah makes sense

borkdude 2024-10-14T16:08:58.006149Z

I would say for a custom log level the min level can't be decided so either all or nothing can be argued for. I think showing nothing is fine (we just don't support it)

👍 1
Stig Brautaset 2024-10-14T16:40:34.831139Z

Thanks for all the help! Now there's a shiny happy on that PR.

👍 1
borkdude 2024-10-14T17:18:00.704649Z

Thanks a lot!

Stig Brautaset 2024-10-14T17:19:36.528789Z

No, thank you for writing an amazing tool that I rely on at work, and for being cool about helping me with my first contribution 😄

Felipe 2024-10-11T18:37:39.798029Z

how do I run the analysis tools? I'm trying

clj -A:flib -M -m clj-kondo.tools.popular-vars 10 src
loaded
Execution error (FileNotFoundException) at clojure.main/main (main.java:40).
Could not locate clj_kondo/tools/popular_vars__init.class, clj_kondo/tools/popular_vars.clj or clj_kondo/tools/popular_vars.cljc on classpath. Please check that namespaces with dashes use underscores in the Clojure file name.
and have checked clj-kondo is in my classpath at the latest version with clj -A:flib -Spath | grep clj-kondo EDIT: of course I have found it five seconds after posting:
To run the tools on your system you will need the Clojure CLI tool version 1.10.1.466 or higher and then use this repo as a git dep:

{:deps {clj-kondo/tools {:git/url ""
                         :sha "1ed3b11025b7f3a582e6db099ba10a888fe0fc2c"
                         :deps/root "analysis"}}} 

✅ 1