This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-09
Channels
- # asami (1)
- # babashka (4)
- # calva (22)
- # clerk (2)
- # clj-kondo (32)
- # cljdoc (2)
- # clojure (49)
- # clojure-europe (9)
- # clojure-norway (3)
- # clojurescript (4)
- # data-science (5)
- # datascript (8)
- # datomic (7)
- # events (1)
- # fulcro (3)
- # hyperfiddle (62)
- # malli (4)
- # missionary (3)
- # off-topic (41)
- # polylith (11)
- # re-frame (19)
- # remote-jobs (2)
- # sci (7)
- # sql (51)
- # tools-build (2)
- # yamlscript (38)
Hi. I continue to work on getting all the lints out of my team's repos this week. I got to a bunch of unresolved symbols due to our usage of hugsql
. I found @borkdude response on https://stackoverflow.com/questions/61727582/how-to-avoid-unresolved-symbol-with-clj-kond-when-using-hugsql-def-db-fns-macro, but wondered if a more automatic solution would be possible. I wrote the following code (that appears to do what I want from my REPL):
(ns hugsql
(:require [clojure.string :as string]))
(defn def-names [resource-path]
(let [lines (-> (str "src/" resource-path)
clojure.core/slurp
string/split-lines)]
(->> lines
(filter (fn [line]
(string/starts-with? line "-- :name")))
(map (fn [line]
(-> line
(string/split #" ")
(nth 2)))))))
(defmacro def-db-fns [file _options]
(->> (for [defn-name (def-names file)]
`(defn ~defn-name [db arg-map]))
(cons 'do)))
And mapped it in my clj-kondo
config. I now get the following output in clj-kondo
:
WARNING: error while trying to read hook for hugsql.core/def-db-fns: Could not resolve symbol: clojure.core/slurp
Is there a way to read a file using the SCI parser that clj-kondo
uses?We put this bit in a separate “queries” namespace:
(hugsql/def-db-fns "select_things.sql")
The indirection means that clj-kondo doesn’t complain.And we don’t have to put declare
anywhere.
Ok! I can do that.
Thanks. Let me give that a try.
Hmm, I think I did what you suggested, but I still get a lint:
src/voltron/queries/db.clj:17:4: warning: Unresolved var: queries/select-stuff
changed from unresolved symbol
to unresolved var
. Did you make any other changes?No, I don’t think so. I don’t see any exclusions in our config file.
I'll try making a little repo and see if I can get it to work.
Thanks
You can get rid off those unresolved-var stuffs by adding a bit more config:
{:linters {:unresolved-var {:exclude [the-hug-sql-vars.ns]}}}
@U03N9E40Q2F On my trivial example project, your suggestion worked. Something must be a little different in my real repo.
Older version of clj-kondo?
clj-kondo v2023.07.13
The difference comes from having a non-empty .cache
entry for that namespace or not
That makes sense. Thanks.
if there is a non-empty one, then clj-kondo thinks its knows that namespace and will start to warn about that namespace
I deleted my cache and the warning went away. Thanks everyone for the help. And @borkdude for the great tool.
I think everything is working now, but I was still wondering if there is a way to read in a file in the interpreter that kondo uses?
The thing I can think of would be the example I wrote at the top of this thread. Hugsql reads in a file to generate defs so I would love to get the same functionality instead of excluding a namespace from some lints.
I use lsp through calva, but for PR checks we use clj-kondo from the command line.
ok. for clojure-lsp you could use stubs: https://clojure-lsp.io/settings/#stub-generation it uses this little library: https://github.com/clj-easy/stub you could just create some stubs manually and add that to a :clj-kondo alias in your deps.edn and add it to the lint classpath
this would probably be an easier route than trying to parse .sql files in a clj-kondo hook
Thanks, I'll take a look
Seems like a leading hyphen in a function name as in -my-function
is very common to denote a “private” function. Is there a Kondo check for misuse? (Invoking the function from another namespace)
if the function is private, you should use the ^:private
metadata. -
does not always mean private, e.g. -main
is a common convention
Yeah, I knew it wasn’t “canon”, but I see it a lot, I suppose to avoid the extra referencing in the tests.
In particular -main
uses a -
to indicate that it should be compiled to a Java main
function -- so you'll see a leading hyphen in any code that is gen-class'd for producing Java-compatible methods.
-
is also a convention in ClojureScript protocols, it doesn't necessarily mean private. it could mean "lower level function" but it was specifically NOT made private, because other namespaces (of the same library) sometimes need to call into those lower level functions