Fork me on GitHub
#clj-kondo
<
2022-01-31
>
neil-dw15:01:47

I'm using clj-kondo 2022.01.15 (with the latest Calva version), and it's treating clojure.core/alias as undefined. Is that a known issue? [Update: not a Kondo issue, I just didn't understand the behavior I should expect in a .cljc file. Thanks @borkdude!]

1
borkdude15:01:28

@U02STT6CVK5 Need more info. Can't repro using a simple example locally here.

borkdude15:01:20

Are you in a .cljc file?

neil-dw15:01:46

@borkdude: I was, actually. I get the same result in a .cljs file. But I see that trying it in a .clj file works.

borkdude15:01:44

Yes, the issue is that alias is not defined in ClojureScript. So in a .cljc branch you'll have to put that code in a reader conditional.

neil-dw15:01:03

Ah, that makes sense! Thank you!

lread15:01:40

This open issue is related: https://github.com/clj-kondo/clj-kondo/issues/1196, maybe someday I’ll find time to work on it! simple_smile

borkdude15:01:32

Feel free to upvote the issue for higher priority. :)

Noah Bogart19:01:03

Is it possible to have a linter check if a given required namespace exists and warn if it doesn’t?

borkdude19:01:33

@nbtheduke That isn't yet available. Could be done though.

👍 1
borkdude19:01:10

There is one caveat: clj-kondo works and has always worked with partially linted projects

Noah Bogart19:01:32

what do you mean by “partially linted projects”?

borkdude19:01:54

That you're not required to lint your project + deps to have good feedback or false positives due to not doing that

borkdude19:01:01

So if there is a namespace not available in the cache, then clj-kondo just assumes you haven't linted that one yet, it can't see the difference between: it doesn't exist or it might exist maybe in the future, but I haven't seen it yet

👍 1
💯 1
borkdude19:01:22

Perhaps it's better if clojure-lsp did this linter, because clojure-lsp does require you to index the whole project. Similar to why :clojure-lsp/unused-public-var is implemented by lsp and not by clj-kondo out of the box

borkdude19:01:27

Or clj-kondo should have something in its cache that signals that clj-kondo --lint $classpath --dependencies has linted the entire project

borkdude19:01:58

and then it could with more certainty say: this ns doesn't exist

borkdude19:01:42

Maybe we could write such a file when you lint using --dependencies and after that activate this project-wide linter

borkdude19:01:19

There is one more edge case I could come up with, is when you rename namespaces, then clj-kondo might still think the old name still exists since there's that file lingering in the cache

borkdude19:01:53

but that might be better than nothing

borkdude19:01:18

ok, let's do it. I'll make an issue about it

Noah Bogart19:01:22

It’s interesting how the classpath makes this more challenging than, say, a node project where it’s as “simple” as searching first the local files and then node_modules for a given filepath

borkdude19:01:17

well, when you've linted your entire project, the linter is easy. it's just that you shouldn't emit warnings when you haven't.

borkdude19:01:30

clj-kondo has a file for each namespace in its cache

borkdude19:01:47

also partitioned by clj, cljs and cljc

borkdude19:01:21

When you lint a file, like (ns foo (:require [clojure.set])) then clj-kondo will read the file .cache/v1/clj/clojure/set.transit.json into memory

borkdude19:01:47

so when there is (:require [clojure.setx]) (typo), there should be no file and then it's a warning

Noah Bogart19:01:35

huh yeah, that makes sense

Noah Bogart19:01:17

so it’s a… two step process? or multi-step at least. first pass is “add a file to the cache for each file in the class path”, and then the second pass allows for referencing and filling those files?

borkdude19:01:55

every time clj-kondo lints a file, it writes the result of analyzing that file to the cache

borkdude19:01:09

so if you start with an empty cache and just start editing your project, the linting gets increasingly more informed.

Noah Bogart19:01:52

which is why it doesn’t attempt to do this kind of linting yet, if you lint a single file it won’t have any other files available in the cache. okay, that makes more sense

Noah Bogart19:01:01

thanks for explaining